From 30adfcd3d8bbbee57fd3968e31ac55d795b683ca Mon Sep 17 00:00:00 2001 From: Sean Hefty Date: Tue, 29 May 2012 14:15:57 -0700 Subject: [PATCH] rstream: Use snprintf in place of sprintf Avoid possible buffer overrun. Signed-off-by: Sean Hefty --- examples/rstream.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/examples/rstream.c b/examples/rstream.c index df36e340..83f9404f 100644 --- a/examples/rstream.c +++ b/examples/rstream.c @@ -82,7 +82,7 @@ static int iterations = 1; static int transfer_size = 1000; static int transfer_count = 1000; static int buffer_size; -static char test_name[9] = "custom"; +static char test_name[10] = "custom"; static char *port = "7471"; static char *dst_addr; static char *src_addr; @@ -105,7 +105,7 @@ static void *buf; #define rs_getsockopt(s,l,n,v,ol) \ use_rs ? rgetsockopt(s,l,n,v,ol) : getsockopt(s,l,n,v,ol) -static void size_str(char *str, long long size) +static void size_str(char *str, size_t ssize, long long size) { long long base, fraction = 0; char mag; @@ -127,22 +127,22 @@ static void size_str(char *str, long long size) if (size / base < 10) fraction = (size % base) * 10 / base; if (fraction) { - sprintf(str, "%lld.%lld%c", size / base, fraction, mag); + snprintf(str, ssize, "%lld.%lld%c", size / base, fraction, mag); } else { - sprintf(str, "%lld%c", size / base, mag); + snprintf(str, ssize, "%lld%c", size / base, mag); } } -static void cnt_str(char *str, long long cnt) +static void cnt_str(char *str, size_t ssize, long long cnt) { if (cnt >= 1000000000) - sprintf(str, "%lldb", cnt / 1000000000); + sprintf(str, ssize, "%lldb", cnt / 1000000000); else if (cnt >= 1000000) - sprintf(str, "%lldm", cnt / 1000000); + sprintf(str, ssize, "%lldm", cnt / 1000000); else if (cnt >= 1000) - sprintf(str, "%lldk", cnt / 1000); + sprintf(str, ssize, "%lldk", cnt / 1000); else - sprintf(str, "%lld", cnt); + sprintf(str, ssize, "%lld", cnt); } static void show_perf(void) @@ -156,13 +156,13 @@ static void show_perf(void) /* name size transfers iterations bytes seconds Gb/sec usec/xfer */ printf("%-10s", test_name); - size_str(str, transfer_size); + size_str(str, sizeof str, transfer_size); printf("%-8s", str); - cnt_str(str, transfer_count); + cnt_str(str, sizeof str, transfer_count); printf("%-8s", str); - cnt_str(str, iterations); + cnt_str(str, sizeof str, iterations); printf("%-8s", str); - size_str(str, bytes); + size_str(str, sizeof str, bytes); printf("%-8s", str); printf("%8.2fs%10.2f%11.2f\n", usec / 1000000., (bytes * 8) / (1000. * usec), @@ -185,8 +185,10 @@ static int size_to_count(int size) static void init_latency_test(int size) { - size_str(test_name, size); - sprintf(test_name, "%s_lat", test_name); + char sstr[5]; + + size_str(sstr, sizeof sstr, size); + snprintf(test_name, sizeof test_name, "%s_lat", sstr); transfer_count = 1; transfer_size = size; iterations = size_to_count(transfer_size); @@ -194,8 +196,10 @@ static void init_latency_test(int size) static void init_bandwidth_test(int size) { - size_str(test_name, size); - sprintf(test_name, "%s_bw", test_name); + char sstr[5]; + + size_str(sstr, sizeof sstr, size); + snprintf(test_name, sizeof test_name, "%s_bw", sstr); iterations = 1; transfer_size = size; transfer_count = size_to_count(transfer_size); -- 2.45.2