Commit 2cf30bd0 by Corentin Wallez

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/269990Reviewed-by: 's avatarKenneth Russell <kbr@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent 0e201cb7
...@@ -7,36 +7,25 @@ ...@@ -7,36 +7,25 @@
// Linux_path_utils.cpp: Implementation of OS-specific path functions for Linux // Linux_path_utils.cpp: Implementation of OS-specific path functions for Linux
#include "path_utils.h" #include "path_utils.h"
#include <array>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <array>
std::string GetExecutablePath() std::string GetExecutablePath()
{ {
struct stat sb; // We cannot use lstat to get the size of /proc/self/exe as it always returns 0
if (lstat("/proc/self/exe", &sb) == -1) // so we just use a big buffer and hope the path fits in it.
{ char path[4096];
return "";
}
char *path = static_cast<char*>(malloc(sb.st_size + 1)); ssize_t result = readlink("/proc/self/exe", path, sizeof(path) - 1);
if (!path) if (result < 0 || result >= sizeof(path) - 1)
{ {
return ""; return "";
} }
ssize_t result = readlink("/proc/self/exe", path, sb.st_size); path[result] = '\0';
if (result != sb.st_size) return path;
{
free(path);
return "";
}
path[sb.st_size] = '\0';
std::string strPath(path);
free(path);
return strPath;
} }
std::string GetExecutableDirectory() std::string GetExecutableDirectory()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment