Commit 10455559

10455559867fMerged

Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

runtime: match Array#assoc/#rassoc against any pair's array kind

[[1, 2], [3, 4]].assoc(3) returned nil. sp_PolyArray_assoc/rassoc skipped every element whose cls_id was not SP_BUILTIN_POLY_ARRAY, but a pair of like-typed values is a homogeneous typed array ([1, 2] is an IntArray), so those pairs were never compared. Mixed pairs (["a", 1]) are PolyArrays and already worked.

Compare the keyed element with sp_poly_arr_get, which boxes an element from any array kind, and return the matched pair through a new sp_pair_to_poly that boxes a non-poly pair into a PolyArray so the functions' PolyArray* return type still holds. The matched pair round-trips through .inspect unchanged.

Guard both functions with a new sp_array_kind_len that reports the element count of an array-kind value and -1 for anything that is not an array (a non-object, a user object, a hash). assoc requires length >= 1 and rassoc length >= 2 before indexing, so a nil search key no longer spuriously matches an out-of-bounds (nil) read on an empty, too-short, or non-array element -- [[], [1], (1..2)].assoc(nil) is nil, as in CRuby.

sp_poly_arr_get was missing its SP_BUILTIN_SYM_ARRAY arm and returned nil for every symbol-array element, so [[:a, :b]].assoc(:c) could not match and a matched symbol pair boxed to nils. Add the arm at the source (it boxes ar->data[ar->start + i] with sp_box_sym) so assoc/rassoc and every other sp_poly_arr_get caller handle symbol arrays.

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

Details

Commit
10455559867f
Parent
3d4efd9a07a7

lib/sp_runtime.hmodifiedView file

@@ -3783,6 +3783,7 @@
37833783 if (a.tag != SP_TAG_OBJ) return sp_box_nil();
37843784 switch (a.cls_id) {
37853785 case SP_BUILTIN_INT_ARRAY: { sp_IntArray *ar=(sp_IntArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_int(ar->data[ar->start+i]); }
3786+ case SP_BUILTIN_SYM_ARRAY: { sp_IntArray *ar=(sp_IntArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_sym((sp_sym)ar->data[ar->start+i]); }
37863787 case SP_BUILTIN_FLT_ARRAY: { sp_FloatArray *ar=(sp_FloatArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_float(ar->data[i]); }
37873788 case SP_BUILTIN_STR_ARRAY: { sp_StrArray *ar=(sp_StrArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_str(ar->data[i]); }
37883789 case SP_BUILTIN_POLY_ARRAY: { sp_PolyArray *ar=(sp_PolyArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return ar->data[i]; }
@@ -4018,31 +4019,57 @@
40184019 char *r = sp_str_alloc(out); memcpy(r, buf, out); free(buf); return r;
40194020 }
40204021
4021-/* Array#assoc — return the first sub-array (poly_array element)
4022- whose first element equals `key`. Returns NULL when no match
4023- so the caller's `.inspect` round-trips to "nil". */
4022+/* Element count of an array-kind value, or -1 if `el` is not an array (a
4023+ non-object, a user object, a hash, etc.). Lets assoc/rassoc skip non-array
4024+ and too-short pairs without indexing them, so a `nil` search key cannot
4025+ spuriously match an out-of-bounds (nil) read. */
4026+static mrb_int sp_array_kind_len(sp_RbVal el) {
4027+ if (el.tag != SP_TAG_OBJ || !el.v.p) return -1;
4028+ switch (el.cls_id) {
4029+ case SP_BUILTIN_INT_ARRAY:
4030+ case SP_BUILTIN_SYM_ARRAY: return ((sp_IntArray *)el.v.p)->len;
4031+ case SP_BUILTIN_FLT_ARRAY: return ((sp_FloatArray *)el.v.p)->len;
4032+ case SP_BUILTIN_STR_ARRAY: return ((sp_StrArray *)el.v.p)->len;
4033+ case SP_BUILTIN_POLY_ARRAY: return ((sp_PolyArray *)el.v.p)->len;
4034+ default: return -1;
4035+ }
4036+}
4037+
4038+/* Box any array-kind element into a PolyArray so assoc/rassoc can return it
4039+ through their PolyArray* type regardless of the matched pair's own kind
4040+ (a like-typed pair such as [1, 2] is an IntArray, not a PolyArray). */
4041+static sp_PolyArray *sp_pair_to_poly(sp_RbVal el) {
4042+ if (el.tag == SP_TAG_OBJ && el.cls_id == SP_BUILTIN_POLY_ARRAY) return (sp_PolyArray *)el.v.p;
4043+ mrb_int n = sp_array_kind_len(el);
4044+ if (n < 0) n = 0;
4045+ sp_PolyArray *r = sp_PolyArray_new();
4046+ SP_GC_ROOT(r);
4047+ for (mrb_int j = 0; j < n; j++) sp_PolyArray_push(r, sp_poly_arr_get(el, j));
4048+ return r;
4049+}
4050+
4051+/* Array#assoc — return the first sub-array whose first element equals `key`.
4052+ Returns NULL when no match so the caller's `.inspect` round-trips to "nil".
4053+ Each pair may be any array kind, so compare element 0 via sp_poly_arr_get;
4054+ a pair with no element 0 (a non-array or empty array) is skipped. */
40244055 static sp_PolyArray *sp_PolyArray_assoc(sp_PolyArray *a, sp_RbVal key) {
40254056 if (!a) return NULL;
40264057 for (mrb_int i = 0; i < a->len; i++) {
40274058 sp_RbVal el = a->data[i];
4028- if (el.tag != SP_TAG_OBJ) continue;
4029- if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue;
4030- sp_PolyArray *sub = (sp_PolyArray *)el.v.p;
4031- if (sub && sub->len >= 1 && sp_poly_eq(sub->data[0], key)) return sub;
4059+ if (sp_array_kind_len(el) >= 1 && sp_poly_eq(sp_poly_arr_get(el, 0), key))
4060+ return sp_pair_to_poly(el);
40324061 }
40334062 return NULL;
40344063 }
40354064
40364065 /* Array#rassoc — same as assoc but matches against the second
4037- element of each sub-array. */
4066+ element of each sub-array (a pair with fewer than 2 elements is skipped). */
40384067 static sp_PolyArray *sp_PolyArray_rassoc(sp_PolyArray *a, sp_RbVal val) {
40394068 if (!a) return NULL;
40404069 for (mrb_int i = 0; i < a->len; i++) {
40414070 sp_RbVal el = a->data[i];
4042- if (el.tag != SP_TAG_OBJ) continue;
4043- if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue;
4044- sp_PolyArray *sub = (sp_PolyArray *)el.v.p;
4045- if (sub && sub->len >= 2 && sp_poly_eq(sub->data[1], val)) return sub;
4071+ if (sp_array_kind_len(el) >= 2 && sp_poly_eq(sp_poly_arr_get(el, 1), val))
4072+ return sp_pair_to_poly(el);
40464073 }
40474074 return NULL;
40484075 }