mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pascal Cuoq <pascal.cuoq@gmail.com>
To: musl@lists.openwall.com
Subject: Re: Undefined behavior in atoi()
Date: Sun, 6 Nov 2011 23:28:41 +0100	[thread overview]
Message-ID: <CAOH62Ji7utffebihNd+UCgVurF+O2fSPSR3oeQJ3GRsS3Kf4zw@mail.gmail.com> (raw)
In-Reply-To: <20111106212145.GO132@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 1193 bytes --]

On Sun, Nov 6, 2011 at 10:21 PM, Rich Felker <dalias@aerifal.cx> wrote:

>
> Thanks. How did you manage to find this bug? Just browsing source?
>

I was looking at implementations for strtod() (long story for another time)
and I noticed that this library was the most pleasant C code I had
seen in months (seriously), so I lingered a bit. So, the straightforward
answer is "yes, code review".

The slightly longer answer is that I have done the formal verification of
"string to number" C functions in the past, found that they were correct,
noticed the opposite trick, and somehow the relationship
between the two clicked. So I was looking for a classic pitfall here.

Since my previous e-mail, I have been analyzing a few more cases for fun,
and musl's original code also has this issue for the 40 or so values
below LONG_MAX.

The problem is then:

src/stdlib/atol.orig.c:14:[kernel] warning: Signed overflow.
                  assert (long)((long)10*n)+(long)*tmp_0 ≤
9223372036854775807LL;

That is, you should in any case subtract '0' from the digit before adding
it in
at line 14. In the case of atoll() on IA32, it will even be more efficient.

Pascal

[-- Attachment #2: Type: text/html, Size: 1772 bytes --]

  reply	other threads:[~2011-11-06 22:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-06 14:24 Pascal Cuoq
2011-11-06 21:21 ` Rich Felker
2011-11-06 22:28   ` Pascal Cuoq [this message]
2011-11-08  3:44     ` Rich Felker
2011-11-08  5:43       ` Pascal Cuoq
2011-11-08 14:12         ` 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=CAOH62Ji7utffebihNd+UCgVurF+O2fSPSR3oeQJ3GRsS3Kf4zw@mail.gmail.com \
    --to=pascal.cuoq@gmail.com \
    --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).