public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Nick Stoughton <nstoughton@logitech.com>, libc-help@sourceware.org
Subject: Re: fmemopen question
Date: Mon, 16 Oct 2023 18:34:53 -0300	[thread overview]
Message-ID: <0126decc-6168-43b6-bb89-179998ce5b18@linaro.org> (raw)
In-Reply-To: <CACpbN916YAqDMVhszqNLO_D_STeg65NPLU3OWBn_xnXdd7w+9Q@mail.gmail.com>



On 12/10/23 11:57, Nick Stoughton via Libc-help wrote:
> If I open a file for writing and immediately close it:
> 
>   fclose(fopen("foo", "w"));
> 
> the file "foo" will be truncated to zero length if it exists.
> 
> However, I I do this with a memory buffer, e.g.
>     char arr[] = { 'a','a','a','a','a','a','a','a','a','a', 0};
>     fclose(fmemopen(arr, 10, "w"));
> I would expect that arr[0] would have a null byte.
> 
> But it doesn't. Is this a bug?
> 

I think this falls to the Austin defect 657 [1], where the original fmemopen
specification [2]:

  When a stream open for writing is flushed or closed, a null byte shall be 
  written at the current position or at the end of the buffer, depending on 
  the size of the contents.

Indeed does not fully define when to write off the null byte.  The proposed
changed now allow glibc behavior (which only updates the stream on write
operation):

  When a stream open for update (the mode argument includes '+') or for writing 
  only is successfully written and the write advances the current buffer end 
  position, a null byte shall be written at the new buffer end position if it 
  fits.

However, the defect submitter has asked for more clarification. So there still
no resolution.  It seems that at glibc and musl do not write a null byte at
the start of the stream, different than BSD implementation (Free, Open, and
NET) as well AIX72.

[1] https://austingroupbugs.net/view.php?id=657

      reply	other threads:[~2023-10-16 21:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12 14:57 Nick Stoughton
2023-10-16 21:34 ` Adhemerval Zanella Netto [this message]

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=0126decc-6168-43b6-bb89-179998ce5b18@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=libc-help@sourceware.org \
    --cc=nstoughton@logitech.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: link
Be 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).