CodeSOD: Oh, K?
Pierre found this cry for help:
for (j = 0; j < DISTRICT_CARDINALITY; j++) { /* * A poor way to work around some sort of memory * violation. k gets clobbered, so I buffer l with it. * Help? */ for (k = 0, l = 0; k < customers; k++, l++) { /* c_id */ FPRINTF(output, "%d", l + 1); METAPRINTF((output, "%c", delimiter));Honestly, I'm always a little worried in C-like languages when the loop variable isn't scoped to the loop and declared in place. Always concerning, so when there's concerns with clobbering, I'm not surprised.
This iterates across a set of "districts", and then iterates across a list of customers. Standard output functions are replaced with macros, and honestly, I don't know that I want to know what happens in METAPRINTF.
But fine, this iterates across a bunch of stuff and generates output. It's the comment that we're really curious about. "Some sort of memory violation", so we duplicate our loop variable. What on earth could be happening here?
Well, Pierre found the "memory violation", which is probably what you expected:
k = strlen(someOutputString) - 8;
k is used elsewhere in the loop as a variable. That's not a memory violation, that's just how variables work.
This leaves us with one possible conclusion: the developer who wrote the comment couldn't find this bug. And given the weird use of macros here, I'm going to go out on a limb and guess that it's not the developer's fault; I strongly suspect the loop is so long, so complicated, and so obfuscated that very few developers could find that.
My earlier complaint about scoping the variable to the loop wouldn't fix this, which I guess is on me. They still should really scope the variable to the loop.