@@ -3012,19 +3012,22 @@ static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size,
3012
3012
{
3013
3013
if (__JS_AtomIsTaggedInt(atom)) {
3014
3014
snprintf(buf, buf_size, "%u", __JS_AtomToUInt32(atom));
3015
- } else {
3016
- JSAtomStruct *p;
3015
+ } else if (atom == JS_ATOM_NULL) {
3016
+ snprintf(buf, buf_size, "<null>");
3017
+ } else if (atom >= rt->atom_size) {
3017
3018
assert(atom < rt->atom_size);
3018
- if (atom == JS_ATOM_NULL) {
3019
- snprintf(buf, buf_size, "<null>");
3019
+ snprintf(buf, buf_size, "<invalid %x>", atom);
3020
+ } else {
3021
+ JSAtomStruct *p = rt->atom_array[atom];
3022
+ if (atom_is_free(p)) {
3023
+ assert(!atom_is_free(p));
3024
+ snprintf(buf, buf_size, "<free %x>", atom);
3020
3025
} else {
3021
3026
int i, c;
3022
3027
char *q;
3023
3028
JSString *str;
3024
3029
3025
3030
q = buf;
3026
- p = rt->atom_array[atom];
3027
- assert(!atom_is_free(p));
3028
3031
str = p;
3029
3032
if (str) {
3030
3033
if (!str->is_wide_char) {
@@ -5454,12 +5457,16 @@ void __JS_FreeValueRT(JSRuntime *rt, JSValue v)
5454
5457
5455
5458
#ifdef DUMP_FREE
5456
5459
{
5457
- printf("Freeing ");
5458
- if (tag == JS_TAG_OBJECT) {
5459
- JS_DumpObject(rt, JS_VALUE_GET_OBJ(v));
5460
- } else {
5461
- JS_DumpValue(rt, v);
5462
- printf("\n");
5460
+ /* Prevent invalid object access during GC */
5461
+ if ((rt->gc_phase != JS_GC_PHASE_REMOVE_CYCLES)
5462
+ || (tag != JS_TAG_OBJECT && tag != JS_TAG_FUNCTION_BYTECODE)) {
5463
+ printf("Freeing ");
5464
+ if (tag == JS_TAG_OBJECT) {
5465
+ JS_DumpObject(rt, JS_VALUE_GET_OBJ(v));
5466
+ } else {
5467
+ JS_DumpValue(rt, v);
5468
+ printf("\n");
5469
+ }
5463
5470
}
5464
5471
}
5465
5472
#endif
@@ -11708,7 +11715,11 @@ static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val)
11708
11715
{
11709
11716
JSFunctionBytecode *b = JS_VALUE_GET_PTR(val);
11710
11717
char buf[ATOM_GET_STR_BUF_SIZE];
11711
- printf("[bytecode %s]", JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
11718
+ if (b->func_name) {
11719
+ printf("[bytecode %s]", JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
11720
+ } else {
11721
+ printf("[bytecode (anonymous)]");
11722
+ }
11712
11723
}
11713
11724
break;
11714
11725
case JS_TAG_OBJECT:
@@ -27476,7 +27487,7 @@ static void js_free_function_def(JSContext *ctx, JSFunctionDef *fd)
27476
27487
27477
27488
#ifdef DUMP_BYTECODE
27478
27489
static const char *skip_lines(const char *p, int n) {
27479
- while (n-- > 0 && *p) {
27490
+ while (p && n-- > 0 && *p) {
27480
27491
while (*p && *p++ != '\n')
27481
27492
continue;
27482
27493
}
@@ -27486,7 +27497,7 @@ static const char *skip_lines(const char *p, int n) {
27486
27497
static void print_lines(const char *source, int line, int line1) {
27487
27498
const char *s = source;
27488
27499
const char *p = skip_lines(s, line);
27489
- if (*p) {
27500
+ if (p && *p) {
27490
27501
while (line++ < line1) {
27491
27502
p = skip_lines(s = p, 1);
27492
27503
printf(";; %.*s", (int)(p - s), s);
0 commit comments