Update even more snprintfs

Use size of actual buffers instead of magic numbers
This commit is contained in:
Christian Göttsche 2020-11-28 19:33:07 +01:00
parent 7899ae2eb1
commit ea4f33409a
6 changed files with 14 additions and 14 deletions

View File

@ -294,7 +294,7 @@ static MaskItem* AffinityPanel_addObject(AffinityPanel* this, hwloc_obj_t obj, u
//left -= len; //left -= len;
} }
xSnprintf(buf, 64, "%s %s%u", type_name, index_prefix, index); xSnprintf(buf, sizeof(buf), "%s %s%u", type_name, index_prefix, index);
MaskItem* item = MaskItem_newMask(buf, indent_buf, obj->complete_cpuset, false); MaskItem* item = MaskItem_newMask(buf, indent_buf, obj->complete_cpuset, false);
if (parent) if (parent)

View File

@ -119,7 +119,7 @@ AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Header* heade
Panel_add(super, (Object*) ListItem_new("CPU average", 0)); Panel_add(super, (Object*) ListItem_new("CPU average", 0));
for (int i = 1; i <= cpus; i++) { for (int i = 1; i <= cpus; i++) {
char buffer[50]; char buffer[50];
xSnprintf(buffer, 50, "%s %d", type->uiName, Settings_cpuId(this->settings, i - 1)); xSnprintf(buffer, sizeof(buffer), "%s %d", type->uiName, Settings_cpuId(this->settings, i - 1));
Panel_add(super, (Object*) ListItem_new(buffer, i)); Panel_add(super, (Object*) ListItem_new(buffer, i));
} }
} else { } else {

View File

@ -69,11 +69,11 @@ void Header_writeBackToSettings(const Header* this) {
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
Meter* meter = (Meter*) Vector_get(vec, i); Meter* meter = (Meter*) Vector_get(vec, i);
char* name = xCalloc(64, sizeof(char)); char* name;
if (meter->param) { if (meter->param) {
xSnprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param); xAsprintf(&name, "%s(%d)", As_Meter(meter)->name, meter->param);
} else { } else {
xSnprintf(name, 63, "%s", As_Meter(meter)->name); xAsprintf(&name, "%s", As_Meter(meter)->name);
} }
colSettings->names[i] = name; colSettings->names[i] = name;
colSettings->modes[i] = meter->mode; colSettings->modes[i] = meter->mode;

12
Meter.c
View File

@ -134,20 +134,20 @@ void Meter_setMode(Meter* this, int modeIndex) {
} }
ListItem* Meter_toListItem(Meter* this, bool moving) { ListItem* Meter_toListItem(Meter* this, bool moving) {
char mode[21]; char mode[20];
if (this->mode) { if (this->mode) {
xSnprintf(mode, 20, " [%s]", Meter_modes[this->mode]->uiName); xSnprintf(mode, sizeof(mode), " [%s]", Meter_modes[this->mode]->uiName);
} else { } else {
mode[0] = '\0'; mode[0] = '\0';
} }
char number[11]; char number[10];
if (this->param > 0) { if (this->param > 0) {
xSnprintf(number, 10, " %d", this->param); xSnprintf(number, sizeof(number), " %d", this->param);
} else { } else {
number[0] = '\0'; number[0] = '\0';
} }
char buffer[51]; char buffer[50];
xSnprintf(buffer, 50, "%s%s%s", Meter_uiName(this), number, mode); xSnprintf(buffer, sizeof(buffer), "%s%s%s", Meter_uiName(this), number, mode);
ListItem* li = ListItem_new(buffer, 0); ListItem* li = ListItem_new(buffer, 0);
li->moving = moving; li->moving = moving;
return li; return li;

View File

@ -35,7 +35,7 @@ Panel* SignalsPanel_new() {
static char buf[16]; static char buf[16];
for (int sig = SIGRTMIN; sig <= SIGRTMAX; i++, sig++) { for (int sig = SIGRTMIN; sig <= SIGRTMAX; i++, sig++) {
int n = sig - SIGRTMIN; int n = sig - SIGRTMIN;
xSnprintf(buf, 16, "%2d SIGRTMIN%-+3d", sig, n); xSnprintf(buf, sizeof(buf), "%2d SIGRTMIN%-+3d", sig, n);
if (n == 0) { if (n == 0) {
buf[11] = '\0'; buf[11] = '\0';
} }

View File

@ -476,8 +476,8 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
void Platform_getPressureStall(const char* file, bool some, double* ten, double* sixty, double* threehundred) { void Platform_getPressureStall(const char* file, bool some, double* ten, double* sixty, double* threehundred) {
*ten = *sixty = *threehundred = 0; *ten = *sixty = *threehundred = 0;
char procname[128 + 1]; char procname[128];
xSnprintf(procname, 128, PROCDIR "/pressure/%s", file); xSnprintf(procname, sizeof(procname), PROCDIR "/pressure/%s", file);
FILE* fd = fopen(procname, "r"); FILE* fd = fopen(procname, "r");
if (!fd) { if (!fd) {
*ten = *sixty = *threehundred = NAN; *ten = *sixty = *threehundred = NAN;