about summary refs log tree commit diff homepage
path: root/runtime/POSIX/stubs.c
diff options
context:
space:
mode:
authorFrank Busse <bb0xfb@gmail.com>2018-09-03 20:49:50 +0100
committerCristian Cadar <c.cadar@imperial.ac.uk>2018-09-06 11:19:12 +0100
commit21380e5201f235a5f9ee9676785d08d39743aa4e (patch)
treebdef9e288514ca5b7611a3cdffbe8c0e823ab4ff /runtime/POSIX/stubs.c
parent21c5a5a52cfccdd8deec6add0dd24d4e5055aea4 (diff)
downloadklee-21380e5201f235a5f9ee9676785d08d39743aa4e.tar.gz
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.
Diffstat (limited to 'runtime/POSIX/stubs.c')
-rw-r--r--runtime/POSIX/stubs.c12
1 files changed, 9 insertions, 3 deletions
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 <errno.h>
 #include <limits.h>
 #include <signal.h>
@@ -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));