* [PATCH iproute2 1/2] lib: utils: introduce scnprintf
@ 2024-02-09 9:36 Denis Kirjanov
2024-02-09 9:36 ` [PATCH iproute2 2/2] ifstat: convert string formatting calls to scnprintf Denis Kirjanov
2024-02-09 16:33 ` [PATCH iproute2 1/2] lib: utils: introduce scnprintf Stephen Hemminger
0 siblings, 2 replies; 4+ messages in thread
From: Denis Kirjanov @ 2024-02-09 9:36 UTC (permalink / raw)
To: stephen, dsahern; +Cc: netdev, Denis Kirjanov
The function is similar to the standard snprintf but
returns the number of characters actually written to @buf
argument excluding the trailing '\0'
Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
---
include/utils.h | 1 +
lib/utils.c | 14 ++++++++++++++
2 files changed, 15 insertions(+)
diff --git a/include/utils.h b/include/utils.h
index 9ba129b8..5b65edc5 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -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__ */
diff --git a/lib/utils.c b/lib/utils.c
index 6c1c1a8d..752da66f 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -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;
+}
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH iproute2 2/2] ifstat: convert string formatting calls to scnprintf
2024-02-09 9:36 [PATCH iproute2 1/2] lib: utils: introduce scnprintf Denis Kirjanov
@ 2024-02-09 9:36 ` Denis Kirjanov
2024-02-09 16:33 ` [PATCH iproute2 1/2] lib: utils: introduce scnprintf Stephen Hemminger
1 sibling, 0 replies; 4+ messages in thread
From: Denis Kirjanov @ 2024-02-09 9:36 UTC (permalink / raw)
To: stephen, dsahern; +Cc: netdev, Denis Kirjanov
Use scnprintf to print only valid data
Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
---
misc/ifstat.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/misc/ifstat.c b/misc/ifstat.c
index 721f4914..288e9064 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));
+ 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());
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH iproute2 1/2] lib: utils: introduce scnprintf
2024-02-09 9:36 [PATCH iproute2 1/2] lib: utils: introduce scnprintf Denis Kirjanov
2024-02-09 9:36 ` [PATCH iproute2 2/2] ifstat: convert string formatting calls to scnprintf Denis Kirjanov
@ 2024-02-09 16:33 ` Stephen Hemminger
2024-02-10 8:37 ` Denis Kirjanov
1 sibling, 1 reply; 4+ messages in thread
From: Stephen Hemminger @ 2024-02-09 16:33 UTC (permalink / raw)
To: Denis Kirjanov; +Cc: dsahern, netdev, Denis Kirjanov
On Fri, 9 Feb 2024 04:36:18 -0500
Denis Kirjanov <kirjanov@gmail.com> wrote:
> The function is similar to the standard snprintf but
> returns the number of characters actually written to @buf
> argument excluding the trailing '\0'
>
> Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
> ---
I don't understand, why not use snprintf in ifstat?
None of the cases in patch 2 care about the return value length.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH iproute2 1/2] lib: utils: introduce scnprintf
2024-02-09 16:33 ` [PATCH iproute2 1/2] lib: utils: introduce scnprintf Stephen Hemminger
@ 2024-02-10 8:37 ` Denis Kirjanov
0 siblings, 0 replies; 4+ messages in thread
From: Denis Kirjanov @ 2024-02-10 8:37 UTC (permalink / raw)
To: Stephen Hemminger, Denis Kirjanov; +Cc: dsahern, netdev
On 2/9/24 19:33, Stephen Hemminger wrote:
> On Fri, 9 Feb 2024 04:36:18 -0500
> Denis Kirjanov <kirjanov@gmail.com> wrote:
>
>> The function is similar to the standard snprintf but
>> returns the number of characters actually written to @buf
>> argument excluding the trailing '\0'
>>
>> Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
>> ---
>
> 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.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-02-10 8:37 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-09 9:36 [PATCH iproute2 1/2] lib: utils: introduce scnprintf Denis Kirjanov
2024-02-09 9:36 ` [PATCH iproute2 2/2] ifstat: convert string formatting calls to scnprintf Denis Kirjanov
2024-02-09 16:33 ` [PATCH iproute2 1/2] lib: utils: introduce scnprintf Stephen Hemminger
2024-02-10 8:37 ` Denis Kirjanov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).