Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cswrap-core: skip known wrappers while invoking an analyzer #44

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ macro(add_cs_executable name sources)
string(REGEX REPLACE "\\+\\+$" "" name_upper ${name_upper})

if(PATH_TO_${name_upper})
target_compile_definitions(${name}
PRIVATE -DPATH_TO_${name_upper}=${PATH_TO_${name_upper}})
set(def -DPATH_TO_${name_upper}=${PATH_TO_${name_upper}})
target_compile_definitions(cswrap PRIVATE ${def})
target_compile_definitions(${name} PRIVATE ${def})
endif()

install(TARGETS ${name} DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand Down
56 changes: 56 additions & 0 deletions src/cswrap-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,61 @@ static bool read_custom_opts(char **dst, const char *str)
}
}

#if defined(PATH_TO_CSCLNG) || defined(PATH_TO_CSGCCA) \
|| defined(PATH_TO_CSMATCH) || defined(PATH_TO_CSCPPC)
/* if `path` starts with `prefix`, remove the prefix and the trailing ':' */
static bool remove_path_prefix(char *path, const char *prefix)
{
const size_t prefix_len = strlen(prefix);
if (strncmp(path, prefix, prefix_len))
// prefix not found
return false;

// skip all ':' that follow the prefix
const char *end = path + prefix_len;
while (*end == ':')
++end;

// remove the prefix, including the trailing double dots
const size_t new_size = strlen(end) + /* NUL */ 1;
memmove(path, end, new_size);
return true;
}

/* if `path` starts with path to a known wrapper, remove the prefix */
static bool remove_known_wrapper_from_path(char *path)
{
bool removed = false;
#ifdef PATH_TO_CSCLNG
removed |= remove_path_prefix(path, PATH_TO_CSCLNG);
#endif
#ifdef PATH_TO_CSGCCA
removed |= remove_path_prefix(path, PATH_TO_CSGCCA);
#endif
#ifdef PATH_TO_CSMATCH
removed |= remove_path_prefix(path, PATH_TO_CSMATCH);
#endif
#ifdef PATH_TO_CSCPPC
removed |= remove_path_prefix(path, PATH_TO_CSCPPC);
#endif
return removed;
}

/* remove paths to known wrappers from the beginning of $PATH */
static void sanitize_path_for_analyzer(void)
{
char *path = getenv("PATH");
if (!path || !*path)
return;

/* the `path` string will be modified in place */
while (remove_known_wrapper_from_path(path))
kdudka marked this conversation as resolved.
Show resolved Hide resolved
;
lzaoral marked this conversation as resolved.
Show resolved Hide resolved
}
#else
# define sanitize_path_for_analyzer()
#endif

static void consider_running_analyzer(
const int argc_orig,
char **const argv_orig)
Expand Down Expand Up @@ -437,6 +492,7 @@ static void consider_running_analyzer(
}

/* try to start analyzer */
sanitize_path_for_analyzer();
pid_analyzer = launch_tool(analyzer_name_actual, argv, /* del_args */ NULL);

/* FIXME: release also the memory allocated by asprintf() and
Expand Down