Skip to content

Commit 3c62f5f

Browse files
committed
Core: Fix RefCounted handling in marshalls.cpp
1 parent c951421 commit 3c62f5f

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

core/io/marshalls.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,19 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
656656
ERR_FAIL_COND_V(!ClassDB::can_instantiate(str), ERR_INVALID_DATA);
657657

658658
Object *obj = ClassDB::instantiate(str);
659-
660659
ERR_FAIL_NULL_V(obj, ERR_UNAVAILABLE);
661-
ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
662660

661+
// Avoid premature free `RefCounted`. This must be done before properties are initialized,
662+
// since script functions (setters, implicit initializer) may be called. See GH-68666.
663+
Variant variant;
664+
if (Object::cast_to<RefCounted>(obj)) {
665+
Ref<RefCounted> ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
666+
variant = ref;
667+
} else {
668+
variant = obj;
669+
}
670+
671+
ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
663672
int32_t count = decode_uint32(buf);
664673
buf += 4;
665674
len -= 4;
@@ -699,12 +708,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
699708
}
700709
}
701710

702-
if (Object::cast_to<RefCounted>(obj)) {
703-
Ref<RefCounted> ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
704-
r_variant = ref;
705-
} else {
706-
r_variant = obj;
707-
}
711+
r_variant = variant;
708712
}
709713
}
710714

0 commit comments

Comments
 (0)