Skip to content

Comments

XUtil: const-correctnes for strchrnul#1916

Merged
BenBE merged 3 commits intohtop-dev:mainfrom
BenBE:xutil-const-strchrnul
Feb 24, 2026
Merged

XUtil: const-correctnes for strchrnul#1916
BenBE merged 3 commits intohtop-dev:mainfrom
BenBE:xutil-const-strchrnul

Conversation

@BenBE
Copy link
Member

@BenBE BenBE commented Feb 24, 2026

This contains multiple parts:

  1. Fix a few warnings related to const-correctness and strchrnul
  2. Fix the buffer overflow handling in LinuxProcessTable_readCGroupFile by handling the snprintf retur values
  3. Actually advance our write location allowing for multiple entries to be read

@BenBE BenBE added this to the 3.5.0 milestone Feb 24, 2026
@BenBE BenBE requested a review from natoscott February 24, 2026 18:00
@BenBE BenBE added bug 🐛 Something isn't working enhancement Extension or improvement to existing feature code quality ♻️ Code quality enhancement Linux 🐧 Linux related issues PCP PCP related issues labels Feb 24, 2026
char* eol = String_strchrnul(group, '\n');
*eol = '\0';
const char* eol = String_strchrnul(group, '\n');
char* eol_w = &buffer[eol - buffer];
Copy link
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
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
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.)

@BenBE BenBE force-pushed the xutil-const-strchrnul branch from 1369bc3 to 08f7356 Compare February 24, 2026 21:57
Copy link
Member

@natoscott natoscott left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @BenBE

@BenBE BenBE merged commit d9ea8a6 into htop-dev:main Feb 24, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐛 Something isn't working code quality ♻️ Code quality enhancement enhancement Extension or improvement to existing feature Linux 🐧 Linux related issues PCP PCP related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants