From 0bfe044a8d2f5baab02e8e7f599d5459664ba11c Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Fri, 27 Nov 2020 14:19:40 +0100
Subject: 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.
---
 src/binary_location.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

(limited to 'src')

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();
-- 
cgit v1.2.1