224 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto f88847841a mruby-array-ext: fix use-after-free in ary_slice_bang
This commit fixes a use-after-free vulnerability in `ary_slice_bang` by
replacing pointer-based operations with index-based operations. This prevents
raw pointers from becoming stale after a garbage collection cycle is triggered
by `mrb_ary_new_from_values`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:50 +09:00
Yukihiro "Matz" Matsumoto 81726bacb8 mruby-array-ext: fix use-after-free in ary_uniq_bang
This commit fixes a use-after-free vulnerability in `ary_uniq_bang` by
replacing pointer-based iteration with index-based loops. This prevents raw
pointers from becoming stale after a garbage collection cycle is triggered by
functions like `mrb_hash_set` or `mrb_equal`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:50 +09:00
Yukihiro "Matz" Matsumoto 96a9150580 mruby-array-ext: fix use-after-free in ary_uniq
This commit fixes a use-after-free vulnerability in `ary_uniq` by replacing
pointer-based iteration with index-based loops. This prevents raw pointers from
becoming stale after a garbage collection cycle is triggered by functions like
`mrb_hash_set`, `mrb_ary_push`, or `mrb_equal`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 48d9113b68 mruby-array-ext: fix use-after-free in ary_intersect_p
This commit fixes a use-after-free vulnerability in `ary_intersect_p` by
replacing pointer-based iteration with index-based loops. This prevents
raw pointers from becoming stale after a garbage collection cycle is
triggered by functions like `mrb_hash_set` or `mrb_equal`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 5640e1bd9e mruby-array-ext: fix use-after-free in ary_rotate
This commit fixes a use-after-free vulnerability in `ary_rotate` by replacing a
pointer-based loop with an index-based loop. This prevents a raw pointer from
becoming stale after a garbage collection cycle is triggered by `mrb_ary_push`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 6eaa585b80 mruby-array-ext: fix use-after-free in ary_compact
This commit fixes a use-after-free vulnerability in `ary_compact` by replacing
a pointer-based loop with an index-based loop. This prevents a raw pointer from
becoming stale after a garbage collection cycle is triggered by `mrb_ary_push`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 6c2a25aa1a mruby-array-ext: fix use-after-free in ary_subtract_internal
This commit fixes a use-after-free vulnerability in
`ary_subtract_internal` by replacing pointer-based iteration
with index-based loops. This prevents raw pointers from becoming
stale after a garbage collection cycle is triggered by functions like
`mrb_hash_set` or `mrb_ary_push`.

This change also ensures that array-like objects are correctly converted
to arrays before being used in the subtraction logic.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 07b803e28a docs: replace xml-style markup with markdown in comments
Replace XML-style markup tags in comments with markdown equivalents:
- <code>...</code> to `...` (inline code)
- <tt>...</tt> to `...` (teletype/monospace)
- <i>...</i> to *...* (italics/emphasis)
- +...+ to `...` (parameter/variable references)

Updated 80+ files across core source, headers, mrbgems, and libraries
to use consistent markdown formatting in documentation comments.
Handled edge cases including special characters like <=> operators.

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 95895789c8 mruby-array-ext: fix use-after-free in ary_intersection_internal
This commit fixes a use-after-free vulnerability in `ary_intersection_internal`
by replacing pointer-based iteration with index-based loops. This prevents raw
pointers from becoming stale after a garbage collection cycle is triggered by
functions like `mrb_hash_set` or `mrb_ary_push`.

This change also ensures that array-like objects are correctly converted to
arrays before being used in the intersection logic.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 0cb5a4ba4b mruby-array-ext: fixed use-after-free in ary_union_internal() 2025-08-14 10:52:48 +09:00
Yukihiro "Matz" Matsumoto cf9596d339 mruby-array-ext: add comprehensive documentation for values_at and internal helpers
- Add complete call-seq documentation for Array#values_at method
- Add helpful comments for internal helper functions:
  * ary_ref: helper function for values_at
  * rev: helper function to reverse array elements in-place
  * flatten_internal: iterative stack-based flatten implementation
  * Updated comments for fill, uniq, normalize_index, and fetch helpers
- Improves code maintainability and follows mruby documentation standards
- Achieves 100% public API documentation coverage

Co-authored-by: Atlassian Rovo Dev
2025-07-12 08:38:39 +09:00
Yukihiro "Matz" Matsumoto 6dc73d2517 mruby-array-ext: unify array#- tests
Merged the separate "Array#-" and "Array#- with large arrays" test
blocks into a single comprehensive test. The unified test covers both
basic functionality (type checking, simple subtraction) and the
hash-based implementation for large arrays (>32 elements).

Co-authored-by: Atlassian Rovo Dev
2025-07-05 18:32:46 +09:00
Yukihiro "Matz" Matsumoto 69d278589d mruby-array-ext: add Array#deconstruct for pattern matching
Implement new method for Ruby 2.7+ pattern matching compatibility.
Returns the array itself to enable case/in pattern matching syntax.
Complements Hash#deconstruct_keys for complete pattern matching support.

Co-authored-by: Atlassian Rovo Dev
2025-07-01 22:34:01 +09:00
Yukihiro "Matz" Matsumoto f3c4d64a5e mruby-array-ext: optimize fetch_values for non-block case
Use the C-implemented `__fetch` for `Array#fetch_values` when no block
is given to improve performance.

Co-authored-by: Atlassian Rovo Dev
2025-06-30 12:17:22 +09:00
Yukihiro "Matz" Matsumoto 90fd382a9e mruby-array-ext: implement Array#fetch with hybrid approach
Moved Array#fetch from Ruby to C using hybrid implementation for
better performance. The C implementation handles all non-block cases
with unified API that eliminates Ruby conditional logic.

Key improvements:
- Fast C implementation for common cases (no blocks)
- Shared index normalization helper reusable for other methods
- Unified C call eliminates NONE sentinel comparison in Ruby
- Block cases use C helper for index normalization

Added comprehensive test coverage including edge cases, default values,
block handling, and error message format verification. Combined tests
to focus on functionality rather than implementation details.

Co-authored-by: Atlassian Rovo Dev
2025-06-30 12:17:21 +09:00
Yukihiro "Matz" Matsumoto 87c39d3c0b mruby-array-ext: implement Array#insert in C
This commit also corrects the behavior of `Array#insert` when a negative
index is out of bounds. It now raises an `IndexError`, which is
consistent with CRuby.

Co-authored-by: Gemini <gemini@google.com>
2025-06-30 12:10:34 +09:00
Yukihiro "Matz" Matsumoto f8451045fe mruby-array-ext: implement flatten and flatten! in C
This commit replaces the Ruby implementation of  and  with a C
implementation. The new implementation is iterative and uses a stack to
avoid deep recursion, which prevents stack overflows when flattening
deeply nested arrays.

Co-authored-by: Gemini <gemini@google.com>
2025-06-30 11:55:17 +09:00
Yukihiro "Matz" Matsumoto ec7d94685d mruby-array-ext: implement fast path for uniq/uniq! in c
Co-authored-by: Gemini <gemini@google.com>
2025-06-30 10:26:43 +09:00
Yukihiro "Matz" Matsumoto 9e8cda73f6 mruby-array-ext: implement Array#fill in C
Implemented shared C argument parser and separate fill logic to eliminate code
duplication while maximizing performance. The implementation uses C implemented
__fill_parse_args for unified argument handling and __fill_exec for fast
C-based value filling.

Added comprehensive test coverage for both shared argument parsing
and C fill implementation, including range arguments, block handling,
and array extension scenarios.

Co-authored-by: Atlassian Rovo Dev
Co-authored-by: Gemini <gemini@google.com>
2025-06-30 10:26:42 +09:00
Yukihiro "Matz" Matsumoto c4464fa25a array.c: expose mrb_ary_dup() as a new C API 2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto 0afe69696c mruby-array-ext: implement Array#intersection in C and refactor Array#&
Co-authored-by: Gemini <gemini@google.com>
2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto 0725f691b4 mruby-array-ext: implement Array#union in C and refactor Array#|
Co-authored-by: Gemini <gemini@google.com>
2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto fdb0a664c6 mruby-array-ext: implement Array#difference in C and refactor Array#-
The Ruby implementation of `Array#difference` was inefficient as it
called `Array#-` repeatedly, creating intermediate arrays.

This commit replaces it with a C implementation that processes all
arguments in a single pass. The core logic is extracted into a
shared helper function, `ary_subtract_internal`, which is now used
by both `Array#-` and `Array#difference`.

Co-authored-by: Gemini <gemini@google.com>
2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto f205c2233d mruby-array-ext: increase SET_OP_HASH_THRESHOLD from 16 to 32
We have more chance to avoid hash allocation in set-like methods. Since
memory situation heavily depends on the platform, we may need to make
this threshold configurable in the future.

Co-authored-by: Atlassian Rovo Dev
2025-06-29 20:46:51 +09:00
Yukihiro "Matz" Matsumoto da813e0486 mruby-array-ext: implement Array#intersect? in C for better performance
Moved Array#intersect? implementation from Ruby to C to improve memory
usage and performance with early termination optimization. The C
implementation uses hash-based lookup for large arrays (>16 elements)
and linear search for smaller arrays.

Added comprehensive test coverage including early termination scenarios,
empty arrays, size optimization verification, and edge cases with
duplicates and large arrays.

Co-authored-by: Atlassian Rovo Dev
2025-06-29 20:46:50 +09:00
Yukihiro "Matz" Matsumoto ea5de2b8da mruby-array-ext: implement Array#& in C for better performance
Moved Array#& (set intersection) implementation from Ruby to C to improve
memory usage and performance. The C implementation uses hash-based
deduplication for large arrays (>16 elements) and linear search for
smaller arrays, following the same hybrid pattern as Array#| and Array#-.

Key improvements:
- Hash-based approach uses mrb_hash_delete_key() for proper deduplication
- Linear search approach checks result array to ensure uniqueness
- Maintains order preservation from the first array
- Eliminates temporary object creation in Ruby implementation

Added comprehensive test coverage for both small and large array scenarios,
including edge cases like no intersection, complete intersection, and
duplicate handling.

Co-authored-by: Atlassian Rovo Dev
2025-06-28 01:33:04 +09:00
Yukihiro "Matz" Matsumoto df35982297 mruby-parray-ext: implement Array#| in C for better performance
The C implementation uses hash-based deduplication for large arrays
(>16 elements) and linear search for smaller arrays, following the same
pattern as other set operations.

Co-authored-by: Atlassian Rovo Dev
2025-06-28 00:47:10 +09:00
Yukihiro "Matz" Matsumoto 409f39e911 mruby-array-ext: implement Array#- in C
Refactor Array#- to a C implementation for improved memory and performance,
especially for set operations. Uses a hybrid approach for efficiency.

Co-authored-by: Gemini <gemini@google.com>
2025-06-28 00:47:09 +09:00
Yukihiro "Matz" Matsumoto c7ffb0cc55 mruby-array-ext: add README.md
The document is written by Google Jules.
2025-06-07 11:46:11 +09:00
Yukihiro "Matz" Matsumoto da4cfbf89c mruby-array-ext: add a new method Array#fetch_values 2024-09-10 10:22:34 +09:00
Yukihiro "Matz" Matsumoto aa06432776 mruby-array-ext: index should be within mrb_int range
Type check is done by `__to_int` method.
2024-08-01 07:34:20 +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
Yukihiro "Matz" Matsumoto cfd90a65f7 mruby-array-ext: use presym for initialization 2024-06-14 01:41:06 +09:00
Yukihiro "Matz" Matsumoto db30b0636f mruby-array-ext: use NONE.equal?() for NONE comparison; fix #6262
The equal (`==`) method of the comparison target might be redefined
(the root cause of #6262), and not supposed to be compared with NONE.
To reduce chance for the problem, we use `NONE.equal?()` for comparison.
2024-05-10 20:37:01 +09:00
leviongit 81bb520919 unify the code for filter methods (and speed up #reject!)
the worst case for `Array#reject!` (i.e. a proc always returning `true`)
is at least 5x worse than the worst case for `Array#select!` (proc
always returning `false`)

this commit unifies these implementations and inlines the (effective)
call of `#select!` in `#keep_if` and `#reject!` in `#delete_if`
2024-04-13 08:53:32 +02:00
John Bampton a83d7d3510 Fix indentation in two Ruby files 2023-10-14 18:52:30 +10:00
Yukihiro "Matz" Matsumoto a80f394d97 mruby-array-ext (ary_rotate_bang): mrb_ary_modify may alter buf address 2023-09-27 18:37:38 +09:00
Yukihiro "Matz" Matsumoto 629a6839fd mruby-array-ext (ary_compact_bang): skip check that is always true 2023-09-27 18:32:44 +09:00
Yukihiro "Matz" Matsumoto f65229d103 mruby-array-ext (ary_compact_bang): mrb_ary_modify may alter buf address 2023-09-27 18:30:43 +09:00
Yukihiro "Matz" Matsumoto 9822c843e9 mruby-array-ext/array.c: remove mrb_ prefix from static functions 2023-04-13 07:39:05 +09:00
Yukihiro "Matz" Matsumoto 4e5be5a288 array.c: avoid slower mrb_get_args() if possible 2023-01-25 14:10:03 +09:00
Yukihiro "Matz" Matsumoto 9c5dc42e59 small cosmetic changes.
I prefer `i++` style unless absolutely necessary.
This commit is an addition to 41e4148.
2022-11-19 17:11:56 +09:00
John Bampton ea8964ef35 ruby: standardize whitespace 2022-10-31 16:25:56 +10:00
Yukihiro "Matz" Matsumoto c27ae4337d mruby-array-ext/array.c (mrb_ary_values_at): avoid mrb_get_args().
If your function doesn't call VM recursively, you can use mrb_get_argv()
instead of mrb_get_args(mrb, "*", ...).
2022-10-20 19:07:25 +09:00
Yukihiro "Matz" Matsumoto 5c2f3f72a4 mruby-array-ext/array.c (mrb_ary_slice_bang): refactor.
Use `mrb_ary_new_from_value()` instead of adding individual elements
using a loop.
2022-10-15 08:46:48 +09:00
Yukihiro "Matz" Matsumoto b99c389ec3 internal.h: aggregate internal functions.
Internal functions can only be called from within the library.
Functions listed in `mruby/internal.h` can be called from:

* core (src/*.c)
* gems (mrbgems/**/*.c)

But not from the application linked with `libmruby`.
2022-04-02 18:25:13 +09:00
Yukihiro "Matz" Matsumoto 0bf1ed9f2c array-ext/array.c: remove unused local variable. 2022-03-03 08:08:01 +09:00
Yukihiro "Matz" Matsumoto 5305c5c84d array.c: call mrb_ary_delete_at() directly from Array#slice!.
Avoid `mrb_funcall()`.
2022-02-26 14:21:35 +09:00
dearblue a137ef12f9 Get object properties after mrb_get_args()
ref. #5613

I checked with Valgrind, and the methods that can cause use-after-free are `Array#rotate`, `Array#rotate!`, and `String#byteslice`.
Since `String#rindex` uses `RSTRING_LEN()` indirectly inside the function, no reference to the out-of-bounds range is generated.
2021-12-30 22:34:22 +09:00
dearblue 586525f99a Add Array#{repeated_combination,repeated_permutation} methods
Ruby 1.9.2 feature.

ref: https://docs.ruby-lang.org/ja/3.1.0/method/Array/i/repeated_combination.html
ref: https://docs.ruby-lang.org/ja/3.1.0/method/Array/i/repeated_permutation.html
2021-12-12 21:28:03 +09:00