fmemopen and fseek(fp, 0L, SEEK_END)

Pietro Cerutti gahr@FreeBSD.org
Tue Jun 3 15:00:00 GMT 2014


Hello,

as discussed on FreeNode/#glibc, I'm raising concerns as to how fmemopen
handles a fseek to offset 0 from SEEK_END.  The issue was already
discussed in [1]. I'll summarize.

    #include <stdio.h>
     
    int main()
    {
        char buf[] = "Hello";
        buf[3] = '\0';
     
        FILE * f = fmemopen((void *)buf, sizeof(buf), "r");
        fseek(f, 0, SEEK_END);
        printf("%ld\n", ftell(f));
        fclose(f);
     
        return 0;
    }

This prints 3 with glibc, but should print 6 according to POSIX [2],
quoting: 

"The stream shall also maintain the size of the current buffer contents;
use of fseek() or fseeko() on the stream with SEEK_END shall seek
relative to this size. For modes r and r+ the size shall be set to the
value given by the size argument."

I suggest to modify glibc's fmemopen implementation *not to* behave
differently depending on whether the stream was opened in binary mode
when fseek'ing wrt SEEK_END.

Comments?

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=6544
[2] http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html

-- 
Pietro Cerutti
The FreeBSD Project
gahr@FreeBSD.org

PGP Public Key:
http://gahr.ch/pgp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 964 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20140603/f0d98b67/attachment.sig>


More information about the Libc-alpha mailing list