1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:27:45 +00:00

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

View file

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