Skip to contentSkip to conversation

lib: utils: introduce scnprintf

PATCH In review Denis Kirjanov 3 participants 4 messages Feb 9, 2024 – Feb 10, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lib: utils: introduce scnprintf

The function is similar to the standard snprintf but returns the number of characters actually written to @buf argument excluding the trailing '\0'

---
 include/utils.h |  1 +
 lib/utils.c     | 14 ++++++++++++++
 2 files changed, 15 insertions(+)
include/utils.h +1 -0
index 9ba129b8..5b65edc5 100644
@@ -393,4 +393,5 @@ int proto_a2n(unsigned short *id, const char *buf,
 const char *proto_n2a(unsigned short id, char *buf, int len,
 		      const struct proto *proto_tb, size_t tb_len);
 
+int scnprintf(char * buf, size_t size, const char * fmt, ...);
 #endif /* __UTILS_H__ */
lib/utils.c +14 -0
index 6c1c1a8d..752da66f 100644
@@ -7,6 +7,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <math.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -2003,3 +2004,16 @@ int proto_a2n(unsigned short *id, const char *buf,
 
 	return 0;
 }
+
+int scnprintf(char * buf, size_t size, const char * fmt, ...)
+{
+       ssize_t ssize = size;
+       va_list args;
+       int i;
+
+       va_start(args, fmt);
+       i = vsnprintf(buf, size, fmt, args);
+       va_end(args);
+
+       return (i >= ssize) ? (ssize - 1) : i;
+}
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 ifstat: convert string formatting calls to scnprintf

Use scnprintf to print only valid data

---
 misc/ifstat.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
misc/ifstat.c +9 -9
index 721f4914..288e9064 100644
@@ -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));
+		scnprintf(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));
+		scnprintf(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));
+		scnprintf(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));
+		scnprintf(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",
+	scnprintf(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());
+	scnprintf(sun.sun_path + 1, sizeof(sun.sun_path) - 1, "ifstat%d", getuid());
 
 	if (scan_interval > 0) {
 		if (time_constant == 0)
@@ -926,14 +926,14 @@ int main(int argc, char *argv[])
 	npatterns = argc;
 
 	if (getenv("IFSTAT_HISTORY"))
-		snprintf(hist_name, sizeof(hist_name),
+		scnprintf(hist_name, sizeof(hist_name),
 			 "%s", getenv("IFSTAT_HISTORY"));
 	else
 		if (!stats_type)
-			snprintf(hist_name, sizeof(hist_name),
+			scnprintf(hist_name, sizeof(hist_name),
 				 "%s/.ifstat.u%d", P_tmpdir, getuid());
 		else
-			snprintf(hist_name, sizeof(hist_name),
+			scnprintf(hist_name, sizeof(hist_name),
 				 "%s/.%s_ifstat.u%d", P_tmpdir, stats_type,
 				 getuid());
 
Signature · 1 line
2.30.2
#3 lore raw
Quoted 7 lines · Stephen Hemminger
Quoted 4 lines · On Fri, 9 Feb 2024 04:36:18 -0500 Denis Kirjanov <[email protected]> wrote:

The function is similar to the standard snprintf but returns the number of characters actually written to @buf argument excluding the trailing '\0'

Quoted 5 lines
Quoted 2 lines

---

I don't understand, why not use snprintf in ifstat? None of the cases in patch 2 care about the return value length.

Hi Stephen,

My intention is just use one safe version of the string formatting function. I'm going to convert other places as well.