Skip to content

Commit ce88c82

Browse files
authored
[ld_logger] Fix suffix match on non-absolute paths (#4577)
When using GNU make-4.2.1, then compiler binary names are not extended to absolute path. By setting "/cc" to CC_LOGGER_GCC_LIKE we assume that the compiler binary is provided by absolute path, since we are performing a suffix check. However, "/cc" is not suffix of "cc". This patch fixes the issue by virtually extending the compiler binary to absolute path.
1 parent 5084dad commit ce88c82

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

analyzer/tools/build-logger/src/ldlogger-tool.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,29 @@ static int matchToProgramList(
5757

5858
if (strchr(token, '/'))
5959
{
60-
const char* posOfToken = strstr(progPath_, token);
61-
if (posOfToken)
62-
found = strcmp(posOfToken, token) == 0;
60+
// When the token contains '/', then the program name is considered as a
61+
// suffix:
62+
//
63+
// token -> /cc
64+
// -------------------
65+
// no match -> c
66+
// match -> cc
67+
// no match -> gcc
68+
// no match -> ccache
69+
// match -> bin2/cc
70+
if (token[0] == '/')
71+
++token;
72+
73+
int len_token = strlen(token);
74+
int len_prog = strlen(progPath_);
75+
76+
if (len_token >= len_prog)
77+
found = strcmp(token, progPath_) == 0;
78+
else
79+
{
80+
const char* suffix = progPath_ + len_prog - len_token;
81+
found = suffix[-1] == '/' && strcmp(token, suffix) == 0;
82+
}
6383
}
6484
else
6585
{

analyzer/tools/build-logger/tests/unit/test_paths.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,60 @@ def test_source_abs(self):
444444
file=file,
445445
directory=binary_dirname,
446446
)
447+
448+
def test_suffix_match_on_non_absolute_path(self):
449+
"""
450+
Test that CC_LOGGER_GCC_LIKE with a '/cc' value matches
451+
both 'cc' and '/usr/bin/cc', but not 'gcc' or 'ccache'.
452+
This covers the fix for suffix matching on non-absolute paths
453+
(see PR #4577).
454+
"""
455+
logger_env = self.get_envvars()
456+
logger_env["CC_LOGGER_GCC_LIKE"] = "/cc"
457+
file = self.source_file
458+
binary = self.binary_file
459+
460+
# Should match 'cc' (non-absolute)
461+
cc = shutil.which("cc")
462+
if cc:
463+
self.assume_successful_command(
464+
[cc, file, "-o", binary], logger_env
465+
)
466+
self.assume_successful_command(
467+
[binary], env=empty_env, outs="--VARIABLE--"
468+
)
469+
self.assert_json(
470+
command=f"{cc} {file} -o {binary}",
471+
file=file
472+
)
473+
os.remove(self.logger_file)
474+
475+
# Should not match 'gcc'
476+
# This can be an interesting edge case test because
477+
# gcc has 'cc' in the end of the path
478+
gcc = shutil.which("gcc")
479+
if gcc:
480+
self.assume_successful_command(
481+
[gcc, file, "-o", binary], logger_env
482+
)
483+
self.assume_successful_command(
484+
[binary], env=empty_env, outs="--VARIABLE--"
485+
)
486+
actual_json = self.read_actual_json()
487+
self.assertEqual(actual_json, "[\n]")
488+
os.remove(self.logger_file)
489+
490+
# Should not match 'ccache'
491+
# This can be an interesting edge case test because
492+
# ccache has 'cc' at the beginning of the path
493+
ccache = shutil.which("ccache")
494+
if ccache:
495+
self.assume_successful_command(
496+
[ccache, file, "-o", binary], logger_env
497+
)
498+
self.assume_successful_command(
499+
[binary], env=empty_env, outs="--VARIABLE--"
500+
)
501+
actual_json = self.read_actual_json()
502+
self.assertEqual(actual_json, "[\n]")
503+
os.remove(self.logger_file)

0 commit comments

Comments
 (0)