osmo_hexdump: Fix segfault when input is too long.

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, ...);
This commit is contained in:
Holger Hans Peter Freyther 2011-07-15 16:07:23 +02:00
parent 96ba20cb44
commit 128d9e2343
1 changed files with 2 additions and 0 deletions

View 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;