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
/* Array#inspect for each typed array: `[elem1, elem2, ...]` with each element rendered via its own primitive inspect. Matches CRuby's Array#inspect output byte-for-byte. Returns a GC-managed C string. */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;}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;}/* The inspect helpers NULL-guard their receiver: a NULL array can reach here when an unresolved call (e.g. an unsupported Array method) emits a 0 placeholder that flows into `.inspect`, and dereferencing a->len would segfault. Rendering "[]" stops the crash and degrades to the same shape as the empty-array case. */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;}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;}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;}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;}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;}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;}/* Symbol arrays share the IntArray representation (sp_sym = mrb_int), but each element is rendered as ":name" via sp_sym_to_s. */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;}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;}/* PtrArray elements are object pointers without a per-element class tag, so we render them as `#<Object>` rather than recursing. */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;}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;}/* Issue #851: Hash#inspect for typed-hash variants beyond sym_int_hash. Renders Ruby's `{"k"=>v, ...}` (string keys), `{42=>"v", ...}` (int keys), or `{:k=>v, ...}` (sym keys buttest/array_unresolved_inspect_no_segv.rbaddedView file
Loading diff…
test/array_unresolved_inspect_no_segv.rb.expectedaddedView file
Loading diff…