This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug stdio/13151] fmemopen streams fail to read eof


http://sourceware.org/bugzilla/show_bug.cgi?id=13151

--- Comment #3 from Michael Kerrisk <mtk.manpages at gmail dot com> 2012-04-28 02:20:55 UTC ---
I tested a little more. See the code below. 

The problem seems to occur only when the specified size of the buffer does not
include a null byte. If the specified size does include a null byte, then the
file position is correctly set to the first null byte. The standard does
clearly cover both cases:

[[
The stream maintains a current position in the buffer. This position is
initially set to either the beginning of the buffer (for r and w modes) or to
the first null byte in the buffer (for a modes). If no null byte is found in
append mode, the initial position is set to one byte after the end of the
buffer.
]]

Thus, glibc's fmemopen() should be setting the file position to the byte after
the end of the supplied string. Instead, it sets the file position to -1.

Using the test program below, here's what happens when the specified size
includes the null byte:

$ ./a.out 15
Initial ftell(): 12

And here's what happens if the specified size doesn't include the null byte:

$ ./a.out  10
Initial ftell(): -1



/* fmemopen_a+_file_pos_bug.c
*/
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define errExit(msg)     do { perror(msg); exit(EXIT_FAILURE); \
            } while (0)

#define BSIZE 20
int
main(int argc, char **argv)
{
    char buf[BSIZE] = {"hello, world"};
    FILE *fp;

    if (argc < 2) {
    fprintf(stderr, "Usage: %s <nchars> [fseek-pos]\n", argv[0]);
    exit(EXIT_FAILURE);
    }

    fp = fmemopen(buf, atoi(argv[1]), "a+");
    if (fp == NULL)
    errExit("fmemopen");

    printf("Initial ftell(): %ld\n", ftell(fp));

    if (argc > 2) {
    printf("Resetting file position\n");
        fseek(fp, atoi(argv[2]), SEEK_SET);
        printf("New ftell(): %ld\n", ftell(fp));
    }
}

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]