aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2013-06-25 09:52:06 +0200
committerDaniel Stenberg <daniel@haxx.se>2013-06-25 09:55:49 +0200
commitcb1aa8b0e32068ec4bbbc42d41267420b46a36e7 (patch)
tree932c53ecd4bd45976c38e6665e018f7ae3900e67
parentd3d5c4a40e962b37ebee5e01884748e024b66e7b (diff)
printf: make sure %x are treated unsigned
When using %x, the number must be treated as unsigned as otherwise it would get sign-extended on for example 64bit machines and do wrong output. This problem showed when doing printf("%08x", 0xffeeddcc) on a 64bit host.
-rw-r--r--lib/mprintf.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/lib/mprintf.c b/lib/mprintf.c
index 2ec4a7534..8f392c7f2 100644
--- a/lib/mprintf.c
+++ b/lib/mprintf.c
@@ -380,11 +380,11 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos,
break;
case 'x':
vto[i].type = FORMAT_INT;
- flags |= FLAGS_HEX;
+ flags |= FLAGS_HEX|FLAGS_UNSIGNED;
break;
case 'X':
vto[i].type = FORMAT_INT;
- flags |= FLAGS_HEX|FLAGS_UPPER;
+ flags |= FLAGS_HEX|FLAGS_UPPER|FLAGS_UNSIGNED;
break;
case 'c':
vto[i].type = FORMAT_INT;
@@ -633,23 +633,23 @@ static int dprintf_formatf(
OUTCHAR(' ');
break;
}
- if(p->flags & FLAGS_UNSIGNED) {
- /* Decimal unsigned integer. */
- base = 10;
- goto unsigned_number;
- }
if(p->flags & FLAGS_OCTAL) {
/* Octal unsigned integer. */
base = 8;
goto unsigned_number;
}
- if(p->flags & FLAGS_HEX) {
+ else if(p->flags & FLAGS_HEX) {
/* Hexadecimal unsigned integer. */
digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits;
base = 16;
goto unsigned_number;
}
+ else if(p->flags & FLAGS_UNSIGNED) {
+ /* Decimal unsigned integer. */
+ base = 10;
+ goto unsigned_number;
+ }
/* Decimal integer. */
base = 10;