]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: remove snprintf from prefix2str
authorQuentin Young <qlyoung@cumulusnetworks.com>
Thu, 13 Jun 2019 17:08:05 +0000 (17:08 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Thu, 13 Jun 2019 17:08:05 +0000 (17:08 +0000)
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/prefix.c
tests/subdir.am

index 134d9cf908df3cc65286be87249b91cd9e0042f5..7abeebcd0961419297ae717c62b76fcf2e31c246 100644 (file)
@@ -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;
        }
 
index 41f1a4873bb4a16cddf6ee5927376af6f81ce867..270c0811b49cb169de79f3310c9f387ccb532d5c 100644 (file)
@@ -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 \