Commit 6c6d9008

6c6d900864e8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Stop NULL array from segfaulting in inspect helpers

An unresolved Array method (e.g. repeated_permutation / repeated_combination, which Spinel does not implement) emits a 0 placeholder for its result. Chaining .to_a.inspect onto that NULL array dereferenced a->len in sp_IntArray_inspect and crashed.

NULL-guard the typed-array inspect helpers (Int/Float/Str/Sym/Ptr) so a NULL receiver renders "[]" instead of dereferencing, matching the defensive pattern the sibling _eq / _combination helpers already use. This removes the whole NULL-deref-on-inspect crash class, not just the repeated_permutation path. (Computing the permutation itself remains a separate unimplemented feature; this only makes the unresolved path degrade safely.)

Fixes #1244

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

Details

Commit
6c6d900864e8
Parent
0960e71d8c0f

lib/sp_runtime.hmodifiedView file

@@ -1739,17 +1739,22 @@
17391739 /* Array#inspect for each typed array: `[elem1, elem2, ...]` with each
17401740 element rendered via its own primitive inspect. Matches CRuby's
17411741 Array#inspect output byte-for-byte. Returns a GC-managed C string. */
1742-static const char*sp_IntArray_inspect(sp_IntArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_int_to_s(a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
1743-static const char*sp_FloatArray_inspect(sp_FloatArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_float_inspect(a->data[i]));}sp_String_append(s,"]");return s->data;}
1742+/* The inspect helpers NULL-guard their receiver: a NULL array can
1743+ reach here when an unresolved call (e.g. an unsupported Array method)
1744+ emits a 0 placeholder that flows into `.inspect`, and dereferencing
1745+ a->len would segfault. Rendering "[]" stops the crash and degrades to
1746+ the same shape as the empty-array case. */
1747+static const char*sp_IntArray_inspect(sp_IntArray*a){if(!a)return "[]";sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_int_to_s(a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
1748+static const char*sp_FloatArray_inspect(sp_FloatArray*a){if(!a)return "[]";sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_float_inspect(a->data[i]));}sp_String_append(s,"]");return s->data;}
17441749 static mrb_bool sp_FloatArray_eq(sp_FloatArray*a,sp_FloatArray*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++)if(a->data[i]!=b->data[i])return FALSE;return TRUE;}
1745-static const char*sp_StrArray_inspect(sp_StrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_str_inspect(a->data[i]));}sp_String_append(s,"]");return s->data;}
1750+static const char*sp_StrArray_inspect(sp_StrArray*a){if(!a)return "[]";sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_str_inspect(a->data[i]));}sp_String_append(s,"]");return s->data;}
17461751 static mrb_bool sp_StrArray_eq(sp_StrArray*a,sp_StrArray*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++)if(!sp_str_eq(a->data[i],b->data[i]))return FALSE;return TRUE;}
17471752 /* Symbol arrays share the IntArray representation (sp_sym = mrb_int),
17481753 but each element is rendered as ":name" via sp_sym_to_s. */
1749-static inline const char*sp_SymArray_inspect(sp_IntArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,":");sp_String_append(s,sp_sym_to_s((sp_sym)a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
1754+static inline const char*sp_SymArray_inspect(sp_IntArray*a){if(!a)return "[]";sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,":");sp_String_append(s,sp_sym_to_s((sp_sym)a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
17501755 /* PtrArray elements are object pointers without a per-element class
17511756 tag, so we render them as `#<Object>` rather than recursing. */
1752-static const char*sp_PtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,"#<Object>");}sp_String_append(s,"]");return s->data;}
1757+static const char*sp_PtrArray_inspect(sp_PtrArray*a){if(!a)return "[]";sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,"#<Object>");}sp_String_append(s,"]");return s->data;}
17531758 /* Issue #851: Hash#inspect for typed-hash variants beyond
17541759 sym_int_hash. Renders Ruby's `{"k"=>v, ...}` (string keys),
17551760 `{42=>"v", ...}` (int keys), or `{:k=>v, ...}` (sym keys but