From 266b9cb6544faf1a631689cd899cfde4ac0a3c25 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 17 Aug 2019 22:07:48 +0200 Subject: [PATCH] LibC: Fix strtol() not populating `endptr' for valid strings We were not writing anything out to the `endptr` pointer if a number was successfully parsed from the input string. Fixes #460. --- Libraries/LibC/stdlib.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Libraries/LibC/stdlib.cpp b/Libraries/LibC/stdlib.cpp index 4a41e5be446..d9962dff18e 100644 --- a/Libraries/LibC/stdlib.cpp +++ b/Libraries/LibC/stdlib.cpp @@ -352,7 +352,8 @@ long strtol(const char* str, char** endptr, int base) } } } - const char* estr = str + strlen(str) - 1; + size_t length = strlen(str); + const char* estr = str + length - 1; long track = 1; long num = 0; while (estr >= str) { @@ -362,12 +363,14 @@ long strtol(const char* str, char** endptr, int base) digit_value = 10 + (*estr - 'A'); num += (track *= base) / base * digit_value; } else { - if (endptr != NULL) + if (endptr) *endptr = const_cast(estr); return 0; }; estr--; } + if (endptr) + *endptr = const_cast(str + length); return num * sign; }