mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Bug: ftell() after fopen(..., "ab") returns 0 unless an explicit fseek() is used first
@ 2022-08-11 11:14 Buchholz, Robert
  2022-08-11 11:36 ` Florian Weimer
  0 siblings, 1 reply; 3+ messages in thread
From: Buchholz, Robert @ 2022-08-11 11:14 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 1090 bytes --]

I'm running Alpine Linux with MUSL as its standard library within a Docker container based on the official openjdk:16-jdk-alpine3.13 Alpine image.

I've used the following minimal test case:
========
#include <stdio.h>

int main()
{
  FILE* f = fopen("foo", "wb");
  fwrite("42", 3, 1, f);
  fclose(f);

  f = fopen("foo", "ab");
  printf("%d ", (int)ftell(f));
  fwrite("42", 3, 1, f);
  fseek(f, 0, SEEK_END);
  printf("%d\n", (int)ftell(f));
}
=======
On my Alpine/MUSL setup this prints "0 6" while on Ubuntu 22.04 with glibc it prints "3 6" - which I'm assuming is the expected output. So, with MUSL the second fwrite() correctly appends to the existing file, but ftell() nevertheless reports that the initial position within the file after opening it for appending is 0 and not 3.

My GCC version is "gcc (Alpine 10.2.1_pre1) 10.2.1 20201203", ldd confirms that the compiled output is linked only against "/lib/ld-musl-x86_64.so.1" and not against glibc.

I'm not subscribed to the MUSL mailing list, please CC me on replies.

Best Regards,
Robert Buchholz

[-- Attachment #2: Type: text/html, Size: 4090 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] Bug: ftell() after fopen(..., "ab") returns 0 unless an explicit fseek() is used first
  2022-08-11 11:14 [musl] Bug: ftell() after fopen(..., "ab") returns 0 unless an explicit fseek() is used first Buchholz, Robert
@ 2022-08-11 11:36 ` Florian Weimer
  2022-08-18  0:31   ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Weimer @ 2022-08-11 11:36 UTC (permalink / raw)
  To: Buchholz, Robert; +Cc: musl

* Robert Buchholz:

> On my Alpine/MUSL setup this prints "0 6" while on Ubuntu 22.04 with
> glibc it prints "3 6" - which I'm assuming is the expected output.

Both behaviors are acceptable.  C11 (7.21.3p1) says this:

| If a file can support positioning requests (such as a disk file, as
| opposed to a terminal), then a file position indicator associated with
| the stream is positioned at the start (character number zero) of the
| file, unless the file is opened with append mode in which case it is
| implementation-defined whether the file position indicator is initially
| positioned at the beginning or the end of the file.

musl is likely not the only libc with this type of behavior, given that
it's explicitly allowed by the standard.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] Bug: ftell() after fopen(..., "ab") returns 0 unless an explicit fseek() is used first
  2022-08-11 11:36 ` Florian Weimer
@ 2022-08-18  0:31   ` Rich Felker
  0 siblings, 0 replies; 3+ messages in thread
From: Rich Felker @ 2022-08-18  0:31 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Buchholz, Robert, musl

On Thu, Aug 11, 2022 at 01:36:33PM +0200, Florian Weimer wrote:
> * Robert Buchholz:
> 
> > On my Alpine/MUSL setup this prints "0 6" while on Ubuntu 22.04 with
> > glibc it prints "3 6" - which I'm assuming is the expected output.
> 
> Both behaviors are acceptable.  C11 (7.21.3p1) says this:
> 
> | If a file can support positioning requests (such as a disk file, as
> | opposed to a terminal), then a file position indicator associated with
> | the stream is positioned at the start (character number zero) of the
> | file, unless the file is opened with append mode in which case it is
> | implementation-defined whether the file position indicator is initially
> | positioned at the beginning or the end of the file.
> 
> musl is likely not the only libc with this type of behavior, given that
> it's explicitly allowed by the standard.

Indeed. We simply match the underlying fd behavior here because it's
the "default" behavior without incurring extra cost. Since
applications can't assume one behavior or the other, ones that care to
have the position start at the end have to do the fseeko to SEEK_END
themselves.

Rich

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-18  0:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-11 11:14 [musl] Bug: ftell() after fopen(..., "ab") returns 0 unless an explicit fseek() is used first Buchholz, Robert
2022-08-11 11:36 ` Florian Weimer
2022-08-18  0:31   ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).