Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ ATTR_NONNULL ATTR_MALLOC
char* String_readLine(FILE* fp);

ATTR_NONNULL ATTR_RETNONNULL
static inline char* String_strchrnul(const char* s, int c) {
static inline const char* String_strchrnul(const char* s, int c) {
#ifdef HAVE_STRCHRNUL
return strchrnul(s, c);
#else
char* result = strchr(s, c);
const char* result = strchr(s, c);
if (result)
return result;
return strchr(s, '\0');
Expand Down
7 changes: 4 additions & 3 deletions linux/LinuxProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1047,16 +1047,17 @@ static void LinuxProcessTable_readCGroupFile(LinuxProcess* process, openat_arg_t
if (!ok)
break;

char* group = buffer;
const char* group = buffer;
for (size_t i = 0; i < 2; i++) {
group = String_strchrnul(group, ':');
if (!*group)
break;
group++;
}

char* eol = String_strchrnul(group, '\n');
*eol = '\0';
const char* eol = String_strchrnul(group, '\n');
char* eol_w = &buffer[eol - buffer];

@Explorer09 Explorer09 Feb 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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';

@BenBE BenBE Feb 24, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my reading of the ISO C99 standard:

  • &p[i] returns a pointer to the ith element in p using the type of p
  • b - a returns uintptr_t in units of the pointed-to types for any a and b that point inside the same object (array) in memory, if the pointed-to types are compatible, and not incomplete; irrespective of cv-qualifiers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

*eol_w = '\0';

if (at != output) {
*at = ';';
Expand Down