libbb/dump: correct handling of 1-byte signed int format

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2023-05-26 14:10:38 +02:00
parent 8fab211141
commit 34751d8bf9
2 changed files with 50 additions and 21 deletions

View file

@ -619,31 +619,31 @@ static NOINLINE void display(priv_dumper_t* dumper)
} }
case F_INT: { case F_INT: {
union { union {
uint16_t val16; int16_t ival16;
uint32_t val32; int32_t ival32;
uint64_t val64; int64_t ival64;
} u; } u;
int value = *bp; int value = (signed char)*bp;
switch (pr->bcnt) { switch (pr->bcnt) {
case 1: case 1:
break; break;
case 2: case 2:
memcpy(&u.val16, bp, 2); move_from_unaligned16(u.ival16, bp);
value = u.val16; value = u.ival16;
break; break;
case 4: case 4:
memcpy(&u.val32, bp, 4); move_from_unaligned32(u.ival32, bp);
value = u.val32; value = u.ival32;
break; break;
case 8: case 8:
memcpy(&u.val64, bp, 8); move_from_unaligned64(u.ival64, bp);
//A hack. Users _must_ use %llX formats to not truncate high bits //A hack. Users _must_ use %llX formats to not truncate high bits
printf(pr->fmt, (long long) u.val64); printf(pr->fmt, (long long)u.ival64);
goto skip; goto skip;
} }
printf(pr->fmt, value); printf(pr->fmt, value);
IF_OD(skip:) skip:
break; break;
} }
case F_P: case F_P:
@ -659,22 +659,19 @@ static NOINLINE void display(priv_dumper_t* dumper)
conv_u(pr, bp); conv_u(pr, bp);
break; break;
case F_UINT: { case F_UINT: {
unsigned ival; unsigned value = (unsigned char)*bp;
unsigned short sval;
switch (pr->bcnt) { switch (pr->bcnt) {
case 1: case 1:
printf(pr->fmt, (unsigned) *bp);
break; break;
case 2: case 2:
memcpy(&sval, bp, sizeof(sval)); move_from_unaligned16(value, bp);
printf(pr->fmt, (unsigned) sval);
break; break;
case 4: case 4:
memcpy(&ival, bp, sizeof(ival)); move_from_unaligned32(value, bp);
printf(pr->fmt, ival);
break; break;
/* case 8: no users yet */
} }
printf(pr->fmt, value);
break; break;
} }
} }
@ -686,7 +683,7 @@ static NOINLINE void display(priv_dumper_t* dumper)
} }
} }
} }
endfu: IF_OD(endfu:)
if (dumper->endfu) { if (dumper->endfu) {
PR *pr; PR *pr;
/* /*

View file

@ -34,7 +34,7 @@ testing "hexdump thinks last full block can match" \
'' \ '' \
'\0\0\0\0\0\0\0\0\0\0\0\0' '\0\0\0\0\0\0\0\0\0\0\0\0'
testing "hexdump e %3_u" \ testing "hexdump -e %3_u" \
"hexdump -e '16/1 \" %3_u\" \"\n\"'" \ "hexdump -e '16/1 \" %3_u\" \"\n\"'" \
"\ "\
nul soh stx etx eot enq ack bel bs ht lf vt ff cr so si nul soh stx etx eot enq ack bel bs ht lf vt ff cr so si
@ -50,4 +50,36 @@ testing "hexdump e %3_u" \
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\ "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\ "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\
testing "hexdump -e /1 %d" \
"hexdump -e '16/1 \" %4d\" \"\n\"'" \
"\
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
-128 -127 -126 -125 -124 -123 -122 -121 -120 -119 -118 -117 -116 -115 -114 -113
-16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
" \
"" \
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"\
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"\
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"\
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\
testing "hexdump -e /2 %d" \
"hexdump -e '8/2 \" %6d\" \"\n\"'" \
"\
256 770 1284 1798 2312 2826 3340 3854
4368 4882 5396 5910 6424 6938 7452 7966
29040 29554 30068 30582 31096 31610 32124 32638
-32384 -31870 -31356 -30842 -30328 -29814 -29300 -28786
-3600 -3086 -2572 -2058 -1544 -1030 -516 -2
" \
"" \
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"\
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"\
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"\
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\
exit $FAILCOUNT exit $FAILCOUNT