[newlib-cygwin/main] Fix strtol when there is an invalid hex number

Jeff Johnston jjohnstn@sourceware.org
Tue Sep 15 00:59:13 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=03f936e72cd84a2bc443fca1b096b2be774a4bc5

commit 03f936e72cd84a2bc443fca1b096b2be774a4bc5
Author: Jeff Johnston <jjohnstn@redhat.com>
Date:   Mon Sep 14 20:53:45 2026 -0400

    Fix strtol when there is an invalid hex number
    
    If input starts with '0x' but then either ends or has an non-hex
    character, treat it as if '0' is read and we stop at 'x'.  This
    matches what glibc does.

Diff:
---
 newlib/libc/stdlib/strtol.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/newlib/libc/stdlib/strtol.c b/newlib/libc/stdlib/strtol.c
index 09d4333ed..8376150eb 100644
--- a/newlib/libc/stdlib/strtol.c
+++ b/newlib/libc/stdlib/strtol.c
@@ -158,6 +158,7 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
 	    c == '0' && (*s == 'x' || *s == 'X')) {
 		c = s[1];
 		s += 2;
+		++nptr; /* if no more valid chars, we read '0' */
 		base = 16;
 	}
 	if (base == 0)


More information about the Newlib-cvs mailing list