From 21380e5201f235a5f9ee9676785d08d39743aa4e Mon Sep 17 00:00:00 2001 From: Frank Busse Date: Mon, 3 Sep 2018 20:49:50 +0100 Subject: runtime: fix memory error in canonicalize_file_name Fixes #46 and reverts #47. As stated in #46, the solution works for musl, glibc etc. However, the code in stub.c is executed by uclibc and uclibc doesn't allocate the target buffer in realpath. The memory error occured while running df for 10min with DFS. --- runtime/POSIX/stubs.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'runtime') diff --git a/runtime/POSIX/stubs.c b/runtime/POSIX/stubs.c index bb528ad4..6b87ad8d 100644 --- a/runtime/POSIX/stubs.c +++ b/runtime/POSIX/stubs.c @@ -7,8 +7,6 @@ // //===----------------------------------------------------------------------===// -#define _XOPEN_SOURCE 700 - #include #include #include @@ -265,7 +263,15 @@ gnu_dev_type gnu_dev_makedev(unsigned int __major, unsigned int __minor) { char *canonicalize_file_name (const char *name) __attribute__((weak)); char *canonicalize_file_name (const char *name) { - return realpath(name, NULL); + // Although many C libraries allocate resolved_name in realpath() if it is NULL, + // this behaviour is implementation-defined (POSIX) and not implemented in uclibc. + char * resolved_name = malloc(PATH_MAX); + if (!resolved_name) return NULL; + if (!realpath(name, resolved_name)) { + free(resolved_name); + return NULL; + } + return resolved_name; } int getloadavg(double loadavg[], int nelem) __attribute__((weak)); -- cgit 1.4.1