From fc575f6986d19205a3b97f1813246529c6d2fc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 5 Oct 2016 22:06:06 +0200 Subject: [PATCH] Fix various issues reported by coverity scan --- buffer.c | 4 +++- text-util.c | 3 +-- ui-curses.c | 4 +++- vis-lua.c | 2 +- vis-menu.c | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/buffer.c b/buffer.c index abf3ab7e3..f483d5337 100644 --- a/buffer.c +++ b/buffer.c @@ -118,8 +118,10 @@ bool buffer_vprintf(Buffer *buf, const char *fmt, va_list ap) { va_list ap_save; va_copy(ap_save, ap); int len = vsnprintf(NULL, 0, fmt, ap); - if (len == -1 || !buffer_grow(buf, len+1)) + if (len == -1 || !buffer_grow(buf, len+1)) { + va_end(ap_save); return false; + } bool ret = vsnprintf(buf->data, len+1, fmt, ap_save) == len; if (ret) buf->len = len+1; diff --git a/text-util.c b/text-util.c index c7a1349b4..f3ba0a06a 100644 --- a/text-util.c +++ b/text-util.c @@ -84,7 +84,6 @@ int text_string_width(const char *data, size_t len) { const char *s = data; while (len > 0) { - char buf[MB_CUR_MAX]; wchar_t wc; size_t wclen = mbrtowc(&wc, s, len, &ps); if (wclen == (size_t)-1 && errno == EILSEQ) { @@ -98,7 +97,7 @@ int text_string_width(const char *data, size_t len) { /* assume NUL byte will be displayed as ^@ */ width += 2; wclen = 1; - } else if (buf[0] == '\t') { + } else if (wc == L'\t') { width++; wclen = 1; } else { diff --git a/ui-curses.c b/ui-curses.c index 087396341..8f184e33c 100644 --- a/ui-curses.c +++ b/ui-curses.c @@ -1103,8 +1103,10 @@ static const char *ui_getkey(Ui *ui) { int tty = open("/dev/tty", O_RDWR); if (tty == -1) goto fatal; - if (tty != STDIN_FILENO && dup2(tty, STDIN_FILENO) == -1) + if (tty != STDIN_FILENO && dup2(tty, STDIN_FILENO) == -1) { + close(tty); goto fatal; + } close(tty); termkey_destroy(uic->termkey); if (!(uic->termkey = ui_termkey_new(STDIN_FILENO))) diff --git a/vis-lua.c b/vis-lua.c index 56b86aeb8..7f045fa45 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -800,7 +800,7 @@ static int window_index(lua_State *L) { const char *key = lua_tostring(L, 2); if (strcmp(key, "viewport") == 0) { - Filerange r = win ? view_viewport_get(win->view) : text_range_empty(); + Filerange r = view_viewport_get(win->view); pushrange(L, &r); return 1; } diff --git a/vis-menu.c b/vis-menu.c index a3162402f..00ba9d567 100644 --- a/vis-menu.c +++ b/vis-menu.c @@ -458,7 +458,7 @@ run(void) { return EXIT_FAILURE; case CONTROL('M'): /* Return */ case CONTROL('J'): - if (sel) strncpy(text, sel->text, sizeof text); /* Complete the input first, when hitting return */ + if (sel) strncpy(text, sel->text, sizeof(text)-1); /* Complete the input first, when hitting return */ cursor = strlen(text); match(); drawmenu();