aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os_unix.c
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2015-02-02 01:21:58 -0500
committerJustin M. Keyes <justinkz@gmail.com>2015-02-02 01:21:58 -0500
commit6360be982fb2683a8ea5d7708e0651937349f9cb (patch)
tree8d53a3adc475f4e424a99dec330f51858e4ad8c9 /src/nvim/os_unix.c
parent4d70aae770e33c45a194634d11b9c49e866dd84b (diff)
parentbb674e0fcdc19ee069ad5130e5978d24b90765d0 (diff)
downloadrneovim-6360be982fb2683a8ea5d7708e0651937349f9cb.tar.gz
rneovim-6360be982fb2683a8ea5d7708e0651937349f9cb.tar.bz2
rneovim-6360be982fb2683a8ea5d7708e0651937349f9cb.zip
Merge #1896 'Fix coverity issues. (3)'
Diffstat (limited to 'src/nvim/os_unix.c')
-rw-r--r--src/nvim/os_unix.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c
index f7b47f9569..d674db951f 100644
--- a/src/nvim/os_unix.c
+++ b/src/nvim/os_unix.c
@@ -13,6 +13,7 @@
* changed beyond recognition.
*/
+#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
@@ -1158,14 +1159,30 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
free(tempname);
goto notfound;
}
- fseek(fd, 0L, SEEK_END);
- len = ftell(fd); /* get size of temp file */
+ int fseek_res = fseek(fd, 0L, SEEK_END);
+ if (fseek_res < 0) {
+ free(tempname);
+ fclose(fd);
+ return FAIL;
+ }
+ long long templen = ftell(fd); /* get size of temp file */
+ if (templen < 0) {
+ free(tempname);
+ fclose(fd);
+ return FAIL;
+ }
+#if SIZEOF_LONG_LONG > SIZEOF_SIZE_T
+ assert(templen <= (long long)SIZE_MAX);
+#endif
+ len = (size_t)templen;
fseek(fd, 0L, SEEK_SET);
buffer = xmalloc(len + 1);
- i = fread((char *)buffer, 1, len, fd);
+ // fread() doesn't terminate buffer with NUL;
+ // appropiate termination (not always NUL) is done below.
+ size_t readlen = fread((char *)buffer, 1, len, fd);
fclose(fd);
os_remove((char *)tempname);
- if (i != (int)len) {
+ if (readlen != len) {
/* unexpected read error */
EMSG2(_(e_notread), tempname);
free(tempname);
@@ -1174,8 +1191,6 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
}
free(tempname);
-
-
/* file names are separated with Space */
if (shell_style == STYLE_ECHO) {
buffer[len] = '\n'; /* make sure the buffer ends in NL */
@@ -1235,6 +1250,8 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
if (len)
++i; /* count last entry */
}
+ assert(buffer[len] == NUL || buffer[len] == '\n');
+
if (i == 0) {
/*
* Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".