NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: lib/48517 (snprintb(3) adds extra '>' to string-converted value when nothing matches)



>Synopsis: snprintb(3) adds extra '>' to string-converted value when nothing 
>matches
>
>State-Changed-From-To: open->closed
>State-Changed-By: shm%NetBSD.org@localhost
>State-Changed-When: Fri, 06 Jun 2014 07:16:34 +0000
>State-Changed-Why:
>the bug stops here

there is bugs yet in below case.

% cat x.c
#include <stdio.h>
#include <util.h>

int
main(int argc, char *argv[])
{
        char tmp[1024];

        /* no match any bit, and value not zero */
        snprintb(tmp, sizeof(tmp), "\20\01ONE\02TWO\03TREE", 8);
        printf("%s\n", tmp);    /* result "0x8>" */

        snprintb(tmp, sizeof(tmp), "\177\020b\x1f" "MSB\0", 1);
        printf("%s\n", tmp);    /* result "0x1>" */

        /* value is zero in new format */
        snprintb(tmp, sizeof(tmp),
                "\177\020"
                "f\x10" "\4" "BURST\0"
                "=\0" "ZERO\0"
                "=\4" "FOUR\0"
                "=\xf" "SIXTEEN\0",
                0);
        printf("%s\n", tmp);    /* result "0x0<BURST=0x0=ZERO" */

        return 0;
}

% cc -Wall x.c -lutil
% ./a.out 
0x8>
0x1>
0x0<BURST=0x0=ZERO

>Fix:
cvs -q diff -aup
Index: snprintb.c
===================================================================
RCS file: /cvsroot/src/common/lib/libutil/snprintb.c,v
retrieving revision 1.15
diff -a -u -p -r1.15 snprintb.c
--- snprintb.c  6 Jun 2014 07:08:37 -0000       1.15
+++ snprintb.c  1 Aug 2014 08:41:03 -0000
@@ -257,7 +257,7 @@ snprintb_m(char *buf, size_t buflen, con
                }
        }
        l_len++;
-       if (val != 0 && (size_t)(++t_len) < buflen)
+       if (sep != '<' && (size_t)(++t_len) < buflen)
                *bp++ = '>';
 terminate:
        *bp++ = '\0';


% cc -Wall x.c -lutil
% ./a.out 
0x8
0x1
0x0<BURST=0x0=ZERO>


--
ryo shimizu


Home | Main Index | Thread Index | Old Index