aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-05-26 14:10:38 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2023-05-26 14:10:38 +0200
commit34751d8bf921a2c07cf9a7ce9074756a3d936013 (patch)
treee6b0c16dd295ba68436aa130d2eec3366a222256
parent8fab21114122c9abf54a750523a12bf263d5b0b2 (diff)
downloadbusybox-w32-34751d8bf921a2c07cf9a7ce9074756a3d936013.tar.gz
busybox-w32-34751d8bf921a2c07cf9a7ce9074756a3d936013.tar.bz2
busybox-w32-34751d8bf921a2c07cf9a7ce9074756a3d936013.zip
libbb/dump: correct handling of 1-byte signed int format
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/dump.c37
-rwxr-xr-xtestsuite/hexdump.tests34
2 files changed, 50 insertions, 21 deletions
diff --git a/libbb/dump.c b/libbb/dump.c
index dace481c2..49340b5a5 100644
--- a/libbb/dump.c
+++ b/libbb/dump.c
@@ -619,31 +619,31 @@ static NOINLINE void display(priv_dumper_t* dumper)
619 } 619 }
620 case F_INT: { 620 case F_INT: {
621 union { 621 union {
622 uint16_t val16; 622 int16_t ival16;
623 uint32_t val32; 623 int32_t ival32;
624 uint64_t val64; 624 int64_t ival64;
625 } u; 625 } u;
626 int value = *bp; 626 int value = (signed char)*bp;
627 627
628 switch (pr->bcnt) { 628 switch (pr->bcnt) {
629 case 1: 629 case 1:
630 break; 630 break;
631 case 2: 631 case 2:
632 memcpy(&u.val16, bp, 2); 632 move_from_unaligned16(u.ival16, bp);
633 value = u.val16; 633 value = u.ival16;
634 break; 634 break;
635 case 4: 635 case 4:
636 memcpy(&u.val32, bp, 4); 636 move_from_unaligned32(u.ival32, bp);
637 value = u.val32; 637 value = u.ival32;
638 break; 638 break;
639 case 8: 639 case 8:
640 memcpy(&u.val64, bp, 8); 640 move_from_unaligned64(u.ival64, bp);
641//A hack. Users _must_ use %llX formats to not truncate high bits 641//A hack. Users _must_ use %llX formats to not truncate high bits
642 printf(pr->fmt, (long long) u.val64); 642 printf(pr->fmt, (long long)u.ival64);
643 goto skip; 643 goto skip;
644 } 644 }
645 printf(pr->fmt, value); 645 printf(pr->fmt, value);
646 IF_OD(skip:) 646 skip:
647 break; 647 break;
648 } 648 }
649 case F_P: 649 case F_P:
@@ -659,22 +659,19 @@ static NOINLINE void display(priv_dumper_t* dumper)
659 conv_u(pr, bp); 659 conv_u(pr, bp);
660 break; 660 break;
661 case F_UINT: { 661 case F_UINT: {
662 unsigned ival; 662 unsigned value = (unsigned char)*bp;
663 unsigned short sval;
664
665 switch (pr->bcnt) { 663 switch (pr->bcnt) {
666 case 1: 664 case 1:
667 printf(pr->fmt, (unsigned) *bp);
668 break; 665 break;
669 case 2: 666 case 2:
670 memcpy(&sval, bp, sizeof(sval)); 667 move_from_unaligned16(value, bp);
671 printf(pr->fmt, (unsigned) sval);
672 break; 668 break;
673 case 4: 669 case 4:
674 memcpy(&ival, bp, sizeof(ival)); 670 move_from_unaligned32(value, bp);
675 printf(pr->fmt, ival);
676 break; 671 break;
672 /* case 8: no users yet */
677 } 673 }
674 printf(pr->fmt, value);
678 break; 675 break;
679 } 676 }
680 } 677 }
@@ -686,7 +683,7 @@ static NOINLINE void display(priv_dumper_t* dumper)
686 } 683 }
687 } 684 }
688 } 685 }
689 endfu: 686 IF_OD(endfu:)
690 if (dumper->endfu) { 687 if (dumper->endfu) {
691 PR *pr; 688 PR *pr;
692 /* 689 /*
diff --git a/testsuite/hexdump.tests b/testsuite/hexdump.tests
index 084156af4..be0379cfc 100755
--- a/testsuite/hexdump.tests
+++ b/testsuite/hexdump.tests
@@ -34,7 +34,7 @@ testing "hexdump thinks last full block can match" \
34 '' \ 34 '' \
35 '\0\0\0\0\0\0\0\0\0\0\0\0' 35 '\0\0\0\0\0\0\0\0\0\0\0\0'
36 36
37testing "hexdump e %3_u" \ 37testing "hexdump -e %3_u" \
38 "hexdump -e '16/1 \" %3_u\" \"\n\"'" \ 38 "hexdump -e '16/1 \" %3_u\" \"\n\"'" \
39 "\ 39 "\
40 nul soh stx etx eot enq ack bel bs ht lf vt ff cr so si 40 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" \
50"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\ 50"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\
51"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\ 51"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\
52 52
53testing "hexdump -e /1 %d" \
54 "hexdump -e '16/1 \" %4d\" \"\n\"'" \
55 "\
56 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
57 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
58 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
59 -128 -127 -126 -125 -124 -123 -122 -121 -120 -119 -118 -117 -116 -115 -114 -113
60 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
61" \
62 "" \
63"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"\
64"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"\
65"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"\
66"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\
67"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\
68
69testing "hexdump -e /2 %d" \
70 "hexdump -e '8/2 \" %6d\" \"\n\"'" \
71 "\
72 256 770 1284 1798 2312 2826 3340 3854
73 4368 4882 5396 5910 6424 6938 7452 7966
74 29040 29554 30068 30582 31096 31610 32124 32638
75 -32384 -31870 -31356 -30842 -30328 -29814 -29300 -28786
76 -3600 -3086 -2572 -2058 -1544 -1030 -516 -2
77" \
78 "" \
79"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"\
80"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"\
81"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"\
82"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"\
83"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"\
84
53exit $FAILCOUNT 85exit $FAILCOUNT