From 250e354525d82cc08335da539ee231fceeb2ed66 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Fri, 12 Jan 2018 11:34:43 +0300 Subject: [PATCH] smatch: get the smatch bin dir correctly when it's a symlink Say you have a symlink from ~/bin/smatch to ~/src/smatch/smatch then Smatch was expecting to find the smatch_data/ directory in ~/bin/smatch_data instead of ~/src/smatch/smatch_data/. We can find the actual bin location from reading in /proc. Reported-by: Knut Omang Signed-off-by: Dan Carpenter --- smatch.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/smatch.c b/smatch.c index c6ae26b0..58ff88d3 100644 --- a/smatch.c +++ b/smatch.c @@ -236,6 +236,18 @@ void parse_args(int *argcp, char ***argvp) option_project = PROJ_WINE; } +static char *read_bin_filename(void) +{ + char filename[PATH_MAX] = {}; + char proc[PATH_MAX]; + + pid_t pid = getpid(); + sprintf(proc, "/proc/%d/exe", pid); + if (readlink(proc, filename, PATH_MAX) < 0) + return NULL; + return alloc_string(filename); +} + static char *get_data_dir(char *arg0) { char *bin_dir; @@ -259,7 +271,9 @@ static char *get_data_dir(char *arg0) if (!access(dir, R_OK)) return dir; - orig = alloc_string(arg0); + orig = read_bin_filename(); + if (!orig) + orig = alloc_string(arg0); bin_dir = dirname(orig); strncpy(buf, bin_dir, 254); free_string(orig); -- 2.11.4.GIT