Skip to content

Commit

Permalink
configuration: fix incorrect handling of out-dir
Browse files Browse the repository at this point in the history
Currently the argument parsing code expands all the arguments.
This means that an out-dir parameter having the same name of an
executable is fully expanded, thus becoming an executable instead of a
directory.

Examples:
   > kcov python echo
   kcov: error: Can't find or open echo

   > kcov python /bin/echo
   kcov: error: Can't open directory /usr/bin/python3.11/

Remove checking if the first argument is an elf file, since the
executable must be after the out-dir argument.

Skip options and don't expand arguments, unless lstat fails.

Issue #414: kcov: error when out-dir is an executable
  • Loading branch information
perillo committed Mar 6, 2024
1 parent 1d036f7 commit 6148d2e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,28 @@ class Configuration : public IConfiguration
*/
for (lastArg = 1; lastArg < argc; lastArg++)
{
if (IParserManager::getInstance().matchParser(get_real_path(argv[lastArg])))
break;

bool found = false;
for (std::vector<std::string>::const_iterator it = paths.begin();
it != paths.end(); ++it)
{
const std::string &curPath = *it;
const std::string cur = get_real_path(
curPath + "/" + argv[lastArg]);
std::string cur = argv[lastArg];
struct stat st;

if (lstat(cur.c_str(), &st) < 0)
// Skip options.
if ((cur.size() > 0) && (cur[0] == '-'))
continue;

// Don't expand the path unless lstat fails, since it may
// incorrectly expand the out-dir argument, in case it has the
// same name as of an existing executable.
if (lstat(argv[lastArg], &st) < 0) {
cur = get_real_path(curPath + "/" + cur);

if (lstat(cur.c_str(), &st) < 0)
continue;
}

// Regular file?
if (S_ISREG(st.st_mode) == 0)
continue;
Expand All @@ -203,7 +210,7 @@ class Configuration : public IConfiguration
if ((st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
continue;

if (IParserManager::getInstance().matchParser(cur))
if (IParserManager::getInstance().matchParser(get_real_path(cur)))
{
// Intentional memory leak
argv[lastArg] = xstrdup(cur.c_str());
Expand Down

0 comments on commit 6148d2e

Please sign in to comment.