aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/buffer.c
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2018-04-27 09:25:02 +0200
committerGitHub <noreply@github.com>2018-04-27 09:25:02 +0200
commit53f11dcfc7139fe6c8a6b114db4bfec5d91005a9 (patch)
treee9bfbaf10fc6681bbbcba0a0eabdce8a6f6840b5 /src/nvim/buffer.c
parent009ccfe170ada2c78ca7feabda567a7e901fb30b (diff)
parent4ce8521ee4a72e050bd187c2986708c5f98c7442 (diff)
downloadrneovim-53f11dcfc7139fe6c8a6b114db4bfec5d91005a9.tar.gz
rneovim-53f11dcfc7139fe6c8a6b114db4bfec5d91005a9.tar.bz2
rneovim-53f11dcfc7139fe6c8a6b114db4bfec5d91005a9.zip
Merge #8218 'Fix errors reported by PVS'
closes #4983
Diffstat (limited to 'src/nvim/buffer.c')
-rw-r--r--src/nvim/buffer.c150
1 files changed, 83 insertions, 67 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c
index 0cd6f628b5..ba63822837 100644
--- a/src/nvim/buffer.c
+++ b/src/nvim/buffer.c
@@ -22,6 +22,7 @@
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>
+#include <assert.h>
#include "nvim/api/private/handle.h"
#include "nvim/api/private/helpers.h"
@@ -2885,15 +2886,13 @@ static char_u *lasticon = NULL;
void maketitle(void)
{
- char_u *p;
char_u *t_str = NULL;
char_u *i_name;
char_u *i_str = NULL;
int maxlen = 0;
int len;
int mustset;
- char_u buf[IOSIZE];
- int off;
+ char buf[IOSIZE];
if (!redrawing()) {
/* Postpone updating the title when 'lazyredraw' is set. */
@@ -2913,97 +2912,117 @@ void maketitle(void)
}
}
- t_str = buf;
if (*p_titlestring != NUL) {
if (stl_syntax & STL_IN_TITLE) {
int use_sandbox = FALSE;
int save_called_emsg = called_emsg;
use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
- called_emsg = FALSE;
- build_stl_str_hl(curwin, t_str, sizeof(buf),
- p_titlestring, use_sandbox,
- 0, maxlen, NULL, NULL);
- if (called_emsg)
- set_string_option_direct((char_u *)"titlestring", -1,
- (char_u *)"", OPT_FREE, SID_ERROR);
+ called_emsg = false;
+ build_stl_str_hl(curwin, (char_u *)buf, sizeof(buf),
+ p_titlestring, use_sandbox,
+ 0, maxlen, NULL, NULL);
+ t_str = (char_u *)buf;
+ if (called_emsg) {
+ set_string_option_direct((char_u *)"titlestring", -1, (char_u *)"",
+ OPT_FREE, SID_ERROR);
+ }
called_emsg |= save_called_emsg;
- } else
+ } else {
t_str = p_titlestring;
+ }
} else {
- /* format: "fname + (path) (1 of 2) - VIM" */
-
-#define SPACE_FOR_FNAME (IOSIZE - 100)
-#define SPACE_FOR_DIR (IOSIZE - 20)
-#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
- if (curbuf->b_fname == NULL)
- STRLCPY(buf, _("[No Name]"), SPACE_FOR_FNAME + 1);
- else {
- p = transstr(path_tail(curbuf->b_fname));
- STRLCPY(buf, p, SPACE_FOR_FNAME + 1);
- xfree(p);
+ // Format: "fname + (path) (1 of 2) - VIM".
+
+#define SPACE_FOR_FNAME (sizeof(buf) - 100)
+#define SPACE_FOR_DIR (sizeof(buf) - 20)
+#define SPACE_FOR_ARGNR (sizeof(buf) - 10) // At least room for " - NVIM".
+ char *buf_p = buf;
+ if (curbuf->b_fname == NULL) {
+ const size_t size = xstrlcpy(buf_p, _("[No Name]"),
+ SPACE_FOR_FNAME + 1);
+ buf_p += MIN(size, SPACE_FOR_FNAME);
+ } else {
+ buf_p += transstr_buf((const char *)path_tail(curbuf->b_fname),
+ buf_p, SPACE_FOR_FNAME + 1);
}
switch (bufIsChanged(curbuf)
- + (curbuf->b_p_ro * 2)
- + (!MODIFIABLE(curbuf) * 4)) {
- case 1: STRCAT(buf, " +"); break;
- case 2: STRCAT(buf, " ="); break;
- case 3: STRCAT(buf, " =+"); break;
- case 4:
- case 6: STRCAT(buf, " -"); break;
- case 5:
- case 7: STRCAT(buf, " -+"); break;
+ | (curbuf->b_p_ro << 1)
+ | (!MODIFIABLE(curbuf) << 2)) {
+ case 0: break;
+ case 1: buf_p = strappend(buf_p, " +"); break;
+ case 2: buf_p = strappend(buf_p, " ="); break;
+ case 3: buf_p = strappend(buf_p, " =+"); break;
+ case 4:
+ case 6: buf_p = strappend(buf_p, " -"); break;
+ case 5:
+ case 7: buf_p = strappend(buf_p, " -+"); break;
+ default: assert(false);
}
if (curbuf->b_fname != NULL) {
- /* Get path of file, replace home dir with ~ */
- off = (int)STRLEN(buf);
- buf[off++] = ' ';
- buf[off++] = '(';
- home_replace(curbuf, curbuf->b_ffname,
- buf + off, (size_t)(SPACE_FOR_DIR - off), true);
+ // Get path of file, replace home dir with ~.
+ *buf_p++ = ' ';
+ *buf_p++ = '(';
+ home_replace(curbuf, curbuf->b_ffname, (char_u *)buf_p,
+ (SPACE_FOR_DIR - (size_t)(buf_p - buf)), true);
#ifdef BACKSLASH_IN_FILENAME
- /* avoid "c:/name" to be reduced to "c" */
- if (isalpha(buf[off]) && buf[off + 1] == ':')
- off += 2;
+ // Avoid "c:/name" to be reduced to "c".
+ if (isalpha((uint8_t)buf_p) && *(buf_p + 1) == ':') {
+ buf_p += 2;
+ }
#endif
- /* remove the file name */
- p = path_tail_with_sep(buf + off);
- if (p == buf + off)
- /* must be a help buffer */
- STRLCPY(buf + off, _("help"), SPACE_FOR_DIR - off);
- else
+ // Remove the file name.
+ char *p = (char *)path_tail_with_sep((char_u *)buf_p);
+ if (p == buf_p) {
+ // Must be a help buffer.
+ xstrlcpy(buf_p, _("help"), SPACE_FOR_DIR - (size_t)(buf_p - buf));
+ } else {
*p = NUL;
+ }
- /* Translate unprintable chars and concatenate. Keep some
- * room for the server name. When there is no room (very long
- * file name) use (...). */
- if (off < SPACE_FOR_DIR) {
- p = transstr(buf + off);
- STRLCPY(buf + off, p, SPACE_FOR_DIR - off + 1);
- xfree(p);
+ // Translate unprintable chars and concatenate. Keep some
+ // room for the server name. When there is no room (very long
+ // file name) use (...).
+ if ((size_t)(buf_p - buf) < SPACE_FOR_DIR) {
+ char *const tbuf = transstr(buf_p);
+ const size_t free_space = SPACE_FOR_DIR - (size_t)(buf_p - buf) + 1;
+ const size_t dir_len = xstrlcpy(buf_p, tbuf, free_space);
+ buf_p += MIN(dir_len, free_space - 1);
+ xfree(tbuf);
} else {
- STRLCPY(buf + off, "...", SPACE_FOR_ARGNR - off + 1);
+ const size_t free_space = SPACE_FOR_ARGNR - (size_t)(buf_p - buf) + 1;
+ const size_t dots_len = xstrlcpy(buf_p, "...", free_space);
+ buf_p += MIN(dots_len, free_space - 1);
}
- STRCAT(buf, ")");
+ *buf_p++ = ')';
+ *buf_p = NUL;
+ } else {
+ *buf_p = NUL;
}
- append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
+ append_arg_number(curwin, (char_u *)buf_p,
+ (int)(SPACE_FOR_ARGNR - (size_t)(buf_p - buf)), false);
- STRCAT(buf, " - NVIM");
+ xstrlcat(buf_p, " - NVIM", (sizeof(buf) - (size_t)(buf_p - buf)));
if (maxlen > 0) {
- /* make it shorter by removing a bit in the middle */
- if (vim_strsize(buf) > maxlen)
- trunc_string(buf, buf, maxlen, IOSIZE);
+ // Make it shorter by removing a bit in the middle.
+ if (vim_strsize((char_u *)buf) > maxlen) {
+ trunc_string((char_u *)buf, (char_u *)buf, maxlen, sizeof(buf));
+ }
}
+ t_str = (char_u *)buf;
+#undef SPACE_FOR_FNAME
+#undef SPACE_FOR_DIR
+#undef SPACE_FOR_ARGNR
}
}
mustset = ti_change(t_str, &lasttitle);
if (p_icon) {
- i_str = buf;
+ i_str = (char_u *)buf;
if (*p_iconstring != NUL) {
if (stl_syntax & STL_IN_ICON) {
int use_sandbox = FALSE;
@@ -3091,7 +3110,6 @@ void free_titles(void)
/// be used when printing numbers in the status line.
typedef enum {
kNumBaseDecimal = 10,
- kNumBaseOctal = 8,
kNumBaseHexadecimal = 16
} NumberBase;
@@ -3889,9 +3907,7 @@ int build_stl_str_hl(
// Note: The `*` means we take the width as one of the arguments
*t++ = '*';
- *t++ = (char_u) (base == kNumBaseHexadecimal ? 'X'
- : (base == kNumBaseOctal ? 'o'
- : 'd'));
+ *t++ = (char_u)(base == kNumBaseHexadecimal ? 'X' : 'd');
*t = 0;
// }
@@ -5346,7 +5362,7 @@ void bufhl_clear_line_range(buf_T *buf,
if (line > line_end) {
break;
}
- if (line_start <= line && line <= line_end) {
+ if (line_start <= line) {
BufhlLineStatus status = bufhl_clear_line(l, src_id, line);
if (status != kBLSUnchanged) {
if (line > last_changed) {