Commit 2238707a authored by Peter Pentchev's avatar Peter Pentchev

Fix the bounded snprintf() problems that I introduced.

Reported by:	Jakub Wilk <jwilk@debian.org>
parent ec25924e
......@@ -29,6 +29,7 @@
*/
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
......@@ -43,27 +44,48 @@ static __inline__ int printable (int c)
#define BUF_SIZE 89
static size_t snprintf_bounded(char * const buf, const size_t len,
const char * const fmt, ...)
{
va_list v;
va_start(v, fmt);
const int res = vsnprintf(buf, len, fmt, v);
va_end(v);
if (res < 0)
{
if (len > 0)
buf[0] = '\0';
return 0;
}
else if ((size_t)res > len)
return len;
else
return res;
}
static void dump16 (char *buf,const uint8_t *s,uintptr_t offset,size_t len)
{
size_t i,n;
n = snprintf (buf, BUF_SIZE, "%08lx - ",(unsigned long)offset);
n = snprintf_bounded (buf, BUF_SIZE, "%08lx - ",(unsigned long)offset);
for (i = 0; i < len; i++)
n += snprintf(buf + n, BUF_SIZE - n, "%s%02x ",
n += snprintf_bounded(buf + n, BUF_SIZE - n, "%s%02x ",
i && !(i & 3)? " ": "", s[i]);
for ( ; i < 16; i++)
n += snprintf(buf + n, BUF_SIZE - n, "%s ",
n += snprintf_bounded(buf + n, BUF_SIZE - n, "%s ",
i && !(i & 3)? " ": "");
n += snprintf(buf + n, BUF_SIZE - n, " ");
n += snprintf_bounded(buf + n, BUF_SIZE - n, " ");
for (i = 0; i < len; i++)
n += snprintf(buf + n, BUF_SIZE - n, "%c",
n += snprintf_bounded(buf + n, BUF_SIZE - n, "%c",
printable (s[i]) ? s[i] : '.');
n += snprintf(buf + n, BUF_SIZE - n, "\n");
n += snprintf_bounded(buf + n, BUF_SIZE - n, "\n");
}
void hexdump_stub (const char *filename,int line,const char *function,int level,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment