1

foreword

Recently, a friend said that he encountered an unsolvable bug. He has a business requirement that only VIP users can participate in the business scenario. He judged that the pseudo-code form of vip is as follows

 private boolean isVip(UserDTO userDTO){
        List<Integer> vipUserIds = userService.vipUserIds();
        for (Integer vipUserId : vipUserIds) {
            if(vipUserId.equals(userDTO.getUserId())){
                return true;
            }
        }


        return false;
    }

He passed a vip user whose userId is 10000 (note: 10000 is sample data), and found that this judgment has always been false. So I found me to look at it for him. The code seems to be fine. I asked him to confirm whether the VIP user exists in the database. My friend is very sure that it exists.

Check

Because my friend is sure that this record exists in the database, I will ask him to add a log to the logic of vip. The modified pseudocode is as follows

 private boolean isVip(UserDTO userDTO){
        List<Integer> vipUserIds = userService.vipUserIds();
        for (Integer vipUserId : vipUserIds) {
            if(vipUserId.equals(userDTO.getUserId())){
                return true;
            }
        }

        log.warn("userId-->{} is not in vipList-->{}",userDTO.getUserId(),vipUserIds);

        return false;
    }

Then the log magically appears

When I saw this log, I was a little confused. A friend later told me that he suspected that there was a problem with the JDK version, but my sixth sense told me that this was not the case.

Years of experience in writing bugs told me that anything will deceive people, only source code will not deceive people. Because the code for judging whether or not vip is just an equal, click directly into it. The source code of this equal is as follows

 public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

After seeing this code, I think some old drivers should feel a little bit. Later, I will tell my friends whether you have written the wrong type. Is your userId type Integer? So he flipped through the code, and sure enough, the type of his userId was written as String

repair

Later, a friend changed the type of userId to Integer, and the problem was satisfactorily solved.

Summarize

Sometimes some bugs are difficult to solve, not because of technical difficulties, but because of some small details that we don't usually pay attention to.


linyb极客之路
336 声望193 粉丝