public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Jim Meyering <jim@meyering•net>
To: "Morten Welinder" <mwelinder@gmail•com>
Cc: "Junio C Hamano" <junkio@cox•net>, git@vger•kernel.org
Subject: Re: avoid atoi, when possible; int overflow -> heap corruption
Date: Mon, 22 May 2006 15:31:28 +0200	[thread overview]
Message-ID: <878xoufaof.fsf@rho.meyering.net> (raw)
In-Reply-To: <118833cc0605220616t75a182b1oa404d5efe8a1f5d9@mail.gmail.com> (Morten Welinder's message of "Mon, 22 May 2006 09:16:50 -0400")

"Morten Welinder" <mwelinder@gmail•com> wrote:
>> There are about 20 uses of atoi, and most calls can return
>> a usable result in spite of an invalid input -- just because
>> atoi returns the same thing for "99" as "99-and-any-suffix".
>> It would be better not to ignore invalid inputs.
>
> atoi has undefined behaviour for "99-and-any-suffix".  You might
> get lucky and get back 99, but you might also get a random value
> or a core dump.

I've never heard of that.
POSIX says that atoi(str) is equivalent to:

    (int) strtol(str, (char **)NULL, 10)
    except that the handling of errors may differ.
    If the value cannot be represented, the behavior is undefined.

Since strtol works fine with such a suffix, and since 99 can be
represented, I don't see why there would be any undefined behavior.

Do you know of an implementation for which `atoi ("99-and-any-suffix")'
does anything other than return 99?

  reply	other threads:[~2006-05-22 13:31 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-20 22:13 synchronizing incremental git changes to cvs Jim Meyering
2006-05-20 23:05 ` Johannes Schindelin
2006-05-21 13:40   ` Jim Meyering
2006-05-22 16:29     ` Pavel Roskin
2006-05-22 17:05       ` Jakub Narebski
2006-05-21  0:09 ` Martin Langhoff
2006-05-21 16:37   ` Jim Meyering
2006-05-21 18:21 ` Junio C Hamano
2006-05-21 21:31   ` Jim Meyering
2006-05-21 21:35   ` don't accept bogus N in `HEAD~N' Jim Meyering
2006-05-21 21:42     ` Jakub Narebski
2006-05-22  7:38       ` Jim Meyering
2006-05-22  8:16         ` Junio C Hamano
2006-05-22  8:25           ` Junio C Hamano
2006-05-22  6:57   ` avoid atoi, when possible; int overflow -> heap corruption Jim Meyering
2006-05-22 13:16     ` Morten Welinder
2006-05-22 13:31       ` Jim Meyering [this message]
2006-05-22 13:37       ` Jeff King
2006-05-22 13:54         ` Morten Welinder
2006-05-22 10:27   ` detect write failure, even for stdout Jim Meyering

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=878xoufaof.fsf@rho.meyering.net \
    --to=jim@meyering$(echo .)net \
    --cc=git@vger$(echo .)kernel.org \
    --cc=junkio@cox$(echo .)net \
    --cc=mwelinder@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox