Closed ajm188 closed 4 years ago
Thanks for the detailed analysis.
AFAICT returning len(p) - len(q)
would also be safe. It doesn't matter much what exactly it is, since it probably hasn't been compressed anyway, but of course it shouldn't be negative.
That sounds good to me as well.
When pgzip's
Writer
is used as abufio.Writer
, calls toWrite
can result in panics like:which come from this bit of code in bufio:
This seems to be due to the section of
Write
that actually writes the compressed data to the underlying buffer, definitely at least during the first iteration, and possibly others. The issue is with this return:On the first iteration of this loop
q := p
, and length is a positive integer, so this will always return a negative number, causingbufio
to panic rather than to propagate the error back to the caller.I think a simple fix here is to return the max of 0 and that value.