@@ -5352,6 +5352,10 @@ static JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func,
5352
5352
if (!name)
5353
5353
name = "";
5354
5354
name_atom = JS_NewAtom(ctx, name);
5355
+ if (name_atom == JS_ATOM_NULL) {
5356
+ JS_FreeValue(ctx, func_obj);
5357
+ return JS_EXCEPTION;
5358
+ }
5355
5359
js_function_set_properties(ctx, func_obj, name_atom, length);
5356
5360
JS_FreeAtom(ctx, name_atom);
5357
5361
return func_obj;
@@ -6882,20 +6886,27 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_obj,
6882
6886
const char *str1;
6883
6887
JSObject *p;
6884
6888
6889
+ if (!JS_IsObject(error_obj))
6890
+ return; /* protection in the out of memory case */
6891
+
6885
6892
js_dbuf_init(ctx, &dbuf);
6886
6893
if (filename) {
6887
6894
dbuf_printf(&dbuf, " at %s", filename);
6888
6895
if (line_num != -1)
6889
6896
dbuf_printf(&dbuf, ":%d:%d", line_num, col_num);
6890
6897
dbuf_putc(&dbuf, '\n');
6891
6898
str = JS_NewString(ctx, filename);
6899
+ if (JS_IsException(str))
6900
+ return;
6892
6901
/* Note: SpiderMonkey does that, could update once there is a standard */
6893
- JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_fileName, str,
6894
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
6895
- JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_lineNumber, JS_NewInt32(ctx, line_num),
6896
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
6897
- JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_columnNumber, JS_NewInt32(ctx, col_num),
6898
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
6902
+ if (JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_fileName, str,
6903
+ JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0 ||
6904
+ JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_lineNumber, JS_NewInt32(ctx, line_num),
6905
+ JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0 ||
6906
+ JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_columnNumber, JS_NewInt32(ctx, col_num),
6907
+ JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0) {
6908
+ return;
6909
+ }
6899
6910
}
6900
6911
for(sf = ctx->rt->current_stack_frame; sf != NULL; sf = sf->prev_frame) {
6901
6912
if (sf->js_mode & JS_MODE_BACKTRACE_BARRIER)
@@ -6980,9 +6991,9 @@ static JSValue JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num,
6980
6991
JS_DefinePropertyValue(ctx, obj, JS_ATOM_message,
6981
6992
JS_NewString(ctx, buf),
6982
6993
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
6983
- }
6984
- if (add_backtrace) {
6985
- build_backtrace(ctx, obj, NULL, 0, 0, 0);
6994
+ if (add_backtrace) {
6995
+ build_backtrace(ctx, obj, NULL, 0, 0, 0);
6996
+ }
6986
6997
}
6987
6998
ret = JS_Throw(ctx, obj);
6988
6999
return ret;
@@ -8454,6 +8465,8 @@ JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj,
8454
8465
JSAtom atom;
8455
8466
JSValue ret;
8456
8467
atom = JS_NewAtom(ctx, prop);
8468
+ if (atom == JS_ATOM_NULL)
8469
+ return JS_EXCEPTION;
8457
8470
ret = JS_GetProperty(ctx, this_obj, atom);
8458
8471
JS_FreeAtom(ctx, atom);
8459
8472
return ret;
@@ -9273,6 +9286,10 @@ int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj,
9273
9286
JSAtom atom;
9274
9287
int ret;
9275
9288
atom = JS_NewAtom(ctx, prop);
9289
+ if (atom == JS_ATOM_NULL) {
9290
+ JS_FreeValue(ctx, val);
9291
+ return -1;
9292
+ }
9276
9293
ret = JS_SetPropertyInternal(ctx, this_obj, atom, val, this_obj, JS_PROP_THROW);
9277
9294
JS_FreeAtom(ctx, atom);
9278
9295
return ret;
@@ -9829,6 +9846,10 @@ int JS_DefinePropertyValueStr(JSContext *ctx, JSValueConst this_obj,
9829
9846
JSAtom atom;
9830
9847
int ret;
9831
9848
atom = JS_NewAtom(ctx, prop);
9849
+ if (atom == JS_ATOM_NULL) {
9850
+ JS_FreeValue(ctx, val);
9851
+ return -1;
9852
+ }
9832
9853
ret = JS_DefinePropertyValue(ctx, this_obj, atom, val, flags);
9833
9854
JS_FreeAtom(ctx, atom);
9834
9855
return ret;
@@ -20971,6 +20992,7 @@ static __exception int js_parse_template_part(JSParseState *s, const uint8_t *p)
20971
20992
{
20972
20993
uint32_t c;
20973
20994
StringBuffer b_s, *b = &b_s;
20995
+ JSValue str;
20974
20996
20975
20997
/* p points to the first byte of the template part */
20976
20998
if (string_buffer_init(s->ctx, b, 32))
@@ -21013,9 +21035,12 @@ static __exception int js_parse_template_part(JSParseState *s, const uint8_t *p)
21013
21035
if (string_buffer_putc(b, c))
21014
21036
goto fail;
21015
21037
}
21038
+ str = string_buffer_end(b);
21039
+ if (JS_IsException(str))
21040
+ return -1;
21016
21041
s->token.val = TOK_TEMPLATE;
21017
21042
s->token.u.str.sep = c;
21018
- s->token.u.str.str = string_buffer_end(b) ;
21043
+ s->token.u.str.str = str ;
21019
21044
s->buf_ptr = p;
21020
21045
return 0;
21021
21046
@@ -21034,7 +21059,8 @@ static __exception int js_parse_string(JSParseState *s, int sep,
21034
21059
uint32_t c;
21035
21060
StringBuffer b_s, *b = &b_s;
21036
21061
const uint8_t *p_escape;
21037
-
21062
+ JSValue str;
21063
+
21038
21064
/* string */
21039
21065
if (string_buffer_init(s->ctx, b, 32))
21040
21066
goto fail;
@@ -21141,9 +21167,12 @@ static __exception int js_parse_string(JSParseState *s, int sep,
21141
21167
if (string_buffer_putc(b, c))
21142
21168
goto fail;
21143
21169
}
21170
+ str = string_buffer_end(b);
21171
+ if (JS_IsException(str))
21172
+ return -1;
21144
21173
token->val = TOK_STRING;
21145
21174
token->u.str.sep = c;
21146
- token->u.str.str = string_buffer_end(b) ;
21175
+ token->u.str.str = str ;
21147
21176
*pp = p;
21148
21177
return 0;
21149
21178
@@ -21171,6 +21200,7 @@ static __exception int js_parse_regexp(JSParseState *s)
21171
21200
StringBuffer b_s, *b = &b_s;
21172
21201
StringBuffer b2_s, *b2 = &b2_s;
21173
21202
uint32_t c;
21203
+ JSValue body_str, flags_str;
21174
21204
21175
21205
p = s->buf_ptr;
21176
21206
p++;
@@ -21252,9 +21282,17 @@ static __exception int js_parse_regexp(JSParseState *s)
21252
21282
p = p_next;
21253
21283
}
21254
21284
21285
+ body_str = string_buffer_end(b);
21286
+ flags_str = string_buffer_end(b2);
21287
+ if (JS_IsException(body_str) ||
21288
+ JS_IsException(flags_str)) {
21289
+ JS_FreeValue(s->ctx, body_str);
21290
+ JS_FreeValue(s->ctx, flags_str);
21291
+ return -1;
21292
+ }
21255
21293
s->token.val = TOK_REGEXP;
21256
- s->token.u.regexp.body = string_buffer_end(b) ;
21257
- s->token.u.regexp.flags = string_buffer_end(b2) ;
21294
+ s->token.u.regexp.body = body_str ;
21295
+ s->token.u.regexp.flags = flags_str ;
21258
21296
s->buf_ptr = p;
21259
21297
return 0;
21260
21298
fail:
@@ -22362,7 +22400,7 @@ BOOL JS_DetectModule(const char *input, size_t input_len)
22362
22400
}
22363
22401
22364
22402
static inline int get_prev_opcode(JSFunctionDef *fd) {
22365
- if (fd->last_opcode_pos < 0)
22403
+ if (fd->last_opcode_pos < 0 || dbuf_error(&fd->byte_code) )
22366
22404
return OP_invalid;
22367
22405
else
22368
22406
return fd->byte_code.buf[fd->last_opcode_pos];
@@ -22427,7 +22465,11 @@ static void emit_op(JSParseState *s, uint8_t val)
22427
22465
22428
22466
static void emit_atom(JSParseState *s, JSAtom name)
22429
22467
{
22430
- emit_u32(s, JS_DupAtom(s->ctx, name));
22468
+ DynBuf *bc = &s->cur_func->byte_code;
22469
+ if (dbuf_realloc(bc, bc->size + 4))
22470
+ return; /* not enough memory : don't duplicate the atom */
22471
+ put_u32(bc->buf + bc->size, JS_DupAtom(s->ctx, name));
22472
+ bc->size += 4;
22431
22473
}
22432
22474
22433
22475
static int update_label(JSFunctionDef *s, int label, int delta)
@@ -22441,29 +22483,33 @@ static int update_label(JSFunctionDef *s, int label, int delta)
22441
22483
return ls->ref_count;
22442
22484
}
22443
22485
22444
- static int new_label_fd(JSFunctionDef *fd, int label )
22486
+ static int new_label_fd(JSFunctionDef *fd)
22445
22487
{
22488
+ int label;
22446
22489
LabelSlot *ls;
22447
22490
22448
- if (label < 0) {
22449
- if (js_resize_array(fd->ctx, (void *)&fd->label_slots,
22450
- sizeof(fd->label_slots[0]),
22451
- &fd->label_size, fd->label_count + 1))
22452
- return -1;
22453
- label = fd->label_count++;
22454
- ls = &fd->label_slots[label];
22455
- ls->ref_count = 0;
22456
- ls->pos = -1;
22457
- ls->pos2 = -1;
22458
- ls->addr = -1;
22459
- ls->first_reloc = NULL;
22460
- }
22491
+ if (js_resize_array(fd->ctx, (void *)&fd->label_slots,
22492
+ sizeof(fd->label_slots[0]),
22493
+ &fd->label_size, fd->label_count + 1))
22494
+ return -1;
22495
+ label = fd->label_count++;
22496
+ ls = &fd->label_slots[label];
22497
+ ls->ref_count = 0;
22498
+ ls->pos = -1;
22499
+ ls->pos2 = -1;
22500
+ ls->addr = -1;
22501
+ ls->first_reloc = NULL;
22461
22502
return label;
22462
22503
}
22463
22504
22464
22505
static int new_label(JSParseState *s)
22465
22506
{
22466
- return new_label_fd(s->cur_func, -1);
22507
+ int label;
22508
+ label = new_label_fd(s->cur_func);
22509
+ if (unlikely(label < 0)) {
22510
+ dbuf_set_error(&s->cur_func->byte_code);
22511
+ }
22512
+ return label;
22467
22513
}
22468
22514
22469
22515
/* don't update the last opcode and don't emit line number info */
@@ -22491,8 +22537,11 @@ static int emit_label(JSParseState *s, int label)
22491
22537
static int emit_goto(JSParseState *s, int opcode, int label)
22492
22538
{
22493
22539
if (js_is_live_code(s)) {
22494
- if (label < 0)
22540
+ if (label < 0) {
22495
22541
label = new_label(s);
22542
+ if (label < 0)
22543
+ return -1;
22544
+ }
22496
22545
emit_op(s, opcode);
22497
22546
emit_u32(s, label);
22498
22547
s->cur_func->label_slots[label].ref_count++;
@@ -24533,6 +24582,8 @@ static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope,
24533
24582
switch(opcode) {
24534
24583
case OP_scope_get_var:
24535
24584
label = new_label(s);
24585
+ if (label < 0)
24586
+ return -1;
24536
24587
emit_op(s, OP_scope_make_ref);
24537
24588
emit_atom(s, name);
24538
24589
emit_u32(s, label);
@@ -24565,6 +24616,8 @@ static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope,
24565
24616
switch(opcode) {
24566
24617
case OP_scope_get_var:
24567
24618
label = new_label(s);
24619
+ if (label < 0)
24620
+ return -1;
24568
24621
emit_op(s, OP_scope_make_ref);
24569
24622
emit_atom(s, name);
24570
24623
emit_u32(s, label);
@@ -28338,6 +28391,8 @@ JSModuleDef *JS_NewCModule(JSContext *ctx, const char *name_str,
28338
28391
if (name == JS_ATOM_NULL)
28339
28392
return NULL;
28340
28393
m = js_new_module_def(ctx, name);
28394
+ if (!m)
28395
+ return NULL;
28341
28396
m->init_func = func;
28342
28397
return m;
28343
28398
}
@@ -30641,6 +30696,8 @@ static void free_bytecode_atoms(JSRuntime *rt,
30641
30696
case OP_FMT_atom_u16:
30642
30697
case OP_FMT_atom_label_u8:
30643
30698
case OP_FMT_atom_label_u16:
30699
+ if ((pos + 1 + 4) > bc_len)
30700
+ break; /* may happen if there is not enough memory when emiting bytecode */
30644
30701
atom = get_u32(bc_buf + pos + 1);
30645
30702
JS_FreeAtomRT(rt, atom);
30646
30703
break;
@@ -31430,7 +31487,13 @@ static void var_object_test(JSContext *ctx, JSFunctionDef *s,
31430
31487
{
31431
31488
dbuf_putc(bc, get_with_scope_opcode(op));
31432
31489
dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
31433
- *plabel_done = new_label_fd(s, *plabel_done);
31490
+ if (*plabel_done < 0) {
31491
+ *plabel_done = new_label_fd(s);
31492
+ if (*plabel_done < 0) {
31493
+ dbuf_set_error(bc);
31494
+ return;
31495
+ }
31496
+ }
31434
31497
dbuf_put_u32(bc, *plabel_done);
31435
31498
dbuf_putc(bc, is_with);
31436
31499
update_label(s, *plabel_done, 1);
@@ -32475,8 +32538,11 @@ static void instantiate_hoisted_definitions(JSContext *ctx, JSFunctionDef *s, Dy
32475
32538
evaluating the module so that the exported functions are
32476
32539
visible if there are cyclic module references */
32477
32540
if (s->module) {
32478
- label_next = new_label_fd(s, -1);
32479
-
32541
+ label_next = new_label_fd(s);
32542
+ if (label_next < 0) {
32543
+ dbuf_set_error(bc);
32544
+ return;
32545
+ }
32480
32546
/* if 'this' is true, initialize the global variables and return */
32481
32547
dbuf_putc(bc, OP_push_this);
32482
32548
dbuf_putc(bc, OP_if_false);
@@ -38001,17 +38067,22 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj,
38001
38067
return 0;
38002
38068
}
38003
38069
38004
- void JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
38005
- const JSCFunctionListEntry *tab, int len)
38070
+ int JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
38071
+ const JSCFunctionListEntry *tab, int len)
38006
38072
{
38007
- int i;
38073
+ int i, ret ;
38008
38074
38009
38075
for (i = 0; i < len; i++) {
38010
38076
const JSCFunctionListEntry *e = &tab[i];
38011
38077
JSAtom atom = find_atom(ctx, e->name);
38012
- JS_InstantiateFunctionListItem(ctx, obj, atom, e);
38078
+ if (atom == JS_ATOM_NULL)
38079
+ return -1;
38080
+ ret = JS_InstantiateFunctionListItem(ctx, obj, atom, e);
38013
38081
JS_FreeAtom(ctx, atom);
38082
+ if (ret)
38083
+ return -1;
38014
38084
}
38085
+ return 0;
38015
38086
}
38016
38087
38017
38088
int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m,
0 commit comments