From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: Re: fwrite() - possible division by zero
Date: Wed, 14 Feb 2018 20:39:42 +0100 [thread overview]
Message-ID: <20180214193942.nar6nvuulv4rg5nt@voyager> (raw)
In-Reply-To: <CACepeQ8pdKRB5UW-CuVXVjhsk4av-V+zQdFwpU1+9fT_LCK2dg@mail.gmail.com>
On Wed, Feb 14, 2018 at 04:24:16PM -0200, Geraldo Netto wrote:
> Dear Friends,
>
> I was playing with musl and I think I may have found an issue on fwrite():
>
> This is the original code:
>
> size_t fwrite(const void *restrict src, size_t size, size_t nmemb,
> FILE *restrict f)
> {
> size_t k, l = size*nmemb;
> if (!size) nmemb = 0;
> FLOCK(f);
> k = __fwritex(src, l, f);
> FUNLOCK(f);
> return k==l ? nmemb : k/size;
> }
>
>
> It seems we need to check the variable size on return because if size is zero
> We'll have a division by zero and a segmentation fault
>
If size is zero, then l is zero. So __fwritex will be called with l as
zero. Which means, if you read that code, that it will have to return
zero. So in the end, k will be zero as well, so k==l, so nmemb will be
returned (which was set to zero earlier), and more importantly, no
division takes place.
> I'm sending the attached patch that changes the return as follows:
>
> return k==l ? nmemb : (size != 0) ? k/size : k;
>
>
Also style: Usual style for musl is to write comparisons with zero as
boolean operations, and to use as few parentheses as possible, i.e.
return k==l ? nmemb : size ? k/size : k;
HTH,
Markus
next prev parent reply other threads:[~2018-02-14 19:39 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-14 18:24 Geraldo Netto
2018-02-14 19:39 ` Markus Wichmann [this message]
2018-02-14 19:48 ` Andrew Bell
2018-02-14 20:07 ` Markus Wichmann
2018-02-14 20:11 ` Andrew Bell
2018-02-14 21:15 ` Szabolcs Nagy
2018-02-14 21:47 ` Andrew Bell
2018-02-15 4:20 ` Rich Felker
2018-02-16 2:58 ` Geraldo Netto
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=20180214193942.nar6nvuulv4rg5nt@voyager \
--to=nullplan@gmx.net \
--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).