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
crash at collection time. Skip per-element scanning; the array header itself is still GC-tracked. */static sp_PtrArray*sp_PtrArray_new_noscan(void){return sp_PtrArray_new_scan(NULL);}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;}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;}/* Array#pop on a `<X>_ptr_array`. Returns NULL when empty (matches CRuby's nil for typed-element arrays since the slot can't carry nil). Issue #520: previously the dispatch on `int_array_ptr_array.pop` warned "cannot resolve call to 'pop'" and emitted 0, silently leaving the array intact. */static inline void *sp_PtrArray_pop(sp_PtrArray*a){if(a->len==0)return NULL;return a->data[--a->len];}static inline void*sp_PtrArray_get(sp_PtrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){return a->len;}static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){return a->len==0;}static inline void *sp_PtrArray_pop(sp_PtrArray*a){if(!a||a->len==0)return NULL;return a->data[--a->len];}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];}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;}static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){if(!a)return 0;return a->len;}static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){if(!a)return TRUE;return a->len==0;}/* `Array#delete_at(i)` -- remove and return the element at index i. Negative indices count from the end. Returns NULL when i is out of range (matches CRuby's nil for typed-element arrays). Mirrorstest/ptr_array_null_guards.rbaddedView file
Loading diff…
test/ptr_array_null_guards.rb.expectedaddedView file
Loading diff…