diff options
author | Ron Yorston | 2021-08-21 09:36:27 +0100 |
---|---|---|
committer | Denys Vlasenko | 2021-08-22 00:08:18 +0200 |
commit | 4357569fdc7bc482dea0ef0bff57a70e7f06523c (patch) | |
tree | 472856f5ad9a28b1459566d39ec50bb7b84f6e36 | |
parent | 62d5a1e56f4022002c5c55e02d7d29e1e68bc236 (diff) | |
download | busybox-4357569fdc7bc482dea0ef0bff57a70e7f06523c.zip busybox-4357569fdc7bc482dea0ef0bff57a70e7f06523c.tar.gz |
rev: correct output for long input lines
The input buffer is initialised to a reasonable size and extended
if necessary. When this happened the offset into the buffer wasn't
reset to zero so subsequent lines were appended to the long line.
Fix this and add some tests.
function old new delta
rev_main 377 368 -9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-9) Total: -9 bytes
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rwxr-xr-x | testsuite/rev.tests | 46 | ||||
-rw-r--r-- | util-linux/rev.c | 1 |
2 files changed, 47 insertions, 0 deletions
diff --git a/testsuite/rev.tests b/testsuite/rev.tests new file mode 100755 index 0000000..dd65dcd --- /dev/null +++ b/testsuite/rev.tests @@ -0,0 +1,46 @@ +#!/bin/sh +# Copyright 2021 by Ron Yorston +# Licensed under GPLv2, see file LICENSE in this source tree. + +. ./testing.sh + +# testing "test name" "commands" "expected result" "file input" "stdin" + +testing "rev works" \ + "rev input" \ +"\ +1 enil + +3 enil +" \ + "line 1\n\nline 3\n" \ + "" + +testing "rev file with missing newline" \ + "rev input" \ +"\ +1 enil + +3 enil" \ + "line 1\n\nline 3" \ + "" + +testing "rev file with NUL character" \ + "rev input" \ +"\ +nil +3 enil +" \ + "lin\000e 1\n\nline 3\n" \ + "" + +testing "rev file with long line" \ + "rev input" \ +"\ ++--------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+--------------- +cba +" \ + "---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+--------------+\nabc\n" \ + "" + +exit $FAILCOUNT diff --git a/util-linux/rev.c b/util-linux/rev.c index d439b4d..63b005c 100644 --- a/util-linux/rev.c +++ b/util-linux/rev.c @@ -109,6 +109,7 @@ int rev_main(int argc UNUSED_PARAM, char **argv) strrev(buf, strlen(buf)); #endif fputs_stdout(buf); + pos = 0; } fclose(fp); } while (*argv); |