Branch: Tag:

2017-07-30

2017-07-30 09:22:26 by Arne Goedeke <el@laramies.com>

Stdio.Buffer: fix unread()

The documentation promises that unread() will always succeed until new
data is added to the buffer. This contract was broken when the new
growth strategy was added by commit 2c4241823. That commit added a
possible trim to every read operation, which would try to shrink the
buffer and make a future unread() fail.

Also, the max_waste check contained a typo, which made it always
succeed.

This issue does not affect rewind keys.

132:    static void io_trim( Buffer *io )    ATTRIBUTE((noinline));    +  static void io_trim_waste( Buffer *io ) +  { +  if( UNLIKELY(io->allocated > (io_len(io) * (1.0+io->max_waste))) ) +  io_trim(io); +  } +     static INT_TYPE io_consume( Buffer *io, ptrdiff_t num )    {    io->offset += num; -  if( UNLIKELY(io->allocated > (io_len(io) * io->max_waste)) ) -  io_trim(io); +     return io_len(io);    }   
2076:    Buffer *io = THIS;    io->max_waste = howmuch;    io_add_space( io, 0, 1 ); -  io_consume( io, 0 ); +  io_trim_waste( io );    }       /*! @decl void trim()