mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: fread, fwrite with size 0
Date: Wed, 10 Feb 2016 19:42:34 -0500	[thread overview]
Message-ID: <20160211004234.GH9349@brightrain.aerifal.cx> (raw)
In-Reply-To: <20160204043948.GM9349@brightrain.aerifal.cx>

On Wed, Feb 03, 2016 at 11:39:48PM -0500, Rich Felker wrote:
> On Wed, Feb 03, 2016 at 09:53:21AM +0100, hombre wrote:
> > Hello,
> > 
> > I think that fread and fwrite does not return 0 when parameter size
> > is 0 (when nmemb is 0, it does).
> > Clib spec says: If size or nmemb is zero, fread/fwrite returns zero
> > 
> > I did the following changes to make it work:
> > 
> > fread:
> > from
> >     return nmemb;
> > to
> >     return len == 0 ? 0 : nmemb;
> > 
> > fwrite:
> > from
> >     return k == l ? nmemb : k / size;
> > to
> >     if (l == 0)
> >         return 0;
> >     else
> >         return k == l ? nmemb : k / size;
> > 
> > Regards,
> > Erwin
> 
> Thanks. Sadly the POSIX version of the text is contradictory on this:
> 
> "Upon successful completion, fread() shall return the number of
> elements successfully read which is less than nitems only if a read
> error or end-of-file is encountered. If size or nitems is 0, fread()
> shall return 0 and the contents of the array and the state of the
> stream remain unchanged."
> 
> First it says that the return value can be less than nitems only if an
> error of EOF happens, but then it gives another way the return value
> can be less than nitems (if size is 0).
> 
> Fortunately the ISO C text is not so broken and agrees with you:
> 
> "The fread function returns the number of elements successfully read,
> which may be less than nmemb if a read error or end-of-file is
> encountered. If size or nmemb is zero, fread returns zero and the
> contents of the array and the state of the stream remain unchanged."
> 
> There's another issue I want to fix here too, but I might do it in two
> commits. Thanks for the report.

I'm fixing this by adding:

	if (!size) nmemb = 0;

to the top of both functions. This is preferable because, at least for
fread, I'm also going to have to check for nmemb>=SIZE_MAX/size in
order to properly handle this case (see glibc bug 19165), and having
already ruled out size==0 (i.e. only doing the overflow check in the
'else' path for the above 'if') makes that division safe.

Rich


  reply	other threads:[~2016-02-11  0:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-03  8:53 hombre
2016-02-04  4:39 ` Rich Felker
2016-02-11  0:42   ` Rich Felker [this message]
2016-02-12 15:06     ` 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=20160211004234.GH9349@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).