| delorie.com/archives/browse.cgi | search |
| DMARC-Filter: | OpenDMARC Filter v1.4.2 delorie.com 689K70WZ2202138 |
| Authentication-Results: | delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com |
| Authentication-Results: | delorie.com; spf=pass smtp.mailfrom=cygwin.com |
| DKIM-Filter: | OpenDKIM Filter v2.11.0 delorie.com 689K70WZ2202138 |
| Authentication-Results: | delorie.com; |
| dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=oGakCBWH | |
| X-Recipient: | archive-cygwin AT delorie DOT com |
| DKIM-Filter: | OpenDKIM Filter v2.11.0 sourceware.org 575694BB590A |
| DKIM-Signature: | v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; |
| s=default; t=1788984418; | |
| bh=qrH1A096Ax3cdbSheR32ySePCJJa3pVCKbK6ONPRopU=; | |
| h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: | |
| List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: | |
| From; | |
| b=oGakCBWHYAikGXvQfYcYpp2zfU3tmeldc6RNsJFOOb8iOl3E7N2NaA9gl5hqlMnQ1 | |
| YsVJlB9jUByHMuWtvL8jFFhYQvcyNeXct41iqKUxInHxxaaiiodllY6epbeG0KsF4h | |
| JFCWjNWJub+K1axPxe0KFQH/SMIi8pIcKyr0sVvs= | |
| X-Original-To: | cygwin AT cygwin DOT com |
| Delivered-To: | cygwin AT cygwin DOT com |
| DMARC-Filter: | OpenDMARC Filter v1.4.2 sourceware.org 47FAE4BA2E04 |
| ARC-Filter: | OpenARC Filter v1.0.0 sourceware.org 47FAE4BA2E04 |
| ARC-Seal: | i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788984399; cv=none; |
| b=ToL94rOPL5W73WFP2mgl3UsuTl4EIJ7Lbzz2RGgGR7g3rT2J5zN3w4Q+egNvm88pQrLJ7wh0Jo8fpznCwPILrtO8B75j84xHjK3anu0W4q+GhBqEM+c8Um0XUZ19Tye8Y3DbsyN904G+ZLiWOABed0TNaYKE5qOrDMy2c0AiVcc= | |
| ARC-Message-Signature: | i=1; a=rsa-sha256; d=sourceware.org; s=key; |
| t=1788984399; c=relaxed/simple; | |
| bh=wxSSDjxTBg0qW6+S/vusF5CQxKA/X0tMuI9zBS0FBK4=; | |
| h=Message-ID:Date:MIME-Version:From:Subject:To:DKIM-Signature; | |
| b=EvRIl0c02NltQGOHFE/5sqUjYeQNzYlrwGCGTgYAeAEUMo/aHoGaGLVn5DTZScBTLrp6IdmaYXeJnKEXj76UoJb+gGC5tJFoJ+m8oY2ll+JTqZYN3luODmnJGOijVSM8sRXNKeMxWsf861DYePRADL1ahgubdgKEksfShmp9ulo= | |
| ARC-Authentication-Results: | i=1; sourceware.org; |
| dkim=pass (2048-bit key, unprotected) | |
| header.d=SystematicSW.ab.ca header.i=@SystematicSW.ab.ca header.a=rsa-sha256 | |
| header.s=he header.b=POYw+Bfd | |
| DKIM-Filter: | OpenDKIM Filter v2.11.0 sourceware.org 47FAE4BA2E04 |
| Message-ID: | <38210c9c-416b-4a14-9cf5-bebcbd031925@SystematicSW.ab.ca> |
| Date: | Wed, 9 Sep 2026 14:06:36 -0600 |
| MIME-Version: | 1.0 |
| User-Agent: | Mozilla Thunderbird |
| Subject: | Re: Newlib's strtol() is not standard-compliant |
| To: | newlib AT sourceware DOT org, cygwin AT cygwin DOT com |
| References: | <PH8PR09MB985214421DEECD10627186F4A5B02 AT PH8PR09MB9852 DOT namprd09 DOT prod DOT outlook DOT com> |
| Organization: | Systematic Software |
| In-Reply-To: | <PH8PR09MB985214421DEECD10627186F4A5B02@PH8PR09MB9852.namprd09.prod.outlook.com> |
| X-Stat-Signature: | 68y4m9c4pp76zmepobgresnnt8nta7mk |
| X-Rspamd-Server: | rspamout02 |
| X-Rspamd-Queue-Id: | 162BE60017 |
| X-Session-Marker: | 427269616E2E496E676C69734053797374656D6174696353572E61622E6361 |
| X-Session-ID: | U2FsdGVkX1/2h4YRwOI2ZRdWhbdANWMkxmnVDUhWzF4= |
| X-HE-Tag: | 1788984396-790914 |
| X-HE-Meta: | U2FsdGVkX18jhnH8dom5l9z2+no6iuhZTGxXoB3kffyx6krbK8a5M/rFmC8YLNGdx4R3bcgHT45k5pWYmNk9XBMyJux1bSRxk03+ARlLKe7taMUrhGr/Ysqoe2al4YsjHKqH+skW0nUQYO7pU3gVRtE7D+izlZI8ZPx0H8nBZePaX1excifKiI0sXkFuyO94HLBxJ2tZbA4BUtjtyqpdjcZWmSYWFzubR4cBbn/rrunhOpiN2hO95jz0w72mDp9Idru86Q1PXFerh2taHKOX0KYO7856tBjKxM5j7YQ8FReFrQ46YgdR6SqlQIJPplq3LL8vNjspapbAJHf+Lcb4AohlZEf/Tpatpdv1Mq7UaMFKNWawdPtHSMuxeIesA6Bg555yfZKd6U/dACRtOTJWc5ziqx7ZudV0oqirOnoX04jI3/ddnWJR5V04g7qjcP7iiE0vN81lgcbLLrvGmHYxPk9G+6KiaWYWq+zsikXr6Js+XMrQgLDsvc0/qzSQV2Xx/yuKrTMFbw6izgpgucC9gSxs1PqiaUD059RKCp9VaLeR4F2E5bKOe97gxJwiY+QONlrLSPkpZixcYcHeHvL6h9YC/22MortT |
| X-BeenThere: | cygwin AT cygwin DOT com |
| X-Mailman-Version: | 2.1.30 |
| List-Id: | General Cygwin discussions and problem reports <cygwin.cygwin.com> |
| List-Unsubscribe: | <https://cygwin.com/mailman/options/cygwin>, |
| <mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe> | |
| List-Archive: | <https://cygwin.com/pipermail/cygwin/> |
| List-Post: | <mailto:cygwin AT cygwin DOT com> |
| List-Help: | <mailto:cygwin-request AT cygwin DOT com?subject=help> |
| List-Subscribe: | <https://cygwin.com/mailman/listinfo/cygwin>, |
| <mailto:cygwin-request AT cygwin DOT com?subject=subscribe> | |
| From: | Brian Inglis via Cygwin <cygwin AT cygwin DOT com> |
| Reply-To: | newlib AT sourceware DOT org |
| Cc: | Brian Inglis <Brian DOT Inglis AT SystematicSW DOT ab DOT ca> |
| Errors-To: | cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com |
| Sender: | "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com> |
| X-MIME-Autoconverted: | from base64 to 8bit by delorie.com id 689K70WZ2202138 |
On 2026-09-09 08:54, Lavrentiev, Anton (NIH/NLM/NCBI) [C] via Newlib wrote:
> I am reposting this from Cygwin ML (as there it seems to not have drawn any attention at all):
> https://cygwin.com/pipermail/cygwin/2026-September/260018.html
>
> I'm using a fairly modern Cygwin (which in turn is using newlib as its C RTL):
> $ uname -a
> CYGWIN_NT-10.0-22631 NCBIPC9109 3.6.10-1.x86_64 2026-07-13 20:20 UTC x86_64 Cygwin
>
> and here's the problem:
> IDK if this was brought up previously, but I just found out that Cygwin's version of strtol() does not adhere to the standard behavior in this corner case.
>
> Consider the test case:
>
> $ cat strtol.c
> #include <errno.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(int argc, const char* argv[])
> {
> char* end;
> long val = strtol(argv[1], &end, 0);
> printf("val = %ld, char(s) read = %tu, errno = %s\n",
> val, end - argv[1], strerror(errno));
> return 0;
> }
>
> $ gcc -Wall strtol.c
>
> As observed, Cygwin cannot convert any string that begins with "0x" but which is not immediately followed by a proper hex digit, while in fact it should have been able to convert the leading "0" and reject the remaining characters (which start with "x"):
>
> $ ./a.exe 0x
> val = 0, char(s) read = 0, errno = No error
>
> $ ./a.exe 0y
> val = 0, char(s) read = 1, errno = No error
>
> $ ./a.exe 0xz
> val = 0, char(s) read = 0, errno = No error
>
> $ ./a.exe 0xa
> val = 10, char(s) read = 3, errno = No error
>
> $ ./a.exe 0xaz
> val = 10, char(s) read = 3, errno = No error
>
> The buggy behavior is the same if the 3rd argument "base" of strtol() is set 16 (instead of 0, as shown in code above).
> Note that it correctly converts "0y" to 0, and rejects the following "y" (and it correctly converts "0xaz" by stopping at "z").
>
> The same code on Linux (libc) yields correct results in all cases:
>
> $ ./a.out 0x
> val = 0, char(s) read = 1, errno = Success
>
> $ ./a.out 0y
> val = 0, char(s) read = 1, errno = Success
>
> $ ./a.out 0xz
> val = 0, char(s) read = 1, errno = Success
>
> $ ./a.out 0xa
> val = 10, char(s) read = 3, errno = Success
>
> $ ./a.out 0xaz
> val = 10, char(s) read = 3, errno = Success
>
> Here's a reference to the standard:
> https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html
The latest is:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/strtol.html
deferring to ISO C e.g. 203Y Working (or Committee, or Final) Draft:
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3685.pdf
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3886.pdf or later
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3685.pdf#page=401
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3886.pdf#page=403
¶7.25.2.8 The strtol, strtoll, strtoul, and strtoull functions
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3685.pdf#page=75
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3886.pdf#page=75
¶6.4.5.2 integer literal including 0[Bb][01]... if base is 0, or 2, 8,
10, 16 respectively
or later documents changing those paragraphs
where with base 0, integers may begin with a sign [-+], decimals begin with a
non-zero digit, 0[Bb] must be followed by a binary digit or base > 11, as value
is 11*BASE^N, 0[Xx] must be followed by a hex digit or base > 33, as the value
is 33*BASE^N depending on position, and 0 is (strictly) octal and may be
followed by octal digits.
Neither grouping or digit delimiter characters are allowed, nor type suffixes.
Any result of zero or where *endptr is not a NUL or some other expected
delimiter character may be an issue, and some diagnostic may be desirable from
the invoker.
> Specifically, this applies:
>
> "The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character that is of the expected form."
>
> In the case of "0x" not followed by any hex digit, the "subject sequence" is exactly first "0" character, which is perfectly convertible (to 0, obviously) in any base (including 0 and 16). Libc behavior on Linux exactly demonstrates this.
--
Take care. Thanks, Brian Inglis Calgary, Alberta, Canada
La perfection est atteinte Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter not when there is no more to add
mais lorsqu'il n'y a plus rien à retrancher but when there is no more to cut
-- Antoine de Saint-Exupéry
--
Problem reports: https://cygwin.com/problems.html
FAQ: https://cygwin.com/faq/
Documentation: https://cygwin.com/docs.html
Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple
| webmaster | delorie software privacy |
| Copyright © 2026 by DJ Delorie | Updated Sep 20 2026 |