offset1 = offset - pos;
if (!s->must_flush && (!s->direct || !s->seek) &&
- offset1 >= 0 && offset1 < buffer_size) {
+ offset1 >= 0 && offset1 <= buffer_size - s->write_flag) {
/* can do the seek inside the buffer */
s->buf_ptr = s->buffer + offset1;
} else if ((!s->seekable ||