Skip to content

Commit 9fe17d4

Browse files
zdohnalbrammool
authored andcommitted
patch 8.2.3290: Vim9: compiling dict may use pointer after free
Problem: Vim9: compiling dict may use pointer after free and leak memory on failure. Solution: Pass a pointer to generate_PUSHS(). (Zdenek Dohnal, closes #8699)
1 parent f18e8a9 commit 9fe17d4

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3290,
758760
/**/
759761
3289,
760762
/**/

src/vim9compile.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,21 +1172,26 @@ generate_PUSHF(cctx_T *cctx, float_T fnumber)
11721172

11731173
/*
11741174
* Generate an ISN_PUSHS instruction.
1175-
* Consumes "str".
1175+
* Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
11761176
*/
11771177
static int
1178-
generate_PUSHS(cctx_T *cctx, char_u *str)
1178+
generate_PUSHS(cctx_T *cctx, char_u **str)
11791179
{
11801180
isn_T *isn;
11811181

11821182
if (cctx->ctx_skip == SKIP_YES)
11831183
{
1184-
vim_free(str);
1184+
if (str != NULL)
1185+
VIM_CLEAR(*str);
11851186
return OK;
11861187
}
11871188
if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1189+
{
1190+
if (str != NULL)
1191+
VIM_CLEAR(*str);
11881192
return FAIL;
1189-
isn->isn_arg.string = str;
1193+
}
1194+
isn->isn_arg.string = str == NULL ? NULL : *str;
11901195

11911196
return OK;
11921197
}
@@ -2785,7 +2790,7 @@ generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
27852790
tv->vval.v_blob = NULL;
27862791
break;
27872792
case VAR_STRING:
2788-
generate_PUSHS(cctx, tv->vval.v_string);
2793+
generate_PUSHS(cctx, &tv->vval.v_string);
27892794
tv->vval.v_string = NULL;
27902795
break;
27912796
default:
@@ -3837,7 +3842,7 @@ compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
38373842
key = get_literal_key(arg);
38383843
if (key == NULL)
38393844
return FAIL;
3840-
if (generate_PUSHS(cctx, key) == FAIL)
3845+
if (generate_PUSHS(cctx, &key) == FAIL)
38413846
return FAIL;
38423847
}
38433848

@@ -6525,7 +6530,7 @@ compile_assign_index(
65256530
char_u *key_end = to_name_end(p + 1, TRUE);
65266531
char_u *key = vim_strnsave(p + 1, key_end - p - 1);
65276532

6528-
r = generate_PUSHS(cctx, key);
6533+
r = generate_PUSHS(cctx, &key);
65296534
}
65306535
return r;
65316536
}
@@ -6811,7 +6816,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
68116816
// Push each line and the create the list.
68126817
FOR_ALL_LIST_ITEMS(l, li)
68136818
{
6814-
generate_PUSHS(cctx, li->li_tv.vval.v_string);
6819+
generate_PUSHS(cctx, &li->li_tv.vval.v_string);
68156820
li->li_tv.vval.v_string = NULL;
68166821
}
68176822
generate_NEWLIST(cctx, l->lv_len);
@@ -8520,7 +8525,7 @@ compile_catch(char_u *arg, cctx_T *cctx UNUSED)
85208525
p += len + 2 + dropped;
85218526
if (pat == NULL)
85228527
return FAIL;
8523-
if (generate_PUSHS(cctx, pat) == FAIL)
8528+
if (generate_PUSHS(cctx, &pat) == FAIL)
85248529
return FAIL;
85258530

85268531
if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
@@ -9008,7 +9013,9 @@ compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
90089013
{
90099014
if (p > start)
90109015
{
9011-
generate_PUSHS(cctx, vim_strnsave(start, p - start));
9016+
char_u *val = vim_strnsave(start, p - start);
9017+
9018+
generate_PUSHS(cctx, &val);
90129019
++count;
90139020
}
90149021
p += 2;
@@ -9029,7 +9036,9 @@ compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
90299036
{
90309037
if (*skipwhite(start) != NUL)
90319038
{
9032-
generate_PUSHS(cctx, vim_strsave(start));
9039+
char_u *val = vim_strsave(start);
9040+
9041+
generate_PUSHS(cctx, &val);
90339042
++count;
90349043
}
90359044
break;
@@ -9847,6 +9856,7 @@ compile_def_function(
98479856
case CMD_execute:
98489857
case CMD_echomsg:
98499858
case CMD_echoerr:
9859+
// TODO: "echoconsole"
98509860
line = compile_mult_expr(p, ea.cmdidx, &cctx);
98519861
break;
98529862

@@ -9885,8 +9895,6 @@ compile_def_function(
98859895
#endif
98869896
break;
98879897

9888-
// TODO: any other commands with an expression argument?
9889-
98909898
case CMD_append:
98919899
case CMD_change:
98929900
case CMD_insert:

0 commit comments

Comments
 (0)