From 6e75bb5cbbf172bf586b3326ac43bad219ae26a3 Mon Sep 17 00:00:00 2001 From: cztchoice Date: Sat, 18 Jul 2015 17:27:05 +0800 Subject: refactor: strlcat instead of str{n}cat. Add xstrlcat function. Closes #3042 References #988 References #1069 coverity: 71530, 71531, 71532 --- src/nvim/ex_cmds.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/nvim/ex_cmds.c') diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index cf711552be..431b608bef 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -4977,8 +4977,12 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname, // Find all *.txt files. size_t dirlen = STRLCPY(NameBuff, dir, sizeof(NameBuff)); - STRCAT(NameBuff, "/**/*"); // NOLINT - STRCAT(NameBuff, ext); + if (dirlen >= MAXPATHL + || STRLCAT(NameBuff, "/**/*", sizeof(NameBuff)) >= MAXPATHL // NOLINT + || STRLCAT(NameBuff, ext, sizeof(NameBuff)) >= MAXPATHL) { + EMSG(_(e_pathtoolong)); + return; + } // Note: We cannot just do `&NameBuff` because it is a statically sized array // so `NameBuff == &NameBuff` according to C semantics. @@ -4995,9 +4999,12 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname, * Open the tags file for writing. * Do this before scanning through all the files. */ - STRLCPY(NameBuff, dir, sizeof(NameBuff)); + memcpy(NameBuff, dir, dirlen + 1); add_pathsep((char *)NameBuff); - STRNCAT(NameBuff, tagfname, sizeof(NameBuff) - dirlen - 2); + if (STRLCAT(NameBuff, tagfname, sizeof(NameBuff)) >= MAXPATHL) { + EMSG(_(e_pathtoolong)); + return; + } fd_tags = mch_fopen((char *)NameBuff, "w"); if (fd_tags == NULL) { EMSG2(_("E152: Cannot open %s for writing"), NameBuff); @@ -5172,7 +5179,11 @@ static void do_helptags(char_u *dirname, bool add_help_tags) // Get a list of all files in the help directory and in subdirectories. STRLCPY(NameBuff, dirname, sizeof(NameBuff)); add_pathsep((char *)NameBuff); - STRCAT(NameBuff, "**"); + if (STRLCAT(NameBuff, "**", MAXPATHL) >= MAXPATHL) { + EMSG(_(e_pathtoolong)); + xfree(dirname); + return; + } // Note: We cannot just do `&NameBuff` because it is a statically sized array // so `NameBuff == &NameBuff` according to C semantics. -- cgit From 7c7c5a80a46b38067e8288e1e3ccd2b9c7c73b6e Mon Sep 17 00:00:00 2001 From: cztchoice Date: Sat, 17 Oct 2015 12:18:26 +0800 Subject: add_pathsep(): Return false if filename is too long. References #3042 --- src/nvim/ex_cmds.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/nvim/ex_cmds.c') diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 431b608bef..3a551975f3 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -4812,9 +4812,13 @@ void fix_help_buffer(void) vimconv_T vc; char_u *cp; - /* Find all "doc/ *.txt" files in this directory. */ - add_pathsep((char *)NameBuff); - STRCAT(NameBuff, "doc/*.??[tx]"); + // Find all "doc/ *.txt" files in this directory. + if (!add_pathsep((char *)NameBuff) + || STRLCAT(NameBuff, "doc/*.??[tx]", + sizeof(NameBuff)) >= MAXPATHL) { + EMSG(_(e_pathtoolong)); + continue; + } // Note: We cannot just do `&NameBuff` because it is a statically sized array // 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; } - /* - * Open the tags file for writing. - * Do this before scanning through all the files. - */ + // + // Open the tags file for writing. + // Do this before scanning through all the files. + // memcpy(NameBuff, dir, dirlen + 1); - add_pathsep((char *)NameBuff); - if (STRLCAT(NameBuff, tagfname, sizeof(NameBuff)) >= MAXPATHL) { + if (!add_pathsep((char *)NameBuff) + || STRLCAT(NameBuff, tagfname, sizeof(NameBuff)) >= MAXPATHL) { EMSG(_(e_pathtoolong)); 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. STRLCPY(NameBuff, dirname, sizeof(NameBuff)); - add_pathsep((char *)NameBuff); - if (STRLCAT(NameBuff, "**", MAXPATHL) >= MAXPATHL) { + if (!add_pathsep((char *)NameBuff) + || STRLCAT(NameBuff, "**", sizeof(NameBuff)) >= MAXPATHL) { EMSG(_(e_pathtoolong)); xfree(dirname); return; -- cgit From 73da522d73c5d686c122ea4482d03e576a6a242c Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 23 Jan 2017 15:32:11 +0100 Subject: errors: Introduce "E856: Filename too long" --- src/nvim/ex_cmds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/ex_cmds.c') diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 3a551975f3..19d8acd75a 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -4816,7 +4816,7 @@ void fix_help_buffer(void) if (!add_pathsep((char *)NameBuff) || STRLCAT(NameBuff, "doc/*.??[tx]", sizeof(NameBuff)) >= MAXPATHL) { - EMSG(_(e_pathtoolong)); + EMSG(_(e_fnametoolong)); continue; } @@ -4984,7 +4984,7 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname, if (dirlen >= MAXPATHL || STRLCAT(NameBuff, "/**/*", sizeof(NameBuff)) >= MAXPATHL // NOLINT || STRLCAT(NameBuff, ext, sizeof(NameBuff)) >= MAXPATHL) { - EMSG(_(e_pathtoolong)); + EMSG(_(e_fnametoolong)); return; } @@ -5006,7 +5006,7 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname, memcpy(NameBuff, dir, dirlen + 1); if (!add_pathsep((char *)NameBuff) || STRLCAT(NameBuff, tagfname, sizeof(NameBuff)) >= MAXPATHL) { - EMSG(_(e_pathtoolong)); + EMSG(_(e_fnametoolong)); return; } fd_tags = mch_fopen((char *)NameBuff, "w"); @@ -5184,7 +5184,7 @@ static void do_helptags(char_u *dirname, bool add_help_tags) STRLCPY(NameBuff, dirname, sizeof(NameBuff)); if (!add_pathsep((char *)NameBuff) || STRLCAT(NameBuff, "**", sizeof(NameBuff)) >= MAXPATHL) { - EMSG(_(e_pathtoolong)); + EMSG(_(e_fnametoolong)); xfree(dirname); return; } -- cgit From d97d66e173b11a26015a99bed8467081d9c8733e Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 22 Jan 2017 22:04:00 +0100 Subject: coverity/155512: Pass correct length to strncat() References 8bc2bffda94bf2de4e8adae57b4b5597ed4e8247 --- src/nvim/ex_cmds.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/nvim/ex_cmds.c') diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 19d8acd75a..56919db024 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1408,8 +1408,8 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp) } if (itmp != NULL) { - strncat(buf, " < ", len - 1); - strncat(buf, (const char *)itmp, len - 1); + xstrlcat(buf, " < ", len - 1); + xstrlcat(buf, (const char *)itmp, len - 1); } #else // For shells that don't understand braces around commands, at least allow @@ -1425,13 +1425,13 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp) *p = NUL; } } - strncat(buf, " < ", len); - strncat(buf, (const char *)itmp, len); + xstrlcat(buf, " < ", len); + xstrlcat(buf, (const char *)itmp, len); if (*p_shq == NUL) { const char *const p = strchr((const char *)cmd, '|'); if (p != NULL) { - strncat(buf, " ", len - 1); // Insert a space before the '|' for DOS - strncat(buf, p, len - 1); + xstrlcat(buf, " ", len - 1); // Insert a space before the '|' for DOS + xstrlcat(buf, p, len - 1); } } } -- cgit