From 3af4fa58f60b28cac18d6d1c075ee51054a1f6ba Mon Sep 17 00:00:00 2001 From: rofl0r Date: Tue, 20 Mar 2018 04:13:42 +0000 Subject: [PATCH] fix rare bug when read bytes < chunksize, but more follows later a very spurious bug was encountered, when the last line of input produced by busybox' ash shell was not processed sometimes. assuming the complete input is: line1 line2 line3 which is a total of 18 characters (with the 3 newlines), the first read would receive only 17 bytes, and the last newline character was retrieved in a subsequent read. our code assumed practically that any read would produce chunksize bytes, unless the end of input was encountered. in this case stale 6 bytes were still contained in the old buffer, but the memcpy happened from the chunksize boundary, not from where the previous read actually stopped. to debug this issue, the size argument in the read() call was changed to 17, to reproduce the input chunks. --- jobflow.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jobflow.c b/jobflow.c index 30abb6f..6717b40 100644 --- a/jobflow.c +++ b/jobflow.c @@ -719,7 +719,7 @@ int main(int argc, char** argv) { prog_state.lineno = 0; - size_t left = 0; + size_t left = 0, bytes_read = 0; const size_t chunksize = prog_state.bulk_bytes ? prog_state.bulk_bytes : 16*1024; char *mem = mmap(NULL, chunksize*2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); @@ -731,12 +731,13 @@ int main(int argc, char** argv) { while(1) { inbuf = buf1+chunksize-left; - memcpy(inbuf, buf2+chunksize-left, left); + memcpy(inbuf, buf2+bytes_read-left, left); ssize_t n = read(0, buf2, chunksize); if(n == -1) { perror("read"); goto out; } + bytes_read = n; left += n; in = inbuf; while(left) { -- 2.11.4.GIT