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/22140] New: ftell() returns incorrect value for memory stream


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

            Bug ID: 22140
           Summary: ftell() returns incorrect value for memory stream
           Product: glibc
           Version: 2.27
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: stdio
          Assignee: unassigned at sourceware dot org
          Reporter: tortoise_74 at yahoo dot co.uk
  Target Milestone: ---

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.

-- 
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]