Bug 22140 - ftell() returns incorrect value for memory stream
Summary: ftell() returns incorrect value for memory stream
Status: RESOLVED DUPLICATE of bug 20005
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: 2.27
: P2 normal
Target Milestone: ---
Assignee: Adhemerval Zanella
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-15 10:34 UTC by Bruce Adams
Modified: 2017-09-22 12:39 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bruce Adams 2017-09-15 10:34:51 UTC
See https://stackoverflow.com/questions/46229457/correct-semantics-for-ftell-when-used-on-a-memory-stream

Given the following program:

#include <stdio.h>
#include <stdlib.h>
#include <gnu/libc-version.h>

int main(void)
{
   puts (gnu_get_libc_version ());

   size_t n_buffer = 1024;
   char *buffer = calloc(n_buffer, sizeof(char));
   FILE *file = fmemopen(buffer, n_buffer, "w");

   /* "ABCD" */
   static const char magic_number[] = 
   {
     0x41, 0x42, 0x43, 0x44 
   };

   const size_t written = fwrite(magic_number, 1, 4, file);
   fprintf(stderr,"written=%d\n",written);

   int fstatus = fflush(file);
   fprintf(stderr,"fstatus=%d\n",fstatus);

   int ftellpos = ftell(file);
   fprintf(stderr,"ftellpos=%d\n",ftellpos);

   fstatus = fseek(file, 0, SEEK_END);
   fprintf(stderr,"fstatus=%d\n",fstatus);

   ftellpos = ftell(file);
   fprintf(stderr,"ftellpos2=%d\n",ftellpos);

   return 0;
}

The output on RHEL7 is:

2.17
written=4
fstatus=0
ftellpos=4
fstatus=0
ftellpos2=4

Whereas the output on OpenSUSE Leap 42 is:

2.22
written=4
fstatus=0
ftellpos=0
fstatus=0
ftellpos2=4

There are significant changes in fmemopen.c and fileop.c in those two versions that probably explain the difference.

I believe ftell() ought to work as it does in 2.17 on RHEL7 and that the version  in 2.22 on OpenSUSE has a bug.
However, it could be that the bug is in the documentation which does not make the relevant semantics for memory streams clear.
Comment 1 Bruce Adams 2017-09-15 11:51:06 UTC
This might relate to:

https://sourceware.org/bugzilla/show_bug.cgi?id=21037

&

https://sourceware.org/bugzilla/show_bug.cgi?id=21735

which are about flushing memory streams
Comment 2 Adhemerval Zanella 2017-09-22 12:38:53 UTC
I can't really tell which kind of backports OpenSUSE LEAP 22 carries on its glibc package, but my understanding is this is in fact BZ#20005 (which has been fixed on 2.24). I can't reproduce it on master:

$ /home/azanella/Projects/glibc/build/x86_64-linux-gnu/testrun.sh ./test
2.26.90
written=4
fstatus=0
ftellpos=4
fstatus=0
ftellpos2=4

I would like to ask you for future bug report if you could use the canonical source tree [1] instead of reporting against distro releases.  It is mostly because distro usually carry both backports and non-upstream patches that make difficult to track down which version it really occurs.

Also, avoid reporting bug against old releases and check against master to see it has been already fixed upstream. This helps us pinpoint the commit for possible backport.

[1] https://www.gnu.org/software/libc/sources.html

*** This bug has been marked as a duplicate of bug 20005 ***