add_pathsep(): Return false if filename is too long.

References #3042
This commit is contained in:
cztchoice
2015-10-17 12:18:26 +08:00
committed by Justin M. Keyes
parent 6e75bb5cbb
commit 7c7c5a80a4
2 changed files with 25 additions and 18 deletions

View File

@@ -391,19 +391,22 @@ char *concat_fnames_realloc(char *fname1, const char *fname2, bool sep)
fname2, len2, sep);
}
/*
* Add a path separator to a file name, unless it already ends in a path
* separator.
*/
void add_pathsep(char *p)
/// Adds a path separator to a filename, unless it already ends in one.
///
/// @return `true` if the path separator was added or already existed.
/// `false` if the filename is too long.
bool add_pathsep(char *p)
FUNC_ATTR_NONNULL_ALL
{
const size_t len = strlen(p);
const size_t pathsep_len = sizeof(PATHSEPSTR);
assert(len < MAXPATHL - pathsep_len);
if (*p != NUL && !after_pathsep(p, p + len)) {
const size_t pathsep_len = sizeof(PATHSEPSTR);
if (len > MAXPATHL - pathsep_len) {
return false;
}
memcpy(p + len, PATHSEPSTR, pathsep_len);
}
return true;
}
/// Get an allocated copy of the full path to a file.