From 60c096eb0433f7573c768a2c8523abd3c11e0720 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 1 Apr 2026 20:37:02 +0200 Subject: Make do even if PATH_MAX isn't defined As explained in `man realpath(3)`: > The POSIX.1-2001 standard version of this function is broken by design, > since it is impossible to determine a suitable size for the output > buffer, resolved_path. According to POSIX.1-2001 a buffer of size PATH_MAX > suffices, but PATH_MAX need not be a defined constant, and may have to be > obtained using pathconf(3). And asking pathconf(3) does not really help, > since, on the one hand POSIX warns that the result of pathconf(3) may be huge > and unsuitable for mallocing memory, and on the other hand pathconf(3) may > return -1 to signify that PATH_MAX is not bounded. The re‐ solved_path > == NULL feature, not standardized in POSIX.1-2001, but standardized in > POSIX.1-2008, allows this design problem to be avoided. So we can either not compile, or be pragmatic, and define PATH_MAX to a sane value, like 4096, which is the one used on Linux and some/most BSD. This commit also adds two tests to ensure that things aren't catastrophically broken by this change. --- tests/Makefile | 2 ++ tests/test_realpath.c | 11 +++++++++++ tests/test_realpath_null.c | 11 +++++++++++ 3 files changed, 24 insertions(+) create mode 100644 tests/test_realpath.c create mode 100644 tests/test_realpath_null.c (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index 81f7f6e..9c2dede 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -61,6 +61,8 @@ RUNTIME_TARGETS= \ test_read_static \ test_readlink_dynamic \ test_readlink_static \ + test_realpath_null \ + test_realpath \ test_recv_dynamic \ test_recv_static \ test_recvfrom_dynamic \ diff --git a/tests/test_realpath.c b/tests/test_realpath.c new file mode 100644 index 0000000..2a17708 --- /dev/null +++ b/tests/test_realpath.c @@ -0,0 +1,11 @@ +#include "common.h" + +#include +#include + +int main(int argc, char** argv) { + char buf[PATH_MAX]; + char* rpath = realpath("/root/../", buf); + printf("%s\n", rpath); + return 0; +} diff --git a/tests/test_realpath_null.c b/tests/test_realpath_null.c new file mode 100644 index 0000000..b62cee8 --- /dev/null +++ b/tests/test_realpath_null.c @@ -0,0 +1,11 @@ +#include "common.h" + +#include +#include + +int main(int argc, char** argv) { + char* rpath = realpath("/root/../", NULL); + printf("%s\n", rpath); + free(rpath); + return 0; +} -- cgit v1.3