osmo_hexdump: Fix segfault when input is too long.
authorHolger Hans Peter Freyther <zecke@selfish.org>
Fri, 15 Jul 2011 14:07:23 +0000 (16:07 +0200)
committerHolger Hans Peter Freyther <zecke@selfish.org>
Fri, 15 Jul 2011 14:07:23 +0000 (16:07 +0200)
In snprinftf the size is a size_t (unsigned) in case we want
to write more than we have available, len_remain will be < 0.

This was spotted while removing hexdump from simtrace and comparing
it to our implementation.

int snprintf(char *str, size_t size, const char *format, ...);

src/utils.c

index 3ee14ab..e1d4c89 100644 (file)
@@ -86,6 +86,8 @@ static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
        hexd_buff[0] = 0;
        for (i = 0; i < len; i++) {
                int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
+               if (len_remain <= 0)
+                       break;
                int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim);
                if (rc <= 0)
                        break;