mirror of
https://github.com/neovim/neovim.git
synced 2025-09-18 17:28:23 +00:00
coverity/13745: Argument cannot be negative: RI.
Problem : Argument cannot be negative @ 1165. Diagnostic : Real issue. Rationale : len can be assigned a negative value @ 1162; len is passed as an unsigned argument @ 1165. Resolution : Refactor variable's types: - Use ftello instead of ftell to avoid using long. - Assert ftello result is safely convertible to size_t. - Introduce variable read_size to avoid using i (int).
This commit is contained in:

committed by
Justin M. Keyes

parent
323f0488c2
commit
4d0ef9a6b9
@@ -7,6 +7,8 @@ check_type_size("int" SIZEOF_INT)
|
|||||||
check_type_size("long" SIZEOF_LONG)
|
check_type_size("long" SIZEOF_LONG)
|
||||||
check_type_size("intmax_t" SIZEOF_INTMAX_T)
|
check_type_size("intmax_t" SIZEOF_INTMAX_T)
|
||||||
check_type_size("off_t" SIZEOF_OFF_T)
|
check_type_size("off_t" SIZEOF_OFF_T)
|
||||||
|
check_type_size("size_t" SIZEOF_SIZE_T)
|
||||||
|
check_type_size("long long" SIZEOF_LONG_LONG)
|
||||||
check_type_size("void *" SIZEOF_VOID_PTR)
|
check_type_size("void *" SIZEOF_VOID_PTR)
|
||||||
|
|
||||||
check_symbol_exists(_NSGetEnviron crt_externs.h HAVE__NSGETENVIRON)
|
check_symbol_exists(_NSGetEnviron crt_externs.h HAVE__NSGETENVIRON)
|
||||||
|
@@ -1159,16 +1159,30 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
|
|||||||
free(tempname);
|
free(tempname);
|
||||||
goto notfound;
|
goto notfound;
|
||||||
}
|
}
|
||||||
fseek(fd, 0L, SEEK_END);
|
int fseek_res = fseek(fd, 0L, SEEK_END);
|
||||||
len = ftell(fd); /* get size of temp file */
|
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);
|
fseek(fd, 0L, SEEK_SET);
|
||||||
buffer = xmalloc(len + 1);
|
buffer = xmalloc(len + 1);
|
||||||
// fread() doesn't terminate buffer with NUL;
|
// fread() doesn't terminate buffer with NUL;
|
||||||
// appropiate termination (not always NUL) is done below.
|
// appropiate termination (not always NUL) is done below.
|
||||||
i = fread((char *)buffer, 1, len, fd);
|
size_t readlen = fread((char *)buffer, 1, len, fd);
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
os_remove((char *)tempname);
|
os_remove((char *)tempname);
|
||||||
if (i != (int)len) {
|
if (readlen != len) {
|
||||||
/* unexpected read error */
|
/* unexpected read error */
|
||||||
EMSG2(_(e_notread), tempname);
|
EMSG2(_(e_notread), tempname);
|
||||||
free(tempname);
|
free(tempname);
|
||||||
|
Reference in New Issue
Block a user