public inbox for glibc-bugs@sourceware.org help / color / mirror / Atom feed
From: "mtk.manpages at gmail dot com" <sourceware-bugzilla@sourceware.org> To: glibc-bugs@sources.redhat.com Subject: [Bug stdio/13151] fmemopen streams fail to read eof Date: Fri, 27 Apr 2012 20:16:00 -0000 [thread overview] Message-ID: <bug-13151-131-Vf2MjROYRc@http.sourceware.org/bugzilla/> (raw) In-Reply-To: <bug-13151-131@http.sourceware.org/bugzilla/> http://sourceware.org/bugzilla/show_bug.cgi?id=13151 Michael Kerrisk <mtk.manpages at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mtk.manpages at gmail dot | |com --- Comment #1 from Michael Kerrisk <mtk.manpages at gmail dot com> 2012-04-27 20:15:20 UTC --- I think your example isn't quite right, but there is a real problem here. See the fopen(3) man page: a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. So, "a+" should set the file pointer to byte 0. It looks like the problem is that the file position is not correctly reset to 0 when mode is "a+". Without a command-line argument, the program below does an fmemopen(), and then tries reading characters, and we see the problem: $ ./a.out Initial ftell(): -1 Segmentation fault (core dumped) With a command-line argument, the program does an initial fseek() to explicitly place the file pointer at byte 0, and then all is well: ./a.out x Initial ftell(): -1 Resetting file position New ftell(): 0 0: h 104; feof()=0; ftell() = 1 1: e 101; feof()=0; ftell() = 2 2: l 108; feof()=0; ftell() = 3 3: l 108; feof()=0; ftell() = 4 4: o 111; feof()=0; ftell() = 5 5: � -1; feof()=1; ftell() = 5 So, it seems that the fix would be that when fmemopen() mode is "a+", the glibc implementation should set the file position to 0. ========= #define _XOPEN_SOURCE 700 #include <stdio.h> #include <string.h> #include <assert.h> #include <stdlib.h> #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0) int main(int argc, char **argv) { FILE *fp = fmemopen((char[20]){"hello, world"}, 5, "a+"); char c; int j; printf("Initial ftell(): %ld\n", ftell(fp)); if (argc > 1) { printf("Resetting file position\n"); fseek(fp, 0, SEEK_SET); printf("New ftell(): %ld\n", ftell(fp)); } for (j = 0; j < 20 && !feof(fp); j++) { c = fgetc(fp); printf("%2d: %c %d; feof()=%d; ftell() = %ld\n", j, c, c, feof(fp), 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.
next prev parent reply other threads:[~2012-04-27 20:16 UTC|newest] Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top 2011-09-05 2:24 [Bug libc/13151] New: " bugdal at aerifal dot cx 2012-02-21 2:30 ` [Bug stdio/13151] " jsm28 at gcc dot gnu.org 2012-04-27 20:16 ` mtk.manpages at gmail dot com [this message] 2012-04-27 20:31 ` mtk.manpages at gmail dot com 2012-04-28 2:21 ` mtk.manpages at gmail dot com 2012-04-28 3:28 ` mtk.manpages at gmail dot com 2012-04-28 4:26 ` mtk.manpages at gmail dot com 2012-12-19 10:39 ` schwab@linux-m68k.org 2013-05-20 15:24 ` ondra at iuuk dot mff.cuni.cz 2014-02-16 18:29 ` jackie.rosen at hushmail dot com 2014-05-28 19:40 ` schwab at sourceware dot org 2014-06-27 12:10 ` fweimer at redhat dot com 2015-07-08 15:16 ` adhemerval.zanella at linaro dot org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-13151-131-Vf2MjROYRc@http.sourceware.org/bugzilla/ \ --to=sourceware-bugzilla@sourceware.org \ --cc=glibc-bugs@sources.redhat.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).