From 46ea7c3e3289fd6704f5fb4cfbb1421523ca1c7a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 9 Sep 2025 09:10:44 +0900 Subject: [PATCH] musl: make strtoll() accept strings start with dot glibc accepts strings start with '.' and returns 0, but musl refuses them. Let's accept them, as our code assumes the function accept such strings. --- src/include/musl/stdlib.h | 7 +++++++ src/libc/musl/meson.build | 1 + src/libc/musl/stdlib.c | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 src/include/musl/stdlib.h create mode 100644 src/libc/musl/stdlib.c diff --git a/src/include/musl/stdlib.h b/src/include/musl/stdlib.h new file mode 100644 index 00000000000..ecfd6ccb430 --- /dev/null +++ b/src/include/musl/stdlib.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include_next + +long long strtoll_fallback(const char *nptr, char **endptr, int base); +#define strtoll strtoll_fallback diff --git a/src/libc/musl/meson.build b/src/libc/musl/meson.build index 78f93c35421..54866a8b612 100644 --- a/src/libc/musl/meson.build +++ b/src/libc/musl/meson.build @@ -6,5 +6,6 @@ endif libc_wrapper_sources += files( 'stdio.c', + 'stdlib.c', 'string.c', ) diff --git a/src/libc/musl/stdlib.c b/src/libc/musl/stdlib.c new file mode 100644 index 00000000000..1106fec9d0b --- /dev/null +++ b/src/libc/musl/stdlib.c @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include + +/* The header stdlib.h overrides strtoll with strtoll_fallback, hence we need to undef it here. */ +#undef strtoll + +long long strtoll_fallback(const char *nptr, char **endptr, int base) { + /* glibc returns 0 if the first character is '.' without error, but musl returns as an error. + * As our code assumes the glibc behavior, let's accept strings start with '.'. */ + if (nptr && *nptr == '.') { + if (endptr) + *endptr = (char*) nptr; + return 0; + } + + /* Otherwise, use the native strtoll(). */ + return strtoll(nptr, endptr, base); +} -- 2.47.3