From de61a2c044b96955e21ce54f177c9ea4eff27627 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sat, 12 Feb 2022 17:40:57 +0100 Subject: [PATCH] Use sizeof() instead of hard-coded values with snprintf() function Signed-off-by: Sebastien GODARD --- common.c | 4 ++-- mpstat.c | 12 ++++++------ pr_stats.c | 2 +- sadf_misc.c | 4 ++-- svg_stats.c | 20 ++++++++++---------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/common.c b/common.c index a322db5..4f71c93 100644 --- a/common.c +++ b/common.c @@ -1040,7 +1040,7 @@ char *get_devname_from_sysfs(unsigned int major, unsigned int minor) char *devname; ssize_t r; - snprintf(link, 256, "%s/%u:%u", SYSFS_DEV_BLOCK, major, minor); + snprintf(link, sizeof(link), "%s/%u:%u", SYSFS_DEV_BLOCK, major, minor); /* Get full path to device knowing its major and minor numbers */ r = readlink(link, target, PATH_MAX); @@ -1084,7 +1084,7 @@ char *get_devname(unsigned int major, unsigned int minor) if ((name != NULL) && strcmp(name, K_NODEV)) return (name); - snprintf(buf, 32, "dev%u-%u", major, minor); + snprintf(buf, sizeof(buf), "dev%u-%u", major, minor); return (buf); } diff --git a/mpstat.c b/mpstat.c index 0c0d409..a63eb05 100644 --- a/mpstat.c +++ b/mpstat.c @@ -795,18 +795,18 @@ void write_json_cpu_stats(int tab, unsigned long long deltot_jiffies, int prev, strcpy(cpu_name, "all"); if (DISPLAY_TOPOLOGY(flags)) { - snprintf(topology, 1024, + snprintf(topology, sizeof(topology), ", \"core\": \"\", \"socket\": \"\", \"node\": \"\""); } } else { - snprintf(cpu_name, 16, "%d", i - 1); - cpu_name[15] = '\0'; + snprintf(cpu_name, sizeof(cpu_name), "%d", i - 1); + cpu_name[sizeof(cpu_name) - 1] = '\0'; if (DISPLAY_TOPOLOGY(flags)) { cpu_topo_i = st_cpu_topology + i - 1; - snprintf(topology, 1024, + snprintf(topology, sizeof(topology), ", \"core\": \"%d\", \"socket\": \"%d\", \"node\": \"%d\"", cpu_topo_i->logical_core_id, cpu_topo_i->phys_package_id, cpu2node[i - 1]); } @@ -1069,8 +1069,8 @@ void write_json_node_stats(int tab, unsigned long long deltot_jiffies, strcpy(node_name, "all"); } else { - snprintf(node_name, 16, "%d", node - 1); - node_name[15] = '\0'; + snprintf(node_name, sizeof(node_name), "%d", node - 1); + node_name[sizeof(node_name) -1] = '\0'; /* Recalculate interval for current node */ deltot_jiffies = 0; diff --git a/pr_stats.c b/pr_stats.c index fb4426c..3121a9c 100644 --- a/pr_stats.c +++ b/pr_stats.c @@ -2695,7 +2695,7 @@ void stub_print_pwr_usb_stats(struct activity *a, int curr, int dispavg) /* bMaxPower is expressed in 2 mA units */ (unsigned long long) (suc->bmaxpower << 1)); - snprintf(fmt, 16, " %%-%ds", MAX_MANUF_LEN - 1); + snprintf(fmt, sizeof(fmt), " %%-%ds", MAX_MANUF_LEN - 1); cprintf_s(IS_STR, fmt, suc->manufacturer); cprintf_s(IS_STR, " %s\n", suc->product); diff --git a/sadf_misc.c b/sadf_misc.c index cff0fe2..2c43c41 100644 --- a/sadf_misc.c +++ b/sadf_misc.c @@ -1017,8 +1017,8 @@ __tm_funct_t print_raw_timestamp(void *parm, int action, char *cur_date, static char pre[80]; if (action & F_BEGIN) { - snprintf(pre, 80, "%s%s", cur_time, strlen(cur_date) && utc ? " UTC" : ""); - pre[79] = '\0'; + snprintf(pre, sizeof(pre), "%s%s", cur_time, strlen(cur_date) && utc ? " UTC" : ""); + pre[sizeof(pre) - 1] = '\0'; return pre; } diff --git a/svg_stats.c b/svg_stats.c index aea9bda..d548ce6 100644 --- a/svg_stats.c +++ b/svg_stats.c @@ -341,8 +341,8 @@ void lnappend(unsigned long long timetag, double value, char **out, int *outsize char data[128]; /* Prepare additional graph definition data */ - snprintf(data, 128, " %c%llu,%.2f", restart ? 'M' : 'L', timetag, value); - data[127] = '\0'; + snprintf(data, sizeof(data), " %c%llu,%.2f", restart ? 'M' : 'L', timetag, value); + data[sizeof(data) - 1] = '\0'; save_svg_data(data, out, outsize); } @@ -374,8 +374,8 @@ void lniappend(unsigned long long timetag, unsigned long long value, char **out, char data[128]; /* Prepare additional graph definition data */ - snprintf(data, 128, " %c%llu,%llu", restart ? 'M' : 'L', timetag, value); - data[127] = '\0'; + snprintf(data, sizeof(data), " %c%llu,%llu", restart ? 'M' : 'L', timetag, value); + data[sizeof(data) - 1] = '\0'; save_svg_data(data, out, outsize); } @@ -416,9 +416,9 @@ void brappend(unsigned long long timetag, double offset, double value, char **ou t = timetag -dt; } - snprintf(data, 128, "", + snprintf(data, sizeof(data), "", t, MINIMUM(offset, 100.0), MINIMUM(value, (100.0 - offset)), dt); - data[127] = '\0'; + data[sizeof(data) - 1] = '\0'; save_svg_data(data, out, outsize); @@ -581,7 +581,7 @@ void gr_autoscaling(unsigned int asfactor[], int asf_nr, int group, int g_type, if (!*(spmax + pos + j) || (*(spmax + pos + j) == gmax)) continue; - snprintf(val, 32, "%u", (unsigned int) (gmax / *(spmax + pos + j))); + snprintf(val, sizeof(val), "%u", (unsigned int) (gmax / *(spmax + pos + j))); if (strlen(val) > 0) { asfactor[j] = pwr10(strlen(val) - 1); } @@ -742,8 +742,8 @@ double ygrid(double lmax, int *dp) *dp = 2; return (lmax / SVG_H_GRIDNR); } - snprintf(val, 32, "%ld", n); - val[31] = '\0'; + snprintf(val, sizeof(val), "%ld", n); + val[sizeof(val) - 1] = '\0'; l = strlen(val); if (l < 2) return n; @@ -976,7 +976,7 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[], for (j = 0; j < group[i]; j++) { /* Set dp to TRUE (1) if current metric is based on integer values */ dp = (g_title[pos + j][0] == '~'); - snprintf(val, 32, "x%u ", asfactor[j]); + snprintf(val, sizeof(val), "x%u ", asfactor[j]); printf("" "%s %s(%.*f, %.*f)\n", xv + 5 + SVG_M_XSIZE + SVG_G_XSIZE, yv + SVG_M_YSIZE + j * 15, -- 2.40.0