Common Boundary Value Bugs in Software Testing and Their Fixes
This article reviews three typical boundary‑value bugs encountered during software testing—incorrect memory size checks, off‑by‑one retry counters, and misplaced increment operations—explaining their causes, showing original C code, and providing concise corrective solutions.
Long testing experience shows that many errors occur at input or output range boundaries rather than inside the range, so this second article in a series presents three typical boundary‑value bugs with examples and fixes.
Boundary Bug 1 : When checking whether a destination buffer can hold a formatted string, the condition uses if((size_t)dest_size < strlen(idc) + strlen(host) + strlen(tmp)) { LOG_ERR("%s failed. Dest buffer_size is not enough.",_func_); return ERR_BUF_NOT_ENOUGH; } which neglects the terminating null character, causing the last character to be dropped. Solution : change the comparison to <= so the null terminator is accounted for.
Boundary Bug 2 : The retry logic for sending messages increments try_send_times after each failure but starts from 0 and checks if (try_send_times < MAX_SEND_MSG_TIMES) , resulting in one extra send attempt (MAX+1). The relevant code is: if (status == -1) { if (errno == EAGAIN) { if (try_send_times < MAX_SEND_MSG_TIMES) { LOG_WARN("msg queue is full! try_send_times:%d",try_send_times); usleep(5000); try_send_times++; continue; } else { LOG_ERR("msg queue is full! try send message times are larger than MAX TIMES: %d",MAX_SEND_MSG_TIMES); } } else { LOG_ERR("Send msg failed! Err: %s", strerror(errno)); } } Solution : initialize try_send_times to 1.
Boundary Bug 3 : An increment operation placed inside a condition ( if(try_get_times++ < _max_repeat_read_times) ) increments the counter even when the condition fails, causing the logged execution count to be one higher than actual. The snippet is: if(try_get_times++ < _max_repeat_read_times) { … } … if (try_get_times >= _max_repeat_read_times) { LOG_ERR("%s: Error value of key: %s, Execute %d times", __func__,key, try_get_times); } Solution : move the increment inside the if block or log _max_repeat_read_times instead.
In summary, boundary‑value problems often hide in surrounding logic, so testers must analyze conditions and loops carefully rather than only checking the obvious boundaries.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.