Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced xMalloc+xSnprintf with xAsprintf in PCP platform code #1587

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kingmidas-hack
Copy link

Hi! I submitted a PR in hopes this would suffice. After refactoring xMalloc+xSnprintf to xAsprintf within

~/pcp/PCPDynamicColumn_addMetric.c
~/pcp/PCPDynamicMeter_lookupMetric.c
~/pcp/PCPDynamicScreen_lookupMetric.c

I was able to compile and test the changes (make clean && make) with no hassle within macOS. I'm open to any constructive criticism for writing C and overall best practices for maintaining large codebases. Thanks all!

@Explorer09
Copy link
Contributor

Please remove docs/km-notes.md from the commit. You likely added that by mistake.

@kingmidas-hack kingmidas-hack force-pushed the replace-xmalloc-with-xasprintf branch from 2dcc0d5 to 6b80b3c Compare January 15, 2025 21:47
@kingmidas-hack
Copy link
Author

kingmidas-hack commented Jan 15, 2025

@Explorer09 Oof my apologies. This is done!

@BenBE BenBE added enhancement Extension or improvement to existing feature code quality ♻️ Code quality enhancement labels Jan 16, 2025
@BenBE BenBE added this to the 3.4.0 milestone Jan 16, 2025
Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

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

Did you compile a PCP version of htop to perform your checks? Because the default on darwin won't cover these platform files.

pcp/PCPDynamicScreen.c Outdated Show resolved Hide resolved
@@ -71,13 +71,11 @@ static void PCPDynamicScreens_appendDynamicColumns(PCPDynamicScreens* screens, P

static PCPDynamicColumn* PCPDynamicScreen_lookupMetric(PCPDynamicScreen* screen, const char* name) {
PCPDynamicColumn* column = NULL;
size_t bytes = strlen(name) + strlen(screen->super.name) + 1; /* colon */
if (bytes >= sizeof(column->super.name))
if ((strlen(name) + strlen(screen->super.name) + 1) >= sizeof(column->super.name)) /* colon */
Copy link
Member

Choose a reason for hiding this comment

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

xAsprintf already returns the actual string length. Thus you could use the return value below for this check (although this means we will allocate memory here in case of overlong column names). But given this would be on the error path this should happen rarely enough to not matter.

Copy link
Author

@kingmidas-hack kingmidas-hack Jan 16, 2025

Choose a reason for hiding this comment

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

Hey there! I appreciate your feedback! I've just committed a change for the indentation. As for using xAsprintf's return value, I see how this helps! To my understanding and in terms of scale, it's better to allocate memory first, then check the size and free() (if necessary) - than to check the size first, then allocate? Which I guess would make sense since it handles any buffer overflow.

I have not yet tested compiling a PCP version, though brew does provide PCP tooling. I can try testing but am curious if opening another branch is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

@BenBE This looks bad. While PCPDynamicScreen_lookupMetric() could return NULL, there are no NULL checks on the only caller of the function, PCPDynamicScreen_parseColumn(). That is, you get undefined behavior with an overlong string.

Copy link
Member

Choose a reason for hiding this comment

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

@Explorer09 This would be a bug with the current code base too. A fix would be independent of this change.

@BenBE BenBE linked an issue Jan 16, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality ♻️ Code quality enhancement enhancement Extension or improvement to existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid strlen+xMalloc+xSnprintf, use xAsprintf instead
3 participants