62 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 17d124b00d array.c (mrb_ary_splice): re-modify a after self-aset ary_dup
`a[range] = a` on a long-enough array tripped a heap-buffer-overflow
in value_move(). mrb_ary_splice's self-aset branch calls ary_dup(a)
to get an independent copy of the source elements, but ary_dup ->
ary_replace converts the source to shared as a copy-on-write
optimization when the length exceeds ARY_REPLACE_SHARED_MIN. After
that, a->as.heap.aux is reinterpreted as `shared` (the union member)
and ARY_CAPA(a) reads from the shared pointer's bits rather than
the real capacity. The expand-capa check below then silently mis-
sizes and value_move walks past the buffer.

Re-modify `a` immediately after ary_dup to un-share before the in-
place mutation. The buffer reads through `argv` (which now points
into the dup's storage) stay valid because ary_modify on a multi-
reference shared array allocates a fresh buffer for `a` and leaves
the original buffer owned by the dup.

Found via clusterfuzz mruby_fuzzer testcase 6525563811725312;
regression test covers a[3, 2] = a on a 31-element array (above
the ARY_REPLACE_SHARED_MIN=20 threshold).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-20 14:27:22 +09:00
Yukihiro "Matz" Matsumoto fa8f66ed1b enum.rb: fix Array#hash infinite loop with self-referencing enumerables
Modify `Enumerable#hash` to use `__method_recursive?(:hash)` for recursion
detection, preventing infinite loops when hashing self-referencing enumerables.
Add a test case to verify the fix.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:04 +09:00
Yukihiro "Matz" Matsumoto ebd9f29b38 The method #initialize_copy should be private, no direct call
Fixed test/t/array.rb and test/t/string.rb in standard tests.
And mrbgems/mruby-struct/test/struct.rb as well.
2025-06-14 13:38:22 +09:00
Yukihiro "Matz" Matsumoto 214ae5aad3 test/array.rb: call #initialize via #__send__ to skip private check 2025-03-07 17:17:39 +09:00
Yukihiro "Matz" Matsumoto 6037e50298 test/hash: update tests to new hash format 2024-12-21 08:53:57 +09:00
Yukihiro "Matz" Matsumoto 5387b74be7 hash.c (mrb_hash_to_s): put spaces around =>
CRuby 3.4 puts spaces around `=>` since for example `{:a!=>2}` can be
confusing where to separate tokens.  mruby should follow the behavior.

Many tests in `test/t` directory assumed no spaces around `=>`, so we
needed to fix them too.
2024-11-11 11:07:43 +09:00
Yukihiro "Matz" Matsumoto eee83ed7af array.c: implement Array#index and Array#rindex in C
No need to override Array#index in mruby-array-ext. We can call
`to_enum` from C implemented methods.
2024-06-29 15:03:12 +09:00
dearblue f0cd35c78b Passes the nonexistent key as a block argument in Array#delete
```ruby
a = %w(R G B A)
p a.delete("Y") { _1 }
# BEFORE => nil (same for mruby 3.3)
# AFTER  => "Y" (same for CRuby)
```
2024-05-10 22:46:11 +09:00
dearblue c753ca33d0 Add test for Array#delete 2024-05-10 22:35:13 +09:00
Koichi ITO 182096d8c0 Make Array#* the CRuby compatible behavior when giving a string argument
## Summary

This following is a behavior from CRuby 1.8.7 to 3.1.0.

```console
% ruby -ve "p ['a', 'b', 'c']*''"
ruby 1.8.7 (2013-12-22 patchlevel 375) [i686-darwin13.0.2]
"abc"

% ruby -ve "p ['a', 'b', 'c']*''"
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin19]
"abc"
```

### Before (mruby 3.0.0)

mruby unexpectedly gives the TypeError.

```ruby
['a', 'b', 'c']*'' #=> String cannot be converted to Integer (TypeError)
```

### After

This PR makes mruby behave compatible with CRuby.

```ruby
['a', 'b', 'c']*'' #=> 'abc'
```

As far as I checked, the behavior is unspecified when `Array#*`'s argument
is not an instance of Integer class in X 3017 : 2013 (ISO/IEC 30170 : 2012).

## Additional Information

I noticed this difference by the following idiom when writing ASCII art code
using Ruby.

```ruby
%w(foo bar baz)*''
```

e.g. TRICK (https://github.com/tric)
2022-02-08 11:20:14 +09:00
Yukihiro "Matz" Matsumoto 9f77232b71 array.c: update Array#shift to take optional argument; close #5428 2021-04-27 16:37:24 +09:00
Yukihiro "Matz" Matsumoto 75ae3d3e23 range.c: fixed a begin-less ranges issue. 2021-03-17 09:48:27 +09:00
John Bampton 940dec5e7d Fix spelling 2020-12-13 18:38:22 +10:00
Yukihiro "Matz" Matsumoto 6b457d2c00 Merge branch 'work_for_merge' of https://github.com/zubycz/mruby into zubycz-work_for_merge 2020-10-15 18:35:13 +09:00
taiyoslime bec4d05340 Introduce endless range (a part of #5085)
Co-Authored-By: n4o847 <22975590+n4o847@users.noreply.github.com>
Co-Authored-By: smallkirby <ssmallkirby@gmail.com>
2020-10-13 14:09:36 +09:00
Yukihiro "Matz" Matsumoto 2b188ed8a1 Reorganize Integer system.
- Integrate `Fixnum` and `Integer`
- Remove `Integral`
- `int / int -> int`
- Replace `mrb_fixnum()` to `mrb_int()`
- Replace `mrb_fixnum_value()` to `mrb_int_value()`.
- Use `mrb_integer_p()` instead of `mrb_fixnum_p()`
2020-10-12 18:19:54 +09:00
Yukihiro "Matz" Matsumoto d605b72c1d Merge branch 'master' into i110/inspect-recursion 2019-07-17 10:35:41 +09:00
KOBAYASHI Shuji f65b35dc9f Fix test for Array#slice 2019-04-07 21:31:12 +09:00
KOBAYASHI Shuji fbad7a1595 Use FrozenError instead of RuntimeError in frozen object modification test 2019-03-19 20:48:32 +09:00
Yukihiro "Matz" Matsumoto e5649eba70 Remove useless regression tests; ref #4306 2019-03-02 14:01:52 +09:00
Yukihiro "Matz" Matsumoto 997cc8238d Remove useless regression tests; fix #4306 2019-03-02 14:00:25 +09:00
Yukihiro "Matz" Matsumoto cca19532c5 Remove Kernel#class_defined? which is not available in CRuby; #3829 2019-01-03 11:34:35 +09:00
Tomoyuki Sahara 91444c4747 replace quicksort with mergesort. 2018-10-18 13:07:47 +09:00
Ichito Nagata 2af92d0ebc Let inspect recursion do the right thing 2018-06-04 11:25:10 +09:00
Takeshi Watanabe e66c990234 Fix array replace leak error in mruby-uri. 2018-04-25 13:27:00 +09:00
YAMAMOTO Masaya dcd5d0ffec Test for MRB_WITHOUT_FLOAT 2017-10-11 17:58:11 +09:00
ksss bf48473cf6 Should only check frozen for Array#pop 2017-07-09 18:05:21 +09:00
ksss c76dc33116 Add frozen test for Array#shift 2017-07-09 17:53:12 +09:00
okkez 6dbe2272cf Set proper class to subclass of Array
More compatibility to CRuby.
2017-03-29 08:02:21 +09:00
Takashi Kokubun 10bb7ad693 Implement Object#freeze 2016-12-11 03:44:15 +09:00
Yutaka HARA 3f83ec64a8 Add test for recently fixed bugs 2016-12-01 14:55:26 +09:00
Yukihiro "Matz" Matsumoto fb7776586e hash value may be overflown from Integer
mruby special.
2016-11-22 18:00:04 +09:00
Yukihiro "Matz" Matsumoto 1f554ff854 Fixed rindex calling into mrb_equal bug
Fixed by Alex Snaps and reported by Mathieu Leduc-Hamel,
both from shopify.com.  Thank you!
2016-11-16 02:10:44 +09:00
take_cheeze ec5b055694 Move direct superclass checking to test/t/superclass.rb. 2014-06-15 15:02:21 +09:00
yui-knk eafc4dd0af Make Array#[]= raise IndexError.
If second param is negative, Array#[] raise IndexError.
2014-05-08 00:17:17 +09:00
Yukihiro "Matz" Matsumoto f5ec2115b5 swap actual and expected; ref #1764 2014-02-28 10:01:39 +09:00
ksss a14a672915 fix test for Array#[] 2014-02-28 08:50:26 +09:00
Yukihiro "Matz" Matsumoto aa08158f67 Array#[]= is now range aware 2014-01-30 10:50:56 +09:00
Yukihiro "Matz" Matsumoto 0006278088 move Array#[] tests from mrbgems to test/t/array.rb 2014-01-30 10:33:47 +09:00
Yukihiro "Matz" Matsumoto c7cff199dc hash value of enumerable should be obtained from its elements; close #1658 2014-01-16 21:33:48 +09:00
Akira Kuroda 5ed6040eb1 add some tests for [], delete_at, index, and rindex 2013-12-30 23:34:56 +09:00
Jun Hiroe 853e26e16a Fix ISO no in Array#* and Array#+ 2013-12-07 12:22:25 +09:00
Paolo Bosetti 6d5bd14481 Added test for inline arrays longer than 126 elements. 2013-10-01 14:47:58 +02:00
Jun Hiroe 90f6b1815c I add ISO test '15.2.12.3' included modules in Arrray 2013-08-13 21:09:55 +09:00
Daniel Bovensiepen 6873674ed1 Fix order of actual and expect test value for Array 2013-08-01 15:12:50 +08:00
Daniel Bovensiepen d7deef8a2d Fix Array Tests 2013-06-09 04:51:19 +08:00
Daniel Bovensiepen 01eb9e0e84 Improve Array tests 2013-06-09 04:49:06 +08:00
Masaki Muranaka e129a4c8ef Add test cases. 2013-04-02 11:43:30 +09:00
Yukihiro Matz Matsumoto 30d7c60cfd Array#[]= should return assigning value; close #584 2012-12-03 23:35:21 +09:00
Akira Kuroda 5c2afd239c add test case for Array#unshift, <=>, and * 2012-09-30 17:59:45 +09:00