XUtil: const-correctnes for strchrnul#1916
Conversation
| char* eol = String_strchrnul(group, '\n'); | ||
| *eol = '\0'; | ||
| const char* eol = String_strchrnul(group, '\n'); | ||
| char* eol_w = &buffer[eol - buffer]; |
There was a problem hiding this comment.
How does this &buffer[eol - buffer]; not trigger warnings at all?
Update: I have a better idea to the problem:
size_t eol_offset = (size_t)(eol - (const char*)buffer);
buffer[eol_offset] = '\0';There was a problem hiding this comment.
Based on my reading of the ISO C99 standard:
&p[i]returns a pointer to theith element inpusing the type ofpb - areturnsuintptr_tin units of the pointed-to types for anyaandbthat point inside the same object (array) in memory, if the pointed-to types are compatible, and not incomplete; irrespective of cv-qualifiers.
There was a problem hiding this comment.
b - a should return a ptrdiff_t, but if they are in different types a compiler may label it as a warning (a strict aliasing violation, maybe). A compiler wasn't told that the pointer returned from strchrnul is derived from the first argument passed into it, and so it doesn't know that b - a would work unless it can examine the body of strchrnul function.
Note: I don't like the constness thing at all. Part of the PR #1915 that I was recently doing had been dealing with -Wcast-qual warnings from GCC that over-warns things. (Not strictly a false positive, but GCC wasn't taught that execv() is special that it has to make an exception and cast away the const qualifier.)
1369bc3 to
08f7356
Compare
This contains multiple parts:
strchrnulLinuxProcessTable_readCGroupFileby handling thesnprintfretur values