mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Domingo Alvarez Duarte <mingodad@gmail.com>
To: musl@lists.openwall.com
Subject: Re: [musl] Bug in atoll strtoll, the output of then differ
Date: Sun, 18 Dec 2022 11:22:59 +0100	[thread overview]
Message-ID: <1499b0ba-f828-6922-a620-24971fb64c8b@gmail.com> (raw)
In-Reply-To: <20221218095852.GA2551@voyager>

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 !

On 18/12/22 10:58, Markus Wichmann wrote:
> On Sun, Dec 18, 2022 at 10:32:10AM +0100, Domingo Alvarez Duarte wrote:
>> Hello !
>>
>> Doing some work with emscripten with this project
>> https://github.com/mingodad/CG-SQL-Lua-playground I was getting some errors
>> with the usage of "atoll" and with this small program to compare the output
>> of "musl" and "glibc" I found what seems to be a bug in "atoll" because with
>> "musl" it gives a different output than "strtoll".
>>
>> =====
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main(int argc, char *argv[])
>> {
>>      const char *s = "9223372036854775808";
>>      long  long ll = atoll(s);
>>      long long ll2 = strtoll (s, (char **) NULL, 10);
>>      int imax = 0x7fffffff;
>>      printf("%s : %lld : %lld : %d : %d\n",  s, ll, ll2, imax, ll <= imax);
>>      return 0;
>> }
>>
>> =====
>>
>> Output from "glibc":
>>
>> =====
>>
>> 9223372036854775808 : 9223372036854775807 : 9223372036854775807 : 2147483647
>> : 0
>>
>> =====
>>
>> Output from "musl":
>>
>> =====
>>
>> 9223372036854775808 : -9223372036854775808 : 9223372036854775807 :
>> 2147483647 : 1
>>
>> =====
>>
>> Cheers !
>>
> Well, your problem here is that ato* behavior on error is not defined.
> The C standard explicitly excepts behavior on error from the requirement
> that these functions return the same thing as their strto* counterparts,
> and §7.24.1 (of C23) explicitly states that behavior in that case is
> undefined.
>
> This means that a test case is wrong; no result is defined. Actually, a
> crash would be acceptable behavior. This also means that when I return
> to work next year, I should really go through my code base and replace
> all ato* calls with their strto* counterparts for that reason alone.
>
> Ciao,
> Markus

  reply	other threads:[~2022-12-18 10:23 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 [this message]
2022-12-18 11:10     ` Markus Wichmann
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=1499b0ba-f828-6922-a620-24971fb64c8b@gmail.com \
    --to=mingodad@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).