Skip to content

Commit 220474d

Browse files
committed
patch 9.1.0608: Coverity warns about a few potential issues
Problem: Coverity warns about a few potential issues Solution: Fix those issues (see details below) 1) Fix overflow warning in highlight.c This happens because we are comparing int with long and assign a potential long value to an int, which could cause an overflow. So add some casts to ensure the value fits into an int. 2) Fix Overflow warning in shift_line(). This happens because we are performing a division/modulo operation of a long type by an int type and assign the result to an int, which could then overflow. So before performing the operation, trim the long to value to at most max int value, so that it can't overflow. 3) Fix overflow warning in syn_list_cluster in syntax.c This is essential the same issue as 1) 4) not checking the return value of vim_mkdir() in spellfile.c Creating the spell directory could fail. Handle this case and return early in this case. 5) qsort() may deref a NULL pointer when fuzzy match does not return a result. Fix this by checking that the accessed growarray fuzzy_indices actually contains data. If not we can silently skip the qsort() and related logic. closes: #15284 Signed-off-by: Christian Brabandt <[email protected]>
1 parent 4aa6b52 commit 220474d

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

src/highlight.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,8 +3351,8 @@ syn_list_header(
33513351

33523352
if (msg_col >= endcol) // output at least one space
33533353
endcol = msg_col + 1;
3354-
if (Columns <= endcol) // avoid hang for tiny window
3355-
endcol = Columns - 1;
3354+
if (Columns <= (long)endcol) // avoid hang for tiny window
3355+
endcol = (int)(Columns - 1);
33563356

33573357
msg_advance(endcol);
33583358

src/insexpand.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,16 +3618,21 @@ get_next_filename_completion(void)
36183618
}
36193619
}
36203620

3621-
fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3622-
qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
3621+
// prevent qsort from deref NULL pointer
3622+
if (fuzzy_indices.ga_len > 0)
3623+
{
3624+
fuzzy_indices_data = (int *)fuzzy_indices.ga_data;
3625+
qsort(fuzzy_indices_data, fuzzy_indices.ga_len, sizeof(int), compare_scores);
3626+
3627+
sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3628+
for (i = 0; i < fuzzy_indices.ga_len; ++i)
3629+
sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
36233630

3624-
sorted_matches = (char_u **)alloc(sizeof(char_u *) * fuzzy_indices.ga_len);
3625-
for (i = 0; i < fuzzy_indices.ga_len; ++i)
3626-
sorted_matches[i] = vim_strsave(matches[fuzzy_indices_data[i]]);
3631+
FreeWild(num_matches, matches);
3632+
matches = sorted_matches;
3633+
num_matches = fuzzy_indices.ga_len;
3634+
}
36273635

3628-
FreeWild(num_matches, matches);
3629-
matches = sorted_matches;
3630-
num_matches = fuzzy_indices.ga_len;
36313636
vim_free(compl_fuzzy_scores);
36323637
ga_clear(&fuzzy_indices);
36333638
}

src/ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ shift_line(
240240

241241
if (round) // round off indent
242242
{
243-
i = count / sw_val; // number of 'shiftwidth' rounded down
244-
j = count % sw_val; // extra spaces
243+
i = trim_to_int(count) / sw_val; // number of 'shiftwidth' rounded down
244+
j = trim_to_int(count) % sw_val; // extra spaces
245245
if (j && left) // first remove extra spaces
246246
--amount;
247247
if (left)

src/spellfile.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6434,7 +6434,13 @@ init_spellfile(void)
64346434
l = (int)STRLEN(buf);
64356435
vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
64366436
if (filewritable(buf) != 2)
6437-
vim_mkdir(buf, 0755);
6437+
{
6438+
if (vim_mkdir(buf, 0755) != 0)
6439+
{
6440+
vim_free(buf);
6441+
return;
6442+
}
6443+
}
64386444

64396445
l = (int)STRLEN(buf);
64406446
vim_snprintf((char *)buf + l, MAXPATHL - l,

src/syntax.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,8 +4084,8 @@ syn_list_cluster(int id)
40844084

40854085
if (msg_col >= endcol) // output at least one space
40864086
endcol = msg_col + 1;
4087-
if (Columns <= endcol) // avoid hang for tiny window
4088-
endcol = Columns - 1;
4087+
if (Columns <= (long)endcol) // avoid hang for tiny window
4088+
endcol = (int)(Columns - 1);
40894089

40904090
msg_advance(endcol);
40914091
if (SYN_CLSTR(curwin->w_s)[id].scl_list != NULL)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
608,
707709
/**/
708710
607,
709711
/**/

0 commit comments

Comments
 (0)