Skip to content

Seeking past the EOF leads to creating new content #91

Description

@mikrosk

lseek() specification is quite clear:

The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.

The lseek() function shall not, by itself, extend the size of a file.
[...]
Although lseek() may position the file offset beyond the end of the file, this function does not itself extend the size of the file.

Despite the explicit prohibition of writing to the file, mintlib does exactly that:

mintlib/unix/lseek.c

Lines 54 to 70 in 58620c0

/* otherwise extend file -- zero filling the hole */
if (new_pos < 0) /* error? */
{
new_pos = Fseek (0L, handle, SEEK_END); /* go to eof */
}
memset(buf, 0, sizeof(buf));
while (expected_pos > new_pos)
{
offset = expected_pos - new_pos;
if (offset > 256)
offset = 256;
if((current_pos = write(handle, buf, offset)) != offset)
return((current_pos > 0) ? (new_pos + current_pos) :
-1L); /* errno set by write */
new_pos += offset;
}

And on top of that, it does it even if the file is read-only. This has pretty catastrophic consequences if some code just accidentally seeks beyond the file size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions