When using 'cmp -n NUM' without '-l', the program would incorrectly continue comparing after finding the first difference, printing multiple 'differ' lines instead of just one.
The bug was in line 144: 'if (opt)' checked if ANY option was set (including -n), causing it to continue. It should only continue for -l (verbose) mode: 'if (opt & CMP_OPT_l)'. This was correct when originally written (only -l and -s existed), but became a bug when -n was added later. Test case: echo -n AAAAA > file1 echo -n BBBBB > file2 Before: cmp -n 3 file1 file2 -> 3 lines of output After: cmp -n 3 file1 file2 -> 1 line of output Both: cmp -l -n 3 file1 file2 -> 3 lines (correct) v2: Removed stale comment as suggested by Harald van Dijk. Signed-off-by: Giorgi Tchankvetadze <[email protected]> --- editors/cmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editors/cmp.c b/editors/cmp.c index 54f347508..ea86a2736 100644 --- a/editors/cmp.c +++ b/editors/cmp.c @@ -146,7 +146,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) line_pos = c1; /* line_pos is unused in the -l case. */ } fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2); - if (opt) { /* This must be -l since not -s. */ + if (opt & CMP_OPT_l) { /* If we encountered an EOF, * the while check will catch it. */ continue; -- 2.47.3 _______________________________________________ busybox mailing list [email protected] https://lists.busybox.net/mailman/listinfo/busybox
