From c373e5de2a454b356b1f2284484985bcf6e33bb7 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 20 Nov 2023 12:18:11 -0800 Subject: [PATCH] Add missing __LONG_MAX definition in alltypes.h This mirrors an upstream change in musl (https://github.com/emscripten-core/musl/commit/7cc79d10) and should really have been done as part of #13006. With had report from a user who was injecting emscripten's `sysroot/include` path explicitly putting it before the clang's builtin include path which triggered `__LONG_MAX` to be undefined. --- .../lib/libc/musl/arch/emscripten/bits/alltypes.h | 8 ++++++++ system/lib/libc/musl/arch/emscripten/bits/limits.h | 11 ----------- test/other/test_stdint_limits.c | 13 +++++++++++++ test/other/test_stdint_limits.out | 8 ++++++++ test/test_other.py | 3 +++ 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/system/lib/libc/musl/arch/emscripten/bits/alltypes.h b/system/lib/libc/musl/arch/emscripten/bits/alltypes.h index d56dedad1acd3..d2ad1b633d026 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/alltypes.h +++ b/system/lib/libc/musl/arch/emscripten/bits/alltypes.h @@ -4,6 +4,14 @@ #define __BYTE_ORDER __LITTLE_ENDIAN +// Can't use __LONG_MAX__ here since musl's libs does pre-processor comparison +// with 0x7fffffffL directly. +#if __LP64__ +#define LONG_MAX 0x7fffffffffffffffL +#else +#define LONG_MAX 0x7fffffffL +#endif + #define _Addr __PTRDIFF_TYPE__ #define _Int64 __INT64_TYPE__ #define _Reg __PTRDIFF_TYPE__ diff --git a/system/lib/libc/musl/arch/emscripten/bits/limits.h b/system/lib/libc/musl/arch/emscripten/bits/limits.h index 338b2f2ebe3bf..7a09fdc1d175d 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/limits.h +++ b/system/lib/libc/musl/arch/emscripten/bits/limits.h @@ -1,12 +1 @@ -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #define PAGE_SIZE 65536 -#define LONG_BIT 32 -#endif - -#if __LP64__ -#define LONG_MAX 0x7fffffffffffffffL -#else -#define LONG_MAX 0x7fffffffL -#endif -#define LLONG_MAX 0x7fffffffffffffffLL diff --git a/test/other/test_stdint_limits.c b/test/other/test_stdint_limits.c index bb15510bbec58..7ba6b65a4fa0a 100644 --- a/test/other/test_stdint_limits.c +++ b/test/other/test_stdint_limits.c @@ -1,7 +1,20 @@ +#include #include #include int main () { + printf("INT_MIN: %d\n", INT_MIN); + printf("INT_MAX: %d\n", INT_MAX); + + printf("LONG_MIN: %ld\n", LONG_MIN); + printf("LONG_MAX: %ld\n", LONG_MAX); + + printf("LLONG_MIN: %lld\n", LLONG_MIN); + printf("LLONG_MAX: %lld\n", LLONG_MAX); + + printf("INTPTR_MIN: %ld\n", INTPTR_MIN); + printf("INTPTR_MAX: %ld\n", INTPTR_MAX); + printf("PTRDIFF_MIN: %ti\n", PTRDIFF_MIN); printf("PTRDIFF_MAX: %ti\n", PTRDIFF_MAX); diff --git a/test/other/test_stdint_limits.out b/test/other/test_stdint_limits.out index c55838c9dc55f..d3dedf21b6787 100644 --- a/test/other/test_stdint_limits.out +++ b/test/other/test_stdint_limits.out @@ -1,3 +1,11 @@ +INT_MIN: -2147483648 +INT_MAX: 2147483647 +LONG_MIN: -2147483648 +LONG_MAX: 2147483647 +LLONG_MIN: -9223372036854775808 +LLONG_MAX: 9223372036854775807 +INTPTR_MIN: -2147483648 +INTPTR_MAX: 2147483647 PTRDIFF_MIN: -2147483648 PTRDIFF_MAX: 2147483647 INTPTR_MIN: -2147483648 diff --git a/test/test_other.py b/test/test_other.py index b5e6ef3f25803..276ab8bce26c4 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -14198,3 +14198,6 @@ def test_no_pthread(self): self.do_runf('hello_world.c', emcc_args=['-pthread', '-no-pthread']) self.assertExists('hello_world.js') self.assertNotExists('hello_world.worker.js') + + def test_sysroot_includes_first(self): + self.do_other_test('test_stdint_limits.c', emcc_args=['-std=c11', '-iwithsysroot/include'])