Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 14 additions & 5 deletions linux/LinuxProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ in the source distribution for its full text.
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -1040,31 +1041,39 @@ static void LinuxProcessTable_readCGroupFile(LinuxProcess* process, openat_arg_t
char output[PROC_LINE_LENGTH + 1];
output[0] = '\0';
char* at = output;
int left = PROC_LINE_LENGTH;
size_t left = PROC_LINE_LENGTH;
while (!feof(file) && left > 0) {
char buffer[PROC_LINE_LENGTH + 1];
const char* ok = fgets(buffer, PROC_LINE_LENGTH, file);
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];
Copy link
Copy Markdown
Contributor

@Explorer09 Explorer09 Feb 24, 2026

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

Copy link
Copy Markdown
Member Author

@BenBE BenBE Feb 24, 2026

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 = ';';
at++;
left--;
}

int wrote = snprintf(at, left, "%s", group);
left -= wrote;
if (wrote < 0 || (size_t)wrote >= left) {
// Output was truncated, we are done
break;
}

at += (size_t)wrote;
left -= (size_t)wrote;
}
fclose(file);

Expand Down
Loading