mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: bug in fwrite/__towrite
Date: Fri, 5 Feb 2016 10:40:21 -0500	[thread overview]
Message-ID: <20160205154021.GW9349@brightrain.aerifal.cx> (raw)
In-Reply-To: <56B4C0AA.3020705@gmx.at>

On Fri, Feb 05, 2016 at 04:32:58PM +0100, hombre wrote:
> Hello,
> 
> I think there is a bug in fwrite/__towrite.
> 
> This is my unittest that fails:
> static void test_write_read2(const char *fname)
> {
>     char wbuf[3];
>     char c;
> 
>     FILE *file = fopen(fname, "wb");
>     assert(file != NULL);
>     wbuf[0] = 'a';
>     wbuf[1] = 'b';
>     wbuf[2] = 'c';
>     size_t written = fwrite(wbuf, 1, 3, file);
>     assert(written == 3);
>     fclose(file);
> 
>     file = fopen(fname, "rb+");
>     size_t nread = fread(&c, 1, 1, file);
>     assert(nread == 1);
>     assert(c == 'a');
>     c = 'B';
>     written = fwrite(&c, 1, 1, file);

This line caused undefined behavior. You cannot switch between writing
and reading on a stream without an intervening successful fseek or
fflush (the latter only in the write->read direction). See 7.21.5.3
The fopen function, paragraph 7:

"When a file is opened with update mode ('+' as the second or third
character in the above list of mode argument values), both input and
output may be performed on the associated stream. However, output
shall not be directly followed by input without an intervening call to
the fflush function or to a file positioning function (fseek, fsetpos,
or rewind), and input shall not be directly followed by output without
an intervening call to a file positioning function, unless the input
operation encounters end- of-file. Opening (or creating) a text file
with update mode may instead open (or create) a binary stream in some
implementations."

>     assert(written == 1);
>     nread = fread(&c, 1, 1, file);

And again.

>     assert(nread == 1); /* <================== nread is 0 here ! */
>     assert(c == 'c');
>     fclose(file);
> }

Rich


      reply	other threads:[~2016-02-05 15:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-05 15:32 hombre
2016-02-05 15:40 ` Rich Felker [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=20160205154021.GW9349@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.
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).