Commit fd72fbcf

fd72fbcf3842Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(runtime): sp_bigint_to_int handles NULL as 0

A promote-mode method body whose last statement is a void-shape side-effect (@ivar = ..., Klass.send(...), a setter call) emits return NULL since c_default_val("bigint") == NULL. Callers that unwrap the result via sp_bigint_to_int -- e.g. an instance_eval lifted function whose adapter expects mrb_int return -- then crash on null deref.

Treat NULL as the int value 0. Matches the existing z->sz == 0 zero-encoding return at the next line.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
fd72fbcf3842
Parent
25e1efc9339b

lib/sp_bigint.cmodifiedView file

@@ -5363,7 +5363,12 @@
53635363 }
53645364
53655365 int64_t sp_bigint_to_int(sp_Bigint *b) {
5366- /* Extract int64 from mpz - for small values */
5366+ /* Extract int64 from mpz - for small values.
5367+ Treat NULL as 0 -- a promote-mode method body whose tail is a
5368+ setter / void-shape statement returns NULL (the bigint default),
5369+ and the caller may unwrap with sp_bigint_to_int when its slot
5370+ wants mrb_int. */
5371+ if (b == NULL) return 0;
53675372 mpz_t *z = &b->mpz;
53685373 if (z->sz == 0) return 0;
53695374 int64_t v = 0;