diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-07-03 16:27:54 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-07-03 16:29:06 +0200 |
commit | e5d5f5b9a770de5a48d1a3bd293d5d611d6624c4 (patch) | |
tree | 64d7021ed5058bf9647778e24fda5ab1656d686c | |
parent | 9634e8a7d545283c662992d4d3e4a1bcd557c055 (diff) | |
download | busybox-w32-e5d5f5b9a770de5a48d1a3bd293d5d611d6624c4.tar.gz busybox-w32-e5d5f5b9a770de5a48d1a3bd293d5d611d6624c4.tar.bz2 busybox-w32-e5d5f5b9a770de5a48d1a3bd293d5d611d6624c4.zip |
hexdump: fix short file of zero butes treated as dup
function old new delta
bb_dump_dump 1466 1491 +25
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/dump.c | 14 | ||||
-rwxr-xr-x | testsuite/hexdump.tests | 18 |
2 files changed, 27 insertions, 5 deletions
diff --git a/libbb/dump.c b/libbb/dump.c index 5941ef902..b4b49d709 100644 --- a/libbb/dump.c +++ b/libbb/dump.c | |||
@@ -387,7 +387,10 @@ static unsigned char *get(priv_dumper_t *dumper) | |||
387 | if (need == blocksize) { | 387 | if (need == blocksize) { |
388 | return NULL; | 388 | return NULL; |
389 | } | 389 | } |
390 | if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) { | 390 | if (dumper->pub.dump_vflag != ALL /* not "show all"? */ |
391 | && dumper->pub.dump_vflag != FIRST /* not first line? */ | ||
392 | && memcmp(dumper->get__curp, dumper->get__savp, nread) == 0 /* same data? */ | ||
393 | ) { | ||
391 | if (dumper->pub.dump_vflag != DUP) { | 394 | if (dumper->pub.dump_vflag != DUP) { |
392 | puts("*"); | 395 | puts("*"); |
393 | } | 396 | } |
@@ -399,7 +402,7 @@ static unsigned char *get(priv_dumper_t *dumper) | |||
399 | } | 402 | } |
400 | n = fread(dumper->get__curp + nread, sizeof(unsigned char), | 403 | n = fread(dumper->get__curp + nread, sizeof(unsigned char), |
401 | dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin); | 404 | dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin); |
402 | if (!n) { | 405 | if (n == 0) { |
403 | if (ferror(stdin)) { | 406 | if (ferror(stdin)) { |
404 | bb_simple_perror_msg(dumper->argv[-1]); | 407 | bb_simple_perror_msg(dumper->argv[-1]); |
405 | } | 408 | } |
@@ -411,9 +414,10 @@ static unsigned char *get(priv_dumper_t *dumper) | |||
411 | dumper->pub.dump_length -= n; | 414 | dumper->pub.dump_length -= n; |
412 | } | 415 | } |
413 | need -= n; | 416 | need -= n; |
414 | if (!need) { | 417 | if (need == 0) { |
415 | if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST | 418 | if (dumper->pub.dump_vflag == ALL /* "show all"? */ |
416 | || memcmp(dumper->get__curp, dumper->get__savp, blocksize) | 419 | || dumper->pub.dump_vflag == FIRST /* first line? */ |
420 | || memcmp(dumper->get__curp, dumper->get__savp, blocksize) != 0 /* not same data? */ | ||
417 | ) { | 421 | ) { |
418 | if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) { | 422 | if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) { |
419 | dumper->pub.dump_vflag = WAIT; | 423 | dumper->pub.dump_vflag = WAIT; |
diff --git a/testsuite/hexdump.tests b/testsuite/hexdump.tests new file mode 100755 index 000000000..45a0c1300 --- /dev/null +++ b/testsuite/hexdump.tests | |||
@@ -0,0 +1,18 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | # Copyright 2018 by Denys Vlasenko <vda.linux@googlemail.com> | ||
4 | # Licensed under GPLv2, see file LICENSE in this source tree. | ||
5 | |||
6 | . ./testing.sh | ||
7 | |||
8 | # testing "description" "command" "result" "infile" "stdin" | ||
9 | testing 'hexdump -C with four NULs' \ | ||
10 | 'hexdump -C' \ | ||
11 | "\ | ||
12 | 00000000 00 00 00 00 |....| | ||
13 | 00000004 | ||
14 | " \ | ||
15 | '' \ | ||
16 | '\0\0\0\0' | ||
17 | |||
18 | exit $FAILCOUNT | ||