Skip to content

Commit

Permalink
Merge branch 'replace-xmalloc-with-xasprintf' of kingmidas-hack/htop
Browse files Browse the repository at this point in the history
  • Loading branch information
natoscott authored and BenBE committed Jan 23, 2025
2 parents 53f7e63 + 842a050 commit f4bd894
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
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
8 changes: 3 additions & 5 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 */
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);

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

0 comments on commit f4bd894

Please sign in to comment.