Currently, if a skip value is larger than the file size, cmp will continue
to loop and call getc() until the skip count is satisfied, even though
EOF has been reached.

For large skip values (e.g., 1 billion), this results in significant
CPU waste and execution time (e.g., 11 seconds on a modern CPU).

This patch checks for EOF inside the skip loop and breaks early.

Test case:
$ touch empty.txt
$ time ./busybox cmp empty.txt empty.txt 1000000000 1000000000

Before:
real    0m10.937s

After:
real    0m0.002s

Signed-off-by: Giorgi Tchankvetadze <[email protected]>
---
 editors/cmp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/editors/cmp.c b/editors/cmp.c
index ea86a2736..89539f8cf 100644
--- a/editors/cmp.c
+++ b/editors/cmp.c
@@ -113,8 +113,8 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
                fmt = fmt_differ;
 
        if (ENABLE_DESKTOP) {
-               while (skip1) { getc(fp1); skip1--; }
-               while (skip2) { getc(fp2); skip2--; }
+               while (skip1) { if (getc(fp1) == EOF) break; skip1--; }
+               while (skip2) { if (getc(fp2) == EOF) break; skip2--; }
        }
        do {
                if (max_count >= 0 && --max_count < 0)
-- 
2.47.3

_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox

Reply via email to