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

Avoid strlen+xMalloc+xSnprintf, use xAsprintf instead #1582

Open
BenBE opened this issue Jan 7, 2025 · 1 comment · May be fixed by #1587
Open

Avoid strlen+xMalloc+xSnprintf, use xAsprintf instead #1582

BenBE opened this issue Jan 7, 2025 · 1 comment · May be fixed by #1587
Labels
code quality ♻️ Code quality enhancement good first issue 🥇 Good for newcomers

Comments

@BenBE
Copy link
Member

BenBE commented Jan 7, 2025

There are several places that feature a length calculation, followed by an explicit xMalloc/xCalloc and a final xSnprintf into a buffer like this (example from PCPDynamicColumn_addMetric):

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

This should better be implemented using xAsprintf:

   char* metricName = NULL;
   xAsprintf(&metricName, "htop.column.%s", column->super.name);

There are several more such places, in particular in the PCP platform code that I noticed (but likely elsewhere too).

@BenBE BenBE added good first issue 🥇 Good for newcomers code quality ♻️ Code quality enhancement labels Jan 7, 2025
@heysujal
Copy link

@BenBE I would like to work on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality ♻️ Code quality enhancement good first issue 🥇 Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants