Skip to content

Commit 3af4fa5

Browse files
committed
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.
1 parent 0c921c1 commit 3af4fa5

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

jobflow.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ int main(int argc, char** argv) {
719719

720720
prog_state.lineno = 0;
721721

722-
size_t left = 0;
722+
size_t left = 0, bytes_read = 0;
723723
const size_t chunksize = prog_state.bulk_bytes ? prog_state.bulk_bytes : 16*1024;
724724

725725
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) {
731731

732732
while(1) {
733733
inbuf = buf1+chunksize-left;
734-
memcpy(inbuf, buf2+chunksize-left, left);
734+
memcpy(inbuf, buf2+bytes_read-left, left);
735735
ssize_t n = read(0, buf2, chunksize);
736736
if(n == -1) {
737737
perror("read");
738738
goto out;
739739
}
740+
bytes_read = n;
740741
left += n;
741742
in = inbuf;
742743
while(left) {

0 commit comments

Comments
 (0)