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
Open
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
5 changes: 2 additions & 3 deletions pcp/PCPDynamicColumn.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ static bool PCPDynamicColumn_addMetric(PCPDynamicColumns* columns, PCPDynamicCol
if (!column->super.name[0])
return false;

size_t bytes = 16 + strlen(column->super.name);
char* metricName = xMalloc(bytes);
xSnprintf(metricName, bytes, "htop.column.%s", column->super.name);
char* metricName = NULL;
xAsprintf(&metricName, "htop.column.%s", column->super.name);

column->metricName = metricName;
column->id = columns->offset + columns->cursor;
Expand Down
5 changes: 2 additions & 3 deletions pcp/PCPDynamicMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ in the source distribution for its full text.


static PCPDynamicMetric* PCPDynamicMeter_lookupMetric(PCPDynamicMeters* meters, PCPDynamicMeter* meter, const char* name) {
size_t bytes = 16 + strlen(meter->super.name) + strlen(name);
char* metricName = xMalloc(bytes);
xSnprintf(metricName, bytes, "htop.meter.%s.%s", meter->super.name, name);
char* metricName = NULL;
xAsprintf(&metricName, "htop.meter.%s.%s", meter->super.name, name);

PCPDynamicMetric* metric;
for (size_t i = 0; i < meter->totalMetrics; i++) {
Expand Down
10 changes: 4 additions & 6 deletions pcp/PCPDynamicScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return NULL;

bytes += 16; /* prefix, dots and terminator */
char* metricName = xMalloc(bytes);
xSnprintf(metricName, bytes, "htop.screen.%s.%s", screen->super.name, name);

char* metricName = NULL;
xAsprintf(&metricName, "htop.screen.%s.%s", screen->super.name, name);
kingmidas-hack marked this conversation as resolved.
Show resolved Hide resolved

for (size_t i = 0; i < screen->totalColumns; i++) {
column = screen->columns[i];
Expand Down