mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: Re: [musl] Bug in atoll strtoll, the output of then differ
Date: Sun, 18 Dec 2022 12:10:08 +0100	[thread overview]
Message-ID: <20221218111008.GB2551@voyager> (raw)
In-Reply-To: <1499b0ba-f828-6922-a620-24971fb64c8b@gmail.com>

On Sun, Dec 18, 2022 at 11:22:59AM +0100, Domingo Alvarez Duarte wrote:
> Here is the "glibc" implementation of "atoll":
>
> =====
>
> /* Convert a string to a long long int.  */
> long long int
> atoll (const char *nptr)
> {
>   return strtoll (nptr, (char **) NULL, 10);
> }
>
> =====
>
> With that there is no way for get different results from "atolll" and
> "strtoll".
>
> Cheers !
>

That's precisely why I would like for programmers to learn some Pascal
at some point in their lives, so they start to understand that interface
and implementation are two separate things.

Yes, that is a nice implementation up there. The interface for atoll
however comes from the C standard, and it says:

| 7.24.1 Numeric conversion functions
| 1 The functions atof, atoi, atol, and atoll need not affect the value of the integer expression errno
| on an error. If the value of the result cannot be represented, the behavior is undefined.
| [...]
| The atoi, atol, and atoll functions convert the initial portion of the string pointed to by nptr to
| int, long int, and long long int representation, respectively. Except for the behavior on error,
| they are equivalent to
| atoi: (int)strtol(nptr, nullptr, 10)
| atol: strtol(nptr, nullptr, 10)
| atoll: strtoll(nptr, nullptr, 10)

Entering 2^63 into atoll() (when long long int is a 64 bit type) is an
error that invokes undefined behavior.  Explicitly undefined behavior.
Some implementations choose to deal with this by implementing the
protections from strtoll() (for example by calling strtoll()). Some
implementations don't. This is an implementation detail. The application
cannot know what will happen, and should not make assumptions about what
will happen. It should only call the ato* functions on known valid
input. If it does not know that the input is valid, it should not call
ato*.

Ciao,
Markus

  reply	other threads:[~2022-12-18 11:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-18  9:32 Domingo Alvarez Duarte
2022-12-18  9:58 ` Markus Wichmann
2022-12-18 10:22   ` Domingo Alvarez Duarte
2022-12-18 11:10     ` Markus Wichmann [this message]
2022-12-18 10:06 ` Quentin Rameau
2022-12-18 12:23 ` Szabolcs Nagy
2022-12-18 15:25 ` 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=20221218111008.GB2551@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).