diff options
author | Denis Kirjanov <kirjanov@gmail.com> | 2024-02-14 07:56:59 -0500 |
---|---|---|
committer | David Ahern <dsahern@kernel.org> | 2024-02-18 17:44:36 +0000 |
commit | d2f1c3c9a8a38493cdec9fb93534ccec76c48fe2 (patch) | |
tree | de6cd59150a3d5ac961350f564277421c9a5461d | |
parent | 7e646c80d7573bc4791b268a8dee472f11c33ff1 (diff) | |
download | iproute2-next-d2f1c3c9a8a38493cdec9fb93534ccec76c48fe2.tar.gz |
ifstat: convert sprintf to snprintf
Use snprintf to print only valid data
v2: adjust formatting
v3: fix the issue with a buffer length
Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
Signed-off-by: David Ahern <dsahern@kernel.org>
-rw-r--r-- | misc/ifstat.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/misc/ifstat.c b/misc/ifstat.c index 767cedd4a..18171a2ca 100644 --- a/misc/ifstat.c +++ b/misc/ifstat.c @@ -379,10 +379,10 @@ static void format_rate(FILE *fp, const unsigned long long *vals, fprintf(fp, "%8llu ", vals[i]); if (rates[i] > mega) { - sprintf(temp, "%uM", (unsigned int)(rates[i]/mega)); + snprintf(temp, sizeof(temp), "%uM", (unsigned int)(rates[i]/mega)); fprintf(fp, "%-6s ", temp); } else if (rates[i] > kilo) { - sprintf(temp, "%uK", (unsigned int)(rates[i]/kilo)); + snprintf(temp, sizeof(temp), "%uK", (unsigned int)(rates[i]/kilo)); fprintf(fp, "%-6s ", temp); } else fprintf(fp, "%-6u ", (unsigned int)rates[i]); @@ -400,10 +400,10 @@ static void format_pair(FILE *fp, const unsigned long long *vals, int i, int k) fprintf(fp, "%8llu ", vals[i]); if (vals[k] > giga) { - sprintf(temp, "%uM", (unsigned int)(vals[k]/mega)); + snprintf(temp, sizeof(temp), "%uM", (unsigned int)(vals[k]/mega)); fprintf(fp, "%-6s ", temp); } else if (vals[k] > mega) { - sprintf(temp, "%uK", (unsigned int)(vals[k]/kilo)); + snprintf(temp, sizeof(temp), "%uK", (unsigned int)(vals[k]/kilo)); fprintf(fp, "%-6s ", temp); } else fprintf(fp, "%-6u ", (unsigned int)vals[k]); @@ -675,7 +675,7 @@ static void server_loop(int fd) p.fd = fd; p.events = p.revents = POLLIN; - sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d", + snprintf(info_source, sizeof(info_source), "%d.%lu sampling_interval=%d time_const=%d", getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000); load_info(); @@ -893,7 +893,7 @@ int main(int argc, char *argv[]) sun.sun_family = AF_UNIX; sun.sun_path[0] = 0; - sprintf(sun.sun_path+1, "ifstat%d", getuid()); + snprintf(sun.sun_path + 1, sizeof(sun.sun_path) - 1, "ifstat%d", getuid()); if (scan_interval > 0) { if (time_constant == 0) |