From f5b66e7481ea9a5c1aaf8fc316bef82e5f8cb4d1 Mon Sep 17 00:00:00 2001 From: implicitfield <114500360+implicitfield@users.noreply.github.com> Date: Thu, 6 Mar 2025 12:10:28 +0200 Subject: [PATCH] LibC+less: Remove optional includes from stdlib.h LLVM's libcxx/include/stdlib.h has this to say about how it includes the system's stdlib.h: """ The inclusion of the system's is intentionally done once outside of any include guards because some code expects to be able to include the underlying system header multiple times to get different definitions based on the macros that are set before inclusion. """ Since our stdlib.h includes math.h (which is more-or-less optional, at least macOS doesn't seem to do this), we end up in a broken include cycle (because libcxx has its own math.h, which then includes its own stdlib.h, getting us back to where we started from.) Due to this, LLVM never sees our definition of ldiv_t, which breaks the toolchain build. Just to illustrate further, the situation looks basically like this: ``` // a.h #include "b.h" #ifndef A #define A foo_t bar = 0; #endif // b.h #ifndef B #define B #include "c.h" typedef int foo_t; #endif // c.h #ifndef C #define C #include "a.h" #endif // main.c #include "a.h" int main() {}; ``` To work around this, this patch makes stdlib.h not pull in any extra headers, since other systems don't appear to do this, and this isn't exactly required by POSIX either (the exact wording in the spec is "Inclusion of the header may also make visible all symbols from , , , and .") --- Userland/Libraries/LibC/stdlib.h | 7 ------- Userland/Utilities/less.cpp | 1 + 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Userland/Libraries/LibC/stdlib.h b/Userland/Libraries/LibC/stdlib.h index cfcd544526b..edc5f7ebd48 100644 --- a/Userland/Libraries/LibC/stdlib.h +++ b/Userland/Libraries/LibC/stdlib.h @@ -6,13 +6,6 @@ #pragma once -// Includes essentially mandated by POSIX: -// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdlib.h.html -#include -#include -#include -#include - #include #include #include diff --git a/Userland/Utilities/less.cpp b/Userland/Utilities/less.cpp index dab56b8529d..dbfc99d2ac0 100644 --- a/Userland/Utilities/less.cpp +++ b/Userland/Utilities/less.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include