Commit 79f3d604

79f3d6040b94Merged

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

Description

fix(runtime): NULL guards on sp_PtrArray_* helpers prevent segv on unresolved-call chains

T.Yamada's #566. A complex one-liner combining unsupported methods (each_cons / with_index) emitted sp_PtrArray *_t4 = 0; for the unresolved-call result, then downstream sp_PtrArray_length / sp_PtrArray_get dereferenced the NULL pointer and segfaulted.

Add NULL guards to sp_PtrArray_push / _pop / _get / _set / _length / _empty so a chain that hits an unresolved call returns the type-default (0 / NULL / TRUE) rather than crashing. Matches the NULL-safe pattern sp_StrIntHash_get, sp_PolyArray_length etc. already follow.

This does NOT add each_cons / with_index dispatch -- the underlying methods are still unresolved, so the program still outputs 0 instead of CRuby's 3. But exit status is 0 (no segv) so the diagnostic surface is the warning lines spinel already prints, not a crash.

Refs #566 (each_cons / with_index enumerator chaining left as future work)

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
79f3d6040b94
Parent
220ad5c6a894

lib/sp_runtime.hmodifiedView file

@@ -700,17 +700,17 @@
700700 crash at collection time. Skip per-element scanning; the array
701701 header itself is still GC-tracked. */
702702 static sp_PtrArray*sp_PtrArray_new_noscan(void){return sp_PtrArray_new_scan(NULL);}
703-static inline void sp_PtrArray_push(sp_PtrArray*a,void*v){if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(void*)*a->cap;h->size-=sizeof(void*)*a->cap;a->cap=a->cap*2+1;a->data=(void**)realloc(a->data,sizeof(void*)*a->cap);h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}a->data[a->len++]=v;}
703+static inline void sp_PtrArray_push(sp_PtrArray*a,void*v){if(!a)return;if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(void*)*a->cap;h->size-=sizeof(void*)*a->cap;a->cap=a->cap*2+1;a->data=(void**)realloc(a->data,sizeof(void*)*a->cap);h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}a->data[a->len++]=v;}
704704 /* Array#pop on a `<X>_ptr_array`. Returns NULL when empty
705705 (matches CRuby's nil for typed-element arrays since the slot
706706 can't carry nil). Issue #520: previously the dispatch on
707707 `int_array_ptr_array.pop` warned "cannot resolve call to
708708 'pop'" and emitted 0, silently leaving the array intact. */
709-static inline void *sp_PtrArray_pop(sp_PtrArray*a){if(a->len==0)return NULL;return a->data[--a->len];}
710-static inline void*sp_PtrArray_get(sp_PtrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}
711-static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}
712-static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){return a->len;}
713-static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){return a->len==0;}
709+static inline void *sp_PtrArray_pop(sp_PtrArray*a){if(!a||a->len==0)return NULL;return a->data[--a->len];}
710+static inline void*sp_PtrArray_get(sp_PtrArray*a,mrb_int i){if(!a)return NULL;if(i<0)i+=a->len;if(i<0||i>=a->len)return NULL;return a->data[i];}
711+static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(!a)return;if(i<0)i+=a->len;a->data[i]=v;}
712+static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){if(!a)return 0;return a->len;}
713+static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){if(!a)return TRUE;return a->len==0;}
714714 /* `Array#delete_at(i)` -- remove and return the element at index i.
715715 Negative indices count from the end. Returns NULL when i is out
716716 of range (matches CRuby's nil for typed-element arrays). Mirrors