Skip to content

Commit 964b7b5

Browse files
mattnchrisbra
authored andcommitted
patch 9.2.0283: unnecessary (int) casts before alloc() calls
Problem: unnecessary (int) casts before alloc() calls, can cause truncation and heap overflows (sgInnora) Solution: Remove casts (Yasuhiro Matsumoto) alloc() already accepts size_t, so (int) casts on size_t values are redundant and could theoretically cause truncation on values > INT_MAX. Remove the casts and change alloc_cmdbuff() signature from int to size_t to match. Note: list_alloc_with_items() keeps its int parameter since lv_len and lv_with_items are int, and the call site already has an INT_MAX guard. fixes: #19888 closes: #19889 Signed-off-by: Yasuhiro Matsumoto <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 0646047 commit 964b7b5

7 files changed

Lines changed: 12 additions & 10 deletions

File tree

src/ex_getln.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void set_cmdspos(void);
4242
static void set_cmdspos_cursor(void);
4343
static void correct_cmdspos(int idx, int cells);
4444
static void dealloc_cmdbuff(void);
45-
static void alloc_cmdbuff(int len);
45+
static void alloc_cmdbuff(size_t len);
4646
static void draw_cmdline(int start, int len);
4747
static void save_cmdline(cmdline_info_T *ccp);
4848
static void restore_cmdline(cmdline_info_T *ccp);
@@ -1537,7 +1537,7 @@ cmdline_browse_history(
15371537
}
15381538
if (i == 0)
15391539
{
1540-
alloc_cmdbuff((int)len);
1540+
alloc_cmdbuff(len);
15411541
if (ccline.cmdbuff == NULL)
15421542
{
15431543
res = GOTO_NORMAL_MODE;
@@ -1550,7 +1550,7 @@ cmdline_browse_history(
15501550
}
15511551
else
15521552
{
1553-
alloc_cmdbuff((int)plen);
1553+
alloc_cmdbuff(plen);
15541554
if (ccline.cmdbuff == NULL)
15551555
{
15561556
res = GOTO_NORMAL_MODE;
@@ -3491,7 +3491,7 @@ dealloc_cmdbuff(void)
34913491
* Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
34923492
*/
34933493
static void
3494-
alloc_cmdbuff(int len)
3494+
alloc_cmdbuff(size_t len)
34953495
{
34963496
/*
34973497
* give some extra space to avoid having to allocate all the time
@@ -3502,7 +3502,7 @@ alloc_cmdbuff(int len)
35023502
len += 20;
35033503

35043504
ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
3505-
ccline.cmdbufflen = len;
3505+
ccline.cmdbufflen = (int)len;
35063506
}
35073507

35083508
/*

src/memline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3674,7 +3674,7 @@ ml_replace_len(
36743674
size_t textproplen = curbuf->b_ml.ml_line_len - oldtextlen;
36753675

36763676
// Need to copy over text properties, stored after the text.
3677-
newline = alloc(len + (int)textproplen);
3677+
newline = alloc(len + textproplen);
36783678
if (newline != NULL)
36793679
{
36803680
mch_memmove(newline, line, len);

src/popupwin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5896,7 +5896,7 @@ popup_set_title(win_T *wp)
58965896

58975897
vim_free(wp->w_popup_title);
58985898
len = STRLEN(wp->w_buffer->b_fname) + 3;
5899-
wp->w_popup_title = alloc((int)len);
5899+
wp->w_popup_title = alloc(len);
59005900
if (wp->w_popup_title != NULL)
59015901
vim_snprintf((char *)wp->w_popup_title, len, " %s ",
59025902
wp->w_buffer->b_fname);

src/session.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ write_session_file(char_u *filename)
10991099
escaped_filename = vim_strsave_escaped(filename, escape_chars);
11001100
if (escaped_filename == NULL)
11011101
return FALSE;
1102-
mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1);
1102+
mksession_cmdline = alloc(10 + STRLEN(escaped_filename) + 1);
11031103
if (mksession_cmdline == NULL)
11041104
{
11051105
vim_free(escaped_filename);

src/terminal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5697,7 +5697,7 @@ get_separator(int text_width, char_u *fname)
56975697
int i;
56985698
size_t off;
56995699

5700-
textline = alloc(width + (int)STRLEN(fname) + 1);
5700+
textline = alloc(width + STRLEN(fname) + 1);
57015701
if (textline == NULL)
57025702
return NULL;
57035703

src/version.c

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

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
283,
737739
/**/
738740
282,
739741
/**/

src/vim9expr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3355,7 +3355,7 @@ compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
33553355
char_u *s2 = tv2->vval.v_string;
33563356
size_t len1 = STRLEN(s1);
33573357

3358-
tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3358+
tv1->vval.v_string = alloc(len1 + STRLEN(s2) + 1);
33593359
if (tv1->vval.v_string == NULL)
33603360
{
33613361
clear_ppconst(ppconst);

0 commit comments

Comments
 (0)