mailing list of musl libc
 help / color / mirror / code / Atom feed
From: hombre <hombre67@gmx.at>
To: musl@lists.openwall.com
Subject: bug in fwrite/__towrite
Date: Fri, 5 Feb 2016 16:32:58 +0100	[thread overview]
Message-ID: <56B4C0AA.3020705@gmx.at> (raw)

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);
     assert(written == 1);
     nread = fread(&c, 1, 1, file);
     assert(nread == 1); /* <================== nread is 0 here ! */
     assert(c == 'c');
     fclose(file);
}

Please note that I have not tested this with the original musl-libc in 
linux. I found this bug while I was trying to port parts of musl to a 
small embedded os. But I think the bug is not in my port. Here is what I 
think is wrong:

- fwrite calls __towrite when the write buffer is not active
     if (!f->wend && __towrite(f)) return 0;
- __towrite clears the read buffer, but the underlying filepointer is 
not adjusted. I think that the filepointer should be adjusted, when the 
read buffer is not empty.
int __towrite(FILE *f)
{
     ...
     /* Clear read buffer (easier than summoning nasal demons) */
     f->rpos = f->rend = 0;
     ...

Here is my fix:
int __towrite(FILE *f)
{
     ...
     /* Clear read buffer (easier than summoning nasal demons) */
     if (f->rpos) {
         /* Adjust underlying filepointer for unread data in buffer. */
         if (f->seek(f, -(f->rend - f->rpos), SEEK_CUR) < 0)
             return -1;
         f->rpos = f->rend = 0;
     }
     ...

Regards,
Erwin



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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-05 15:32 hombre [this message]
2016-02-05 15:40 ` Rich Felker

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=56B4C0AA.3020705@gmx.at \
    --to=hombre67@gmx.at \
    --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).