LibC: Fix FILE::flush() passing bogus arguments to lseek()

This was a regression from the 64-bit off_t changes.

When dropping buffered data after a flush, we would subtract the
buffered amount from zero to get the seek offset. This didn't work
right since the subtraction was done with a 32-bit size_t and we
ended up with e.g (i64)0xfffffffc as the offset.

Fixes #6003.
This commit is contained in:
Andreas Kling 2021-04-03 22:51:05 +02:00
parent 64d4921f35
commit bf1ad16078
Notes: sideshowbarker 2024-07-18 20:51:26 +09:00

View File

@ -175,12 +175,12 @@ bool FILE::flush()
}
if (m_mode & O_RDONLY) {
// When open for reading, just drop the buffered data.
size_t had_buffered = m_buffer.buffered_size();
VERIFY(m_buffer.buffered_size() <= NumericLimits<off_t>::max());
off_t had_buffered = m_buffer.buffered_size();
m_buffer.drop();
// Attempt to reset the underlying file position to what the user
// expects.
int rc = lseek(m_fd, -had_buffered, SEEK_CUR);
if (rc < 0) {
if (lseek(m_fd, -had_buffered, SEEK_CUR) < 0) {
if (errno == ESPIPE) {
// We can't set offset on this file; oh well, the user will just
// have to cope.