From 2cf30bd09c1c1eede7fc088c790d20a334a1e7b9 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Thu, 7 May 2015 13:55:14 -0400 Subject: [PATCH] Fix GetExecutablePath on Linux It was using lstat to get the size of /proc/self/exe but it always returns 0, so we just use a big buffer on the stack instead. BUG=angleproject:892 Change-Id: I6d88efeb4ec5de7a78cb3668e3d78520203ad1d5 Reviewed-on: https://chromium-review.googlesource.com/269990 Reviewed-by: Kenneth Russell Reviewed-by: Jamie Madill Tested-by: Corentin Wallez --- util/linux/Linux_path_utils.cpp | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/util/linux/Linux_path_utils.cpp b/util/linux/Linux_path_utils.cpp index be1d8aafa..b674e1018 100644 --- a/util/linux/Linux_path_utils.cpp +++ b/util/linux/Linux_path_utils.cpp @@ -7,36 +7,25 @@ // Linux_path_utils.cpp: Implementation of OS-specific path functions for Linux #include "path_utils.h" -#include + #include #include +#include std::string GetExecutablePath() { - struct stat sb; - if (lstat("/proc/self/exe", &sb) == -1) + // We cannot use lstat to get the size of /proc/self/exe as it always returns 0 + // so we just use a big buffer and hope the path fits in it. + char path[4096]; + + ssize_t result = readlink("/proc/self/exe", path, sizeof(path) - 1); + if (result < 0 || result >= sizeof(path) - 1) { return ""; } - char *path = static_cast(malloc(sb.st_size + 1)); - if (!path) - { - return ""; - } - - ssize_t result = readlink("/proc/self/exe", path, sb.st_size); - if (result != sb.st_size) - { - free(path); - return ""; - } - - path[sb.st_size] = '\0'; - - std::string strPath(path); - free(path); - return strPath; + path[result] = '\0'; + return path; } std::string GetExecutableDirectory()