diff options
author | David Robillard <d@drobilla.net> | 2020-11-27 14:19:40 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-11-27 15:20:08 +0100 |
commit | 0bfe044a8d2f5baab02e8e7f599d5459664ba11c (patch) | |
tree | d56a8fa8b0e097c50e0b2cc3587301125716a46f | |
parent | 25d846ec5bbe757099833641b859f987c57b4991 (diff) | |
download | patchage-0bfe044a8d2f5baab02e8e7f599d5459664ba11c.tar.gz patchage-0bfe044a8d2f5baab02e8e7f599d5459664ba11c.tar.bz2 patchage-0bfe044a8d2f5baab02e8e7f599d5459664ba11c.zip |
Modernize binary_location() implementation
On any reasonably modern system, realpath() does allocation so we can avoid
using PATH_MAX here which has its own issues.
-rw-r--r-- | src/binary_location.h | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/src/binary_location.h b/src/binary_location.h index 111a04f..3705d94 100644 --- a/src/binary_location.h +++ b/src/binary_location.h @@ -26,24 +26,23 @@ #include <string> /** Return the absolute path of the binary. */ -static std::string +inline std::string binary_location() { Dl_info dli; std::string loc; const int ret = dladdr((void*)&binary_location, &dli); if (ret) { - char* const bin_loc = (char*)calloc(PATH_MAX, 1); - if (realpath(dli.dli_fname, bin_loc)) { + if (char* const bin_loc = realpath(dli.dli_fname, nullptr)) { loc = bin_loc; + free(bin_loc); } - free(bin_loc); } return loc; } /** Return the absolute path of the bundle (binary parent directory). */ -static std::string +inline std::string bundle_location() { const std::string binary = binary_location(); |