]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
musl: make strtoll() accept strings start with dot 39700/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Sep 2025 00:10:44 +0000 (09:10 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 14 Nov 2025 18:41:40 +0000 (03:41 +0900)
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 [new file with mode: 0644]
src/libc/musl/meson.build
src/libc/musl/stdlib.c [new file with mode: 0644]

diff --git a/src/include/musl/stdlib.h b/src/include/musl/stdlib.h
new file mode 100644 (file)
index 0000000..ecfd6cc
--- /dev/null
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include_next <stdlib.h>
+
+long long strtoll_fallback(const char *nptr, char **endptr, int base);
+#define strtoll strtoll_fallback
index 78f93c35421a6cc548119527757fcfbfdc651ac8..54866a8b612766565879bf61513fb7b0b2d1286f 100644 (file)
@@ -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 (file)
index 0000000..1106fec
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <stdlib.h>
+
+/* 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);
+}