From ec466f65463e6bfbdf508b50cc2a60593a41b80c Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Thu, 13 Jun 2019 17:08:05 +0000 Subject: [PATCH] lib: remove snprintf from prefix2str Signed-off-by: Quentin Young --- lib/prefix.c | 26 +++++++++++++++++++++----- tests/subdir.am | 6 ++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/prefix.c b/lib/prefix.c index 134d9cf908..7abeebcd09 100644 --- a/lib/prefix.c +++ b/lib/prefix.c @@ -1329,13 +1329,29 @@ const char *prefix2str(union prefixconstptr pu, char *str, int size) { const struct prefix *p = pu.p; char buf[PREFIX2STR_BUFFER]; + int byte, tmp, a, b; + bool z = false; + size_t l; switch (p->family) { case AF_INET: case AF_INET6: - snprintf(str, size, "%s/%d", inet_ntop(p->family, &p->u.prefix, - buf, PREFIX2STR_BUFFER), - p->prefixlen); + inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf)); + l = strlen(buf); + buf[l++] = '/'; + byte = p->prefixlen; + if ((tmp = p->prefixlen - 100) >= 0) { + buf[l++] = '1'; + z = true; + byte = tmp; + } + b = byte % 10; + a = byte / 10; + if (a || z) + buf[l++] = '0' + a; + buf[l++] = '0' + b; + buf[l] = '\0'; + strlcpy(str, buf, size); break; case AF_ETHERNET: @@ -1349,11 +1365,11 @@ const char *prefix2str(union prefixconstptr pu, char *str, int size) break; case AF_FLOWSPEC: - sprintf(str, "FS prefix"); + strlcpy(str, "FS prefix", size); break; default: - sprintf(str, "UNK prefix"); + strlcpy(str, "UNK prefix", size); break; } diff --git a/tests/subdir.am b/tests/subdir.am index 41f1a4873b..270c0811b4 100644 --- a/tests/subdir.am +++ b/tests/subdir.am @@ -55,6 +55,7 @@ check_PROGRAMS = \ tests/lib/test_memory \ tests/lib/test_nexthop_iter \ tests/lib/test_ntop \ + tests/lib/test_prefix2str \ tests/lib/test_printfrr \ tests/lib/test_privs \ tests/lib/test_ringbuf \ @@ -236,6 +237,10 @@ tests_lib_test_ntop_CFLAGS = $(TESTS_CFLAGS) tests_lib_test_ntop_CPPFLAGS = $(TESTS_CPPFLAGS) tests_lib_test_ntop_LDADD = # none tests_lib_test_ntop_SOURCES = tests/lib/test_ntop.c tests/helpers/c/prng.c +tests_lib_test_prefix2str_CFLAGS = $(TESTS_CFLAGS) +tests_lib_test_prefix2str_CPPFLAGS = $(TESTS_CPPFLAGS) +tests_lib_test_prefix2str_LDADD = $(ALL_TESTS_LDADD) +tests_lib_test_prefix2str_SOURCES = tests/lib/test_prefix2str.c tests/helpers/c/prng.c tests_lib_test_printfrr_CFLAGS = $(TESTS_CFLAGS) tests_lib_test_printfrr_CPPFLAGS = $(TESTS_CPPFLAGS) tests_lib_test_printfrr_LDADD = $(ALL_TESTS_LDADD) @@ -328,6 +333,7 @@ EXTRA_DIST += \ tests/lib/test_atomlist.py \ tests/lib/test_nexthop_iter.py \ tests/lib/test_ntop.py \ + tests/lib/test_prefix2str.py \ tests/lib/test_printfrr.py \ tests/lib/test_ringbuf.py \ tests/lib/test_srcdest_table.py \