Skip to content

Commit 6ee9658

Browse files
committed
patch 8.1.1219: not checking for NULL return from alloc()
Problem: Not checking for NULL return from alloc(). Solution: Add checks. (Martin Kunev, closes #4303, closes #4174)
1 parent 00aa069 commit 6ee9658

14 files changed

Lines changed: 82 additions & 36 deletions

File tree

src/beval.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ get_beval_info(
127127
#ifdef FEAT_VARTABS
128128
vim_free(beval->vts);
129129
beval->vts = tabstop_copy(wp->w_buffer->b_p_vts_array);
130+
if (wp->w_buffer->b_p_vts_array != NULL && beval->vts == NULL)
131+
return FAIL;
130132
#endif
131133
beval->ts = wp->w_buffer->b_p_ts;
132134
return OK;

src/blowfish.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ crypt_blowfish_decode(
636636
}
637637
}
638638

639-
void
639+
int
640640
crypt_blowfish_init(
641641
cryptstate_T *state,
642642
char_u* key,
@@ -647,17 +647,21 @@ crypt_blowfish_init(
647647
{
648648
bf_state_T *bfs = (bf_state_T *)alloc_clear(sizeof(bf_state_T));
649649

650+
if (bfs == NULL)
651+
return FAIL;
650652
state->method_state = bfs;
651653

652654
/* "blowfish" uses a 64 byte buffer, causing it to repeat 8 byte groups 8
653655
* times. "blowfish2" uses a 8 byte buffer to avoid repeating. */
654656
bfs->cfb_len = state->method_nr == CRYPT_M_BF ? BF_MAX_CFB_LEN : BF_BLOCK;
655657

656658
if (blowfish_self_test() == FAIL)
657-
return;
659+
return FAIL;
658660

659661
bf_key_init(bfs, key, salt, salt_len);
660662
bf_cfb_init(bfs, seed, seed_len);
663+
664+
return OK;
661665
}
662666

663667
/*

src/crypt.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef struct {
4343
int (* self_test_fn)();
4444

4545
// Function pointer for initializing encryption/decryption.
46-
void (* init_fn)(cryptstate_T *state, char_u *key,
46+
int (* init_fn)(cryptstate_T *state, char_u *key,
4747
char_u *salt, int salt_len, char_u *seed, int seed_len);
4848

4949
/* Function pointers for encoding/decoding from one buffer into another.
@@ -243,6 +243,7 @@ crypt_self_test(void)
243243

244244
/*
245245
* Allocate a crypt state and initialize it.
246+
* Return NULL for failure.
246247
*/
247248
cryptstate_T *
248249
crypt_create(
@@ -255,8 +256,16 @@ crypt_create(
255256
{
256257
cryptstate_T *state = (cryptstate_T *)alloc((int)sizeof(cryptstate_T));
257258

259+
if (state == NULL)
260+
return state;
261+
258262
state->method_nr = method_nr;
259-
cryptmethods[method_nr].init_fn(state, key, salt, salt_len, seed, seed_len);
263+
if (cryptmethods[method_nr].init_fn(
264+
state, key, salt, salt_len, seed, seed_len) == FAIL)
265+
{
266+
vim_free(state);
267+
return NULL;
268+
}
260269
return state;
261270
}
262271

src/crypt_zip.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ make_crc_tab(void)
7878
/*
7979
* Initialize for encryption/decryption.
8080
*/
81-
void
81+
int
8282
crypt_zip_init(
8383
cryptstate_T *state,
8484
char_u *key,
@@ -91,6 +91,8 @@ crypt_zip_init(
9191
zip_state_T *zs;
9292

9393
zs = (zip_state_T *)alloc(sizeof(zip_state_T));
94+
if (zs == NULL)
95+
return FAIL;
9496
state->method_state = zs;
9597

9698
make_crc_tab();
@@ -99,6 +101,8 @@ crypt_zip_init(
99101
zs->keys[2] = 878082192L;
100102
for (p = key; *p != NUL; ++p)
101103
UPDATE_KEYS_ZIP(zs->keys, (int)*p);
104+
105+
return OK;
102106
}
103107

104108
/*

src/gui_gtk_f.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ gtk_form_put(GtkForm *form,
130130

131131
/* LINTED: avoid warning: conversion to 'unsigned long' */
132132
child = g_new(GtkFormChild, 1);
133+
if (child == NULL)
134+
return;
133135

134136
child->widget = child_widget;
135137
child->window = NULL;

src/gui_gtk_x11.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,12 +1576,15 @@ selection_get_cb(GtkWidget *widget UNUSED,
15761576
if (string != NULL)
15771577
{
15781578
tmpbuf = alloc(length + 2);
1579-
tmpbuf[0] = 0xff;
1580-
tmpbuf[1] = 0xfe;
1581-
mch_memmove(tmpbuf + 2, string, (size_t)length);
1582-
vim_free(string);
1583-
string = tmpbuf;
1584-
length += 2;
1579+
if (tmpbuf != NULL)
1580+
{
1581+
tmpbuf[0] = 0xff;
1582+
tmpbuf[1] = 0xfe;
1583+
mch_memmove(tmpbuf + 2, string, (size_t)length);
1584+
vim_free(string);
1585+
string = tmpbuf;
1586+
length += 2;
1587+
}
15851588

15861589
#if !GTK_CHECK_VERSION(3,0,0)
15871590
/* Looks redundant even for GTK2 because these values are
@@ -1606,10 +1609,10 @@ selection_get_cb(GtkWidget *widget UNUSED,
16061609
tmpbuf[0] = motion_type;
16071610
STRCPY(tmpbuf + 1, p_enc);
16081611
mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1612+
length += l + 2;
1613+
vim_free(string);
1614+
string = tmpbuf;
16091615
}
1610-
length += l + 2;
1611-
vim_free(string);
1612-
string = tmpbuf;
16131616
type = vimenc_atom;
16141617
}
16151618

src/libvterm/src/state.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ static int on_text(const char bytes[], size_t len, void *user)
253253
// We'll have at most len codepoints, plus one from a previous incomplete
254254
// sequence.
255255
codepoints = vterm_allocator_malloc(state->vt, (len + 1) * sizeof(uint32_t));
256+
if (codepoints == NULL)
257+
return 0;
256258

257259
encoding =
258260
state->gsingle_set ? &state->encoding[state->gsingle_set] :
@@ -330,6 +332,8 @@ static int on_text(const char bytes[], size_t len, void *user)
330332
break;
331333

332334
chars = vterm_allocator_malloc(state->vt, (glyph_ends - glyph_starts + 1) * sizeof(uint32_t));
335+
if (chars == NULL)
336+
break;
333337

334338
for( ; i < glyph_ends; i++) {
335339
int this_width;
@@ -1626,6 +1630,8 @@ static int on_resize(int rows, int cols, void *user)
16261630

16271631
if(cols != state->cols) {
16281632
unsigned char *newtabstops = vterm_allocator_malloc(state->vt, (cols + 7) / 8);
1633+
if (newtabstops == NULL)
1634+
return 0;
16291635

16301636
/* TODO: This can all be done much more efficiently bytewise */
16311637
int col;
@@ -1651,6 +1657,8 @@ static int on_resize(int rows, int cols, void *user)
16511657

16521658
if(rows != state->rows) {
16531659
VTermLineInfo *newlineinfo = vterm_allocator_malloc(state->vt, rows * sizeof(VTermLineInfo));
1660+
if (newlineinfo == NULL)
1661+
return 0;
16541662

16551663
int row;
16561664
for(row = 0; row < state->rows && row < rows; row++) {

src/libvterm/src/termscreen.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ static ScreenCell *realloc_buffer(VTermScreen *screen, ScreenCell *buffer, int n
8383
ScreenCell *new_buffer = vterm_allocator_malloc(screen->vt, sizeof(ScreenCell) * new_rows * new_cols);
8484
int row, col;
8585

86+
if (new_buffer == NULL)
87+
return NULL;
8688
for(row = 0; row < new_rows; row++) {
8789
for(col = 0; col < new_cols; col++) {
8890
ScreenCell *new_cell = new_buffer + row*new_cols + col;

src/ops.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6170,21 +6170,25 @@ handle_viminfo_register(garray_T *values, int force)
61706170
y_ptr->y_size = linecount;
61716171
y_ptr->y_time_set = timestamp;
61726172
if (linecount == 0)
6173+
{
61736174
y_ptr->y_array = NULL;
6174-
else
6175+
return;
6176+
}
6177+
y_ptr->y_array = (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6178+
if (y_ptr->y_array == NULL)
6179+
{
6180+
y_ptr->y_size = 0; // ensure object state is consistent
6181+
return;
6182+
}
6183+
for (i = 0; i < linecount; i++)
61756184
{
6176-
y_ptr->y_array =
6177-
(char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6178-
for (i = 0; i < linecount; i++)
6185+
if (vp[i + 6].bv_allocated)
61796186
{
6180-
if (vp[i + 6].bv_allocated)
6181-
{
6182-
y_ptr->y_array[i] = vp[i + 6].bv_string;
6183-
vp[i + 6].bv_string = NULL;
6184-
}
6185-
else
6186-
y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6187+
y_ptr->y_array[i] = vp[i + 6].bv_string;
6188+
vp[i + 6].bv_string = NULL;
61876189
}
6190+
else
6191+
y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
61886192
}
61896193
}
61906194

src/option.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13011,13 +13011,12 @@ tabstop_copy(int *oldts)
1301113011
int *newts;
1301213012
int t;
1301313013

13014-
if (oldts == 0)
13015-
return 0;
13016-
13017-
newts = (int *) alloc((unsigned) ((oldts[0] + 1) * sizeof(int)));
13018-
for (t = 0; t <= oldts[0]; ++t)
13019-
newts[t] = oldts[t];
13020-
13014+
if (oldts == NULL)
13015+
return NULL;
13016+
newts = (int *)alloc((unsigned)((oldts[0] + 1) * sizeof(int)));
13017+
if (newts != NULL)
13018+
for (t = 0; t <= oldts[0]; ++t)
13019+
newts[t] = oldts[t];
1302113020
return newts;
1302213021
}
1302313022
#endif

0 commit comments

Comments
 (0)