Fix malloc_vsnprintf handling of %o, %u and %x

These flags take unsigned values, but they were fed with signed values
taken with va_arg, and that led to sign extension in cases where the
corresponding value has the most significant bit set.
This commit is contained in:
Mike Hommey 2012-04-18 18:29:44 +02:00 committed by Jason Evans
parent 666c5bf7a8
commit 1ad56385ad

View File

@ -320,12 +320,21 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
case '?': \ case '?': \
val = va_arg(ap, int); \ val = va_arg(ap, int); \
break; \ break; \
case '?' | 0x80: \
val = va_arg(ap, unsigned int); \
break; \
case 'l': \ case 'l': \
val = va_arg(ap, long); \ val = va_arg(ap, long); \
break; \ break; \
case 'l' | 0x80: \
val = va_arg(ap, unsigned long); \
break; \
case 'q': \ case 'q': \
val = va_arg(ap, long long); \ val = va_arg(ap, long long); \
break; \ break; \
case 'q' | 0x80: \
val = va_arg(ap, unsigned long long); \
break; \
case 'j': \ case 'j': \
val = va_arg(ap, intmax_t); \ val = va_arg(ap, intmax_t); \
break; \ break; \
@ -335,6 +344,9 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
case 'z': \ case 'z': \
val = va_arg(ap, ssize_t); \ val = va_arg(ap, ssize_t); \
break; \ break; \
case 'z' | 0x80: \
val = va_arg(ap, size_t); \
break; \
case 'p': /* Synthetic; used for %p. */ \ case 'p': /* Synthetic; used for %p. */ \
val = va_arg(ap, uintptr_t); \ val = va_arg(ap, uintptr_t); \
break; \ break; \
@ -358,7 +370,7 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
bool plus_plus = false; bool plus_plus = false;
int prec = -1; int prec = -1;
int width = -1; int width = -1;
char len = '?'; unsigned char len = '?';
f++; f++;
if (*f == '%') { if (*f == '%') {
@ -480,7 +492,7 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
uintmax_t val JEMALLOC_CC_SILENCE_INIT(0); uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
char buf[O2S_BUFSIZE]; char buf[O2S_BUFSIZE];
GET_ARG_NUMERIC(val, len); GET_ARG_NUMERIC(val, len | 0x80);
s = o2s(val, alt_form, buf, &slen); s = o2s(val, alt_form, buf, &slen);
APPEND_PADDED_S(s, slen, width, left_justify); APPEND_PADDED_S(s, slen, width, left_justify);
f++; f++;
@ -489,7 +501,7 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
uintmax_t val JEMALLOC_CC_SILENCE_INIT(0); uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
char buf[U2S_BUFSIZE]; char buf[U2S_BUFSIZE];
GET_ARG_NUMERIC(val, len); GET_ARG_NUMERIC(val, len | 0x80);
s = u2s(val, 10, false, buf, &slen); s = u2s(val, 10, false, buf, &slen);
APPEND_PADDED_S(s, slen, width, left_justify); APPEND_PADDED_S(s, slen, width, left_justify);
f++; f++;
@ -498,7 +510,7 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
uintmax_t val JEMALLOC_CC_SILENCE_INIT(0); uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
char buf[X2S_BUFSIZE]; char buf[X2S_BUFSIZE];
GET_ARG_NUMERIC(val, len); GET_ARG_NUMERIC(val, len | 0x80);
s = x2s(val, alt_form, *f == 'X', buf, &slen); s = x2s(val, alt_form, *f == 'X', buf, &slen);
APPEND_PADDED_S(s, slen, width, left_justify); APPEND_PADDED_S(s, slen, width, left_justify);
f++; f++;