Anders sends us an example which isn't precisely a WTF. It's just C handling C strings. Which, I guess, when I say it that way, is a WTF.
while(modPath != NULL) {
p = strchr(modPath, ':');
if(p != NULL) {
*p++ = '\0';
}
dvModpathCreate(utSymCreate(modPath));
modPath = p;
} while(modPath != NULL);
We start with a variable called modPath
which points to a C string. So long as modPath
is not null, we're going to search through the string.
The string is in a :
separated format, e.g., foo:bar:goo
. We want to split it. This function does this by being very C about it.
It uses strchr
to find the address of the first colon. If we think about this in C strings, complete with null terminators, it looks something like this:
"foo:bar:goo\0"
^ ^
| |
| p
modPath
We then replace :
with \0
and increment p
, doing a "wonderful" blend of using the dereference operator and the post-increment operator and an assignment to accomplish a lot in one line.
"foo\0bar:goo\0"
^ ^
| |
| p
modPath
So now, modPath
points at a terminated string foo
, which we then pass down through some functions. Then we set it equal to p
.
"foo\0bar:goo\0"
^
|
p
modPath
This repeats until strchr
doesn't find a :
, at which point it returns NULL
. Our loop is guarded by a check that modPath
(which gets set equal to p
) can't be null, so that breaks us out of the loop.
And enters us immediately into another, single line loop with no body, which immediately exits as well. I suspect that originally this was written as a do{}while
, and then someone realized that it could just be a plain while{}
, and completely forgot to remove the second while
clause.
This is, honestly, a pretty common idiom in C. It's arguably wrong to even put it here; aside from the bad while
clause, you'll see this kind of string handling all the time. But, maybe, that is the WTF.