Today's bug challenge is a design bug.
Came across some C code like this awhile back (this is pseudo code and has all "work" elements removed from it):
void
{
strcpy(szID," ");
strcat(szID,pszObjID);
strcat(szID," ");
strcat(szIDList,szID);
}
The code uses a char array to store the IDs so that it can use strstr() to search the list before adding a new ID to it. Each ID is padded on the left and right with a space in order to simplify this check.
I discovered all this because this code lived in a DLL that my app used and was AVing. It turned out that a different value was used to size the ID list than was used to constrain the number of IDs that could be added to the list. In other words, there was a buffer overrun – a fairly simple, garden-variety one.
Can you identify the design bug here? Can you think of a way to improve this design (keep your solution in C/C++)? What alternative way of storing the list can you come up with that would not only be more efficient in terms of memory, but also facilitate quick searches for IDs? What mechanism can we employ to avoid the possibility of a buffer overrun altogether?