@@ -5287,6 +5287,10 @@ JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func,
5287
5287
if (!name)
5288
5288
name = "";
5289
5289
name_atom = JS_NewAtom(ctx, name);
5290
+ if (name_atom == JS_ATOM_NULL) {
5291
+ JS_FreeValue(ctx, func_obj);
5292
+ return JS_EXCEPTION;
5293
+ }
5290
5294
js_function_set_properties(ctx, func_obj, name_atom, length);
5291
5295
JS_FreeAtom(ctx, name_atom);
5292
5296
return func_obj;
@@ -8489,6 +8493,8 @@ JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj,
8489
8493
JSAtom atom;
8490
8494
JSValue ret;
8491
8495
atom = JS_NewAtom(ctx, prop);
8496
+ if (atom == JS_ATOM_NULL)
8497
+ return JS_EXCEPTION;
8492
8498
ret = JS_GetProperty(ctx, this_obj, atom);
8493
8499
JS_FreeAtom(ctx, atom);
8494
8500
return ret;
@@ -9276,6 +9282,10 @@ int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj,
9276
9282
JSAtom atom;
9277
9283
int ret;
9278
9284
atom = JS_NewAtom(ctx, prop);
9285
+ if (atom == JS_ATOM_NULL) {
9286
+ JS_FreeValue(ctx, val);
9287
+ return -1;
9288
+ }
9279
9289
ret = JS_SetPropertyInternal(ctx, this_obj, atom, val, JS_PROP_THROW);
9280
9290
JS_FreeAtom(ctx, atom);
9281
9291
return ret;
@@ -9827,6 +9837,10 @@ int JS_DefinePropertyValueStr(JSContext *ctx, JSValueConst this_obj,
9827
9837
JSAtom atom;
9828
9838
int ret;
9829
9839
atom = JS_NewAtom(ctx, prop);
9840
+ if (atom == JS_ATOM_NULL) {
9841
+ JS_FreeValue(ctx, val);
9842
+ return -1;
9843
+ }
9830
9844
ret = JS_DefinePropertyValue(ctx, this_obj, atom, val, flags);
9831
9845
JS_FreeAtom(ctx, atom);
9832
9846
return ret;
@@ -20585,6 +20599,7 @@ static __exception int js_parse_template_part(JSParseState *s,
20585
20599
const uint8_t *p_next;
20586
20600
uint32_t c;
20587
20601
StringBuffer b_s, *b = &b_s;
20602
+ JSValue str;
20588
20603
20589
20604
/* p points to the first byte of the template part */
20590
20605
if (string_buffer_init(s->ctx, b, 32))
@@ -20630,9 +20645,12 @@ static __exception int js_parse_template_part(JSParseState *s,
20630
20645
if (string_buffer_putc(b, c))
20631
20646
goto fail;
20632
20647
}
20648
+ str = string_buffer_end(b);
20649
+ if (JS_IsException(str))
20650
+ return -1;
20633
20651
s->token.val = TOK_TEMPLATE;
20634
20652
s->token.u.str.sep = c;
20635
- s->token.u.str.str = string_buffer_end(b) ;
20653
+ s->token.u.str.str = str ;
20636
20654
s->buf_ptr = p;
20637
20655
return 0;
20638
20656
@@ -20651,6 +20669,7 @@ static __exception int js_parse_string(JSParseState *s, int sep,
20651
20669
int ret;
20652
20670
uint32_t c;
20653
20671
StringBuffer b_s, *b = &b_s;
20672
+ JSValue str;
20654
20673
20655
20674
/* string */
20656
20675
if (string_buffer_init(s->ctx, b, 32))
@@ -20759,9 +20778,12 @@ static __exception int js_parse_string(JSParseState *s, int sep,
20759
20778
if (string_buffer_putc(b, c))
20760
20779
goto fail;
20761
20780
}
20781
+ str = string_buffer_end(b);
20782
+ if (JS_IsException(str))
20783
+ return -1;
20762
20784
token->val = TOK_STRING;
20763
20785
token->u.str.sep = c;
20764
- token->u.str.str = string_buffer_end(b) ;
20786
+ token->u.str.str = str ;
20765
20787
*pp = p;
20766
20788
return 0;
20767
20789
@@ -20789,6 +20811,7 @@ static __exception int js_parse_regexp(JSParseState *s)
20789
20811
StringBuffer b_s, *b = &b_s;
20790
20812
StringBuffer b2_s, *b2 = &b2_s;
20791
20813
uint32_t c;
20814
+ JSValue body_str, flags_str;
20792
20815
20793
20816
p = s->buf_ptr;
20794
20817
p++;
@@ -20861,9 +20884,17 @@ static __exception int js_parse_regexp(JSParseState *s)
20861
20884
p = p_next;
20862
20885
}
20863
20886
20887
+ body_str = string_buffer_end(b);
20888
+ flags_str = string_buffer_end(b2);
20889
+ if (JS_IsException(body_str) ||
20890
+ JS_IsException(flags_str)) {
20891
+ JS_FreeValue(s->ctx, body_str);
20892
+ JS_FreeValue(s->ctx, flags_str);
20893
+ return -1;
20894
+ }
20864
20895
s->token.val = TOK_REGEXP;
20865
- s->token.u.regexp.body = string_buffer_end(b) ;
20866
- s->token.u.regexp.flags = string_buffer_end(b2) ;
20896
+ s->token.u.regexp.body = body_str ;
20897
+ s->token.u.regexp.flags = flags_str ;
20867
20898
s->buf_ptr = p;
20868
20899
return 0;
20869
20900
fail:
@@ -21893,7 +21924,7 @@ static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end)
21893
21924
}
21894
21925
21895
21926
static inline int get_prev_opcode(JSFunctionDef *fd) {
21896
- if (fd->last_opcode_pos < 0)
21927
+ if (fd->last_opcode_pos < 0 || dbuf_error(&fd->byte_code) )
21897
21928
return OP_invalid;
21898
21929
else
21899
21930
return fd->byte_code.buf[fd->last_opcode_pos];
@@ -21954,7 +21985,11 @@ static void emit_op(JSParseState *s, uint8_t val)
21954
21985
21955
21986
static void emit_atom(JSParseState *s, JSAtom name)
21956
21987
{
21957
- emit_u32(s, JS_DupAtom(s->ctx, name));
21988
+ DynBuf *bc = &s->cur_func->byte_code;
21989
+ if (dbuf_realloc(bc, bc->size + 4))
21990
+ return; /* not enough memory : don't duplicate the atom */
21991
+ put_u32(bc->buf + bc->size, JS_DupAtom(s->ctx, name));
21992
+ bc->size += 4;
21958
21993
}
21959
21994
21960
21995
static int update_label(JSFunctionDef *s, int label, int delta)
@@ -21968,29 +22003,33 @@ static int update_label(JSFunctionDef *s, int label, int delta)
21968
22003
return ls->ref_count;
21969
22004
}
21970
22005
21971
- static int new_label_fd(JSFunctionDef *fd, int label )
22006
+ static int new_label_fd(JSFunctionDef *fd)
21972
22007
{
22008
+ int label;
21973
22009
LabelSlot *ls;
21974
22010
21975
- if (label < 0) {
21976
- if (js_resize_array(fd->ctx, (void *)&fd->label_slots,
22011
+ if (js_resize_array(fd->ctx, (void *)&fd->label_slots,
21977
22012
sizeof(fd->label_slots[0]),
21978
22013
&fd->label_size, fd->label_count + 1))
21979
- return -1;
21980
- label = fd->label_count++;
21981
- ls = &fd->label_slots[label];
21982
- ls->ref_count = 0;
21983
- ls->pos = -1;
21984
- ls->pos2 = -1;
21985
- ls->addr = -1;
21986
- ls->first_reloc = NULL;
21987
- }
22014
+ return -1;
22015
+ label = fd->label_count++;
22016
+ ls = &fd->label_slots[label];
22017
+ ls->ref_count = 0;
22018
+ ls->pos = -1;
22019
+ ls->pos2 = -1;
22020
+ ls->addr = -1;
22021
+ ls->first_reloc = NULL;
21988
22022
return label;
21989
22023
}
21990
22024
21991
22025
static int new_label(JSParseState *s)
21992
22026
{
21993
- return new_label_fd(s->cur_func, -1);
22027
+ int label;
22028
+ label = new_label_fd(s->cur_func);
22029
+ if (unlikely(label < 0)) {
22030
+ dbuf_set_error(&s->cur_func->byte_code);
22031
+ }
22032
+ return label;
21994
22033
}
21995
22034
21996
22035
/* don't update the last opcode and don't emit line number info */
@@ -22018,8 +22057,11 @@ static int emit_label(JSParseState *s, int label)
22018
22057
static int emit_goto(JSParseState *s, int opcode, int label)
22019
22058
{
22020
22059
if (js_is_live_code(s)) {
22021
- if (label < 0)
22060
+ if (label < 0) {
22022
22061
label = new_label(s);
22062
+ if (label < 0)
22063
+ return -1;
22064
+ }
22023
22065
emit_op(s, opcode);
22024
22066
emit_u32(s, label);
22025
22067
s->cur_func->label_slots[label].ref_count++;
@@ -24154,6 +24196,8 @@ static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope,
24154
24196
switch(opcode) {
24155
24197
case OP_scope_get_var:
24156
24198
label = new_label(s);
24199
+ if (label < 0)
24200
+ return -1;
24157
24201
emit_op(s, OP_scope_make_ref);
24158
24202
emit_atom(s, name);
24159
24203
emit_u32(s, label);
@@ -24189,6 +24233,8 @@ static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope,
24189
24233
switch(opcode) {
24190
24234
case OP_scope_get_var:
24191
24235
label = new_label(s);
24236
+ if (label < 0)
24237
+ return -1;
24192
24238
emit_op(s, OP_scope_make_ref);
24193
24239
emit_atom(s, name);
24194
24240
emit_u32(s, label);
@@ -27894,6 +27940,8 @@ JSModuleDef *JS_NewCModule(JSContext *ctx, const char *name_str,
27894
27940
if (name == JS_ATOM_NULL)
27895
27941
return NULL;
27896
27942
m = js_new_module_def(ctx, name);
27943
+ if (!m)
27944
+ return NULL;
27897
27945
m->init_func = func;
27898
27946
return m;
27899
27947
}
@@ -29997,6 +30045,8 @@ static void free_bytecode_atoms(JSRuntime *rt,
29997
30045
case OP_FMT_atom_u16:
29998
30046
case OP_FMT_atom_label_u8:
29999
30047
case OP_FMT_atom_label_u16:
30048
+ if ((pos + 1 + 4) > bc_len)
30049
+ break; /* may happen if there is not enough memory when emiting bytecode */
30000
30050
atom = get_u32(bc_buf + pos + 1);
30001
30051
JS_FreeAtomRT(rt, atom);
30002
30052
break;
@@ -30814,7 +30864,13 @@ static void var_object_test(JSContext *ctx, JSFunctionDef *s,
30814
30864
{
30815
30865
dbuf_putc(bc, get_with_scope_opcode(op));
30816
30866
dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
30817
- *plabel_done = new_label_fd(s, *plabel_done);
30867
+ if (*plabel_done < 0) {
30868
+ *plabel_done = new_label_fd(s);
30869
+ if (*plabel_done < 0) {
30870
+ dbuf_set_error(bc);
30871
+ return;
30872
+ }
30873
+ }
30818
30874
dbuf_put_u32(bc, *plabel_done);
30819
30875
dbuf_putc(bc, is_with);
30820
30876
update_label(s, *plabel_done, 1);
@@ -31858,7 +31914,11 @@ static void instantiate_hoisted_definitions(JSContext *ctx, JSFunctionDef *s, Dy
31858
31914
evaluating the module so that the exported functions are
31859
31915
visible if there are cyclic module references */
31860
31916
if (s->module) {
31861
- label_next = new_label_fd(s, -1);
31917
+ label_next = new_label_fd(s);
31918
+ if (label_next < 0) {
31919
+ dbuf_set_error(bc);
31920
+ return;
31921
+ }
31862
31922
31863
31923
/* if 'this' is true, initialize the global variables and return */
31864
31924
dbuf_putc(bc, OP_push_this);
@@ -37578,17 +37638,22 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj,
37578
37638
return 0;
37579
37639
}
37580
37640
37581
- void JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
37641
+ int JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
37582
37642
const JSCFunctionListEntry *tab, int len)
37583
37643
{
37584
- int i;
37644
+ int i, ret ;
37585
37645
37586
37646
for (i = 0; i < len; i++) {
37587
37647
const JSCFunctionListEntry *e = &tab[i];
37588
37648
JSAtom atom = find_atom(ctx, e->name);
37589
- JS_InstantiateFunctionListItem(ctx, obj, atom, e);
37649
+ if (atom == JS_ATOM_NULL)
37650
+ return -1;
37651
+ ret = JS_InstantiateFunctionListItem(ctx, obj, atom, e);
37590
37652
JS_FreeAtom(ctx, atom);
37653
+ if (ret)
37654
+ return -1;
37591
37655
}
37656
+ return 0;
37592
37657
}
37593
37658
37594
37659
int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m,
@@ -42989,7 +43054,9 @@ static JSValue js_string_constructor(JSContext *ctx, JSValueConst new_target,
42989
43054
JSString *p1 = JS_VALUE_GET_STRING(val);
42990
43055
42991
43056
obj = js_create_from_ctor(ctx, new_target, JS_CLASS_STRING);
42992
- if (!JS_IsException(obj)) {
43057
+ if (JS_IsException(obj)) {
43058
+ JS_FreeValue(ctx, val);
43059
+ } else {
42993
43060
JS_SetObjectData(ctx, obj, val);
42994
43061
JS_DefinePropertyValue(ctx, obj, JS_ATOM_length, js_int32(p1->len), 0);
42995
43062
}
0 commit comments