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

@ -4812,9 +4812,13 @@ void fix_help_buffer(void)
vimconv_T vc; vimconv_T vc;
char_u *cp; char_u *cp;
/* Find all "doc/ *.txt" files in this directory. */ // Find all "doc/ *.txt" files in this directory.
add_pathsep((char *)NameBuff); if (!add_pathsep((char *)NameBuff)
STRCAT(NameBuff, "doc/*.??[tx]"); || STRLCAT(NameBuff, "doc/*.??[tx]",
sizeof(NameBuff)) >= MAXPATHL) {
EMSG(_(e_pathtoolong));
continue;
}
// Note: We cannot just do `&NameBuff` because it is a statically sized array // Note: We cannot just do `&NameBuff` because it is a statically sized array
// so `NameBuff == &NameBuff` according to C semantics. // so `NameBuff == &NameBuff` according to C semantics.
@ -4995,13 +4999,13 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
return; return;
} }
/* //
* Open the tags file for writing. // Open the tags file for writing.
* Do this before scanning through all the files. // Do this before scanning through all the files.
*/ //
memcpy(NameBuff, dir, dirlen + 1); memcpy(NameBuff, dir, dirlen + 1);
add_pathsep((char *)NameBuff); if (!add_pathsep((char *)NameBuff)
if (STRLCAT(NameBuff, tagfname, sizeof(NameBuff)) >= MAXPATHL) { || STRLCAT(NameBuff, tagfname, sizeof(NameBuff)) >= MAXPATHL) {
EMSG(_(e_pathtoolong)); EMSG(_(e_pathtoolong));
return; return;
} }
@ -5178,8 +5182,8 @@ static void do_helptags(char_u *dirname, bool add_help_tags)
// Get a list of all files in the help directory and in subdirectories. // Get a list of all files in the help directory and in subdirectories.
STRLCPY(NameBuff, dirname, sizeof(NameBuff)); STRLCPY(NameBuff, dirname, sizeof(NameBuff));
add_pathsep((char *)NameBuff); if (!add_pathsep((char *)NameBuff)
if (STRLCAT(NameBuff, "**", MAXPATHL) >= MAXPATHL) { || STRLCAT(NameBuff, "**", sizeof(NameBuff)) >= MAXPATHL) {
EMSG(_(e_pathtoolong)); EMSG(_(e_pathtoolong));
xfree(dirname); xfree(dirname);
return; return;

View File

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