Skip to content

Commit

Permalink
main: consider an empty path component when combining a path and file…
Browse files Browse the repository at this point in the history
… name

combinePathAndFile takes two C strings.
The first one PATH was used in an expression: path [strlen(path) - 1] .
If PATH is an empty, this can cause a buffer overrun.

This change adds a guard-condition for such processing.

Inspired from the comment submitted by @itchyny in universal-ctags#2384.
  • Loading branch information
masatake committed Dec 23, 2019
1 parent e216bb4 commit 0a813e5
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions main/routines.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,18 @@ extern char *combinePathAndFile (
const char *const path, const char *const file)
{
vString *const filePath = vStringNew ();
const int lastChar = path [strlen (path) - 1];
bool terminated = isPathSeparator (lastChar);
size_t len = strlen (path);

vStringCopyS (filePath, path);
if (! terminated)
vStringPut (filePath, OUTPUT_PATH_SEPARATOR);
vStringCatS (filePath, file);
if (len)
{
const int lastChar = path [len - 1];
bool terminated = isPathSeparator (lastChar);
vStringCopyS (filePath, path);
if (! terminated)
vStringPut (filePath, OUTPUT_PATH_SEPARATOR);
}

vStringCatS (filePath, file);
return vStringDeleteUnwrap (filePath);
}

Expand Down

0 comments on commit 0a813e5

Please sign in to comment.