arun11299 / cpp-subprocess

Subprocessing with modern C++
Other
456 stars 90 forks source link

Reading from a pipe is messed up #14

Closed matthijskooijman closed 6 years ago

matthijskooijman commented 6 years ago

I tried reading output from stderr from a process, and it gets all messed up. It seems the length of the buffer returned is correct, but only the first couple of hundred bytes are filled, the rest are zeroes.

Looking at the code, it seems that read_atmost_n does not handle partial reads correctly. Changing this line to the following seems to fix this.

int read_bytes = read(fd, buf + rbytes, read_upto - rbytes)

On a related note, I suspect that if read_all resizes the buffer and causes a realloc, then the buffer variable might become invalid.

arun11299 commented 6 years ago

Thanks for reporting the bug @matthijskooijman Would you be able to create a pull request which addresses both the issues ? I think both are valid bugs.

If you are unable to do it, I will of course patch it.

matthijskooijman commented 6 years ago

Sorry, I really can't spare the time right now.

arun11299 commented 6 years ago

No problem. I have pushed the fix.

Thanks!

matthijskooijman commented 6 years ago

Looks good, thanks.