Commit Graph

17846 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 0211004cf2 variable.c: combine variable declaration with initialization 2025-10-25 09:36:48 +09:00
Yukihiro "Matz" Matsumoto 0bfc2164ed vm.c: combine variable declaration with initialization 2025-10-25 09:19:14 +09:00
Yukihiro "Matz" Matsumoto bd3b5f87fb mruby-array-ext: revert unsafe length caching in ary_intersect_p; ref #6652
both hash and linear paths cache array lengths before loops that call
mrb_eql() and mrb_equal(), which can execute user code that modifies
arrays, causing out-of-bounds access.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-25 08:45:39 +09:00
Yukihiro "Matz" Matsumoto 56a0bdf493 mruby-array-ext: revert unsafe hash path hoisting; ref #6652
khash operations (kh_get, kh_put) call mrb_eql() which can execute user
code that modifies arrays during iteration, invalidating cached pointers
and lengths. reverted hoisting in ary_subtract_internal, ary_union_internal,
ary_intersection_internal, and ary_uniq_bang hash paths.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-25 08:45:39 +09:00
Yukihiro "Matz" Matsumoto 2f4d3a329b partial revert "mruby-array-ext: hoist RARRAY_PTR calls outside loops"; ref #6652
revert hoisting in functions that call mrb_equal() which can execute user
code that modifies arrays during iteration causing use-after-free

reverted functions:
- ary_assoc, ary_rassoc: call mrb_equal()
- ary_subtract_internal (linear path): calls mrb_equal()
- ary_union_internal (linear path): calls add_uniq() -> mrb_equal()
- ary_intersection_internal (linear path): calls mrb_equal()
- ary_intersect_p (linear path): calls mrb_equal()
- ary_uniq_bang (linear path): calls mrb_equal()

kept optimizations in:
- ary_compact_bang: only checks mrb_nil_p(), no callbacks
- ary_rotate: only reads from self, no callbacks
- hash paths: use kh_get/kh_put, no Ruby callbacks

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-25 08:45:39 +09:00
Yukihiro "Matz" Matsumoto 12268dc3ef revert "array.c: hoist RARRAY_PTR calls in comparison operator"; fix #6652
this reverts commit 04af58db89 which caused use-after-free vulnerability.
cached array pointers become invalid when mrb_cmp() executes user's <=>
method that can modify arrays during iteration

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-25 08:39:01 +09:00
Yukihiro "Matz" Matsumoto b135601e6a mruby-array-ext: combine variable declaration with initialization
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 15:28:25 +09:00
Yukihiro "Matz" Matsumoto 8c4bf04856 mruby-time: combine variable declaration with initialization
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 13:55:47 +09:00
Yukihiro "Matz" Matsumoto d4d2955c6b mruby-sprintf: combine variable declarations with initialization
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 13:35:44 +09:00
Yukihiro "Matz" Matsumoto 036b40e265 gc.c: combine variable declaration with initialization
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 11:34:21 +09:00
Yukihiro "Matz" Matsumoto 93619f06dd mruby-array-ext: validate start and length in fill operation; fix #6650
add validation to prevent out-of-bounds write when negative start or
length bypasses normalization

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 11:03:11 +09:00
Yukihiro "Matz" Matsumoto dee72daf97 mruby-sprintf: prevent buffer overread in named format parsing; fix #6648
add bounds check at retry label to prevent reading past end of format string
when parsing unterminated named parameters like %<foo without closing >

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 15:25:47 +09:00
Yukihiro "Matz" Matsumoto 01ab2ffc29 mruby-io: fix buffer overflow in io#ungetc; fix #6647
io_unget_data had two issues that caused crashes with repeated ungetc:

1. Integer underflow in buffer size check: "len > MRB_IO_BUF_SIZE - buf->len"
   could underflow when buf->len was large, bypassing reallocation

2. Short overflow: buf->len could exceed SHRT_MAX after multiple ungetc
   calls, causing integer overflow when cast to short

Fixed by checking buf->len + len against both MRB_IO_BUF_SIZE and
SHRT_MAX before buffer operations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 14:04:14 +09:00
Yukihiro "Matz" Matsumoto c21604eea6 mruby-io: validate negative length in io#gets; fix #6646
io_gets was passing negative limit values to io_buf_cat without
validation, causing negative-size-param in memcpy detected by ASAN.

Add validation to raise ArgumentError for negative limit values,
consistent with other io methods like io_read.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:49:40 +09:00
Yukihiro "Matz" Matsumoto a3797173c2 mruby-set: fix memory leak from double initialization; fix #6645
set_init was overwriting set->set without freeing the existing khash
table, causing a memory leak when initialize is called multiple times.

Prevent double initialization by raising an exception in set_init,
while allowing replace/dup semantics in set_init_copy by properly
freeing old data before reinitializing.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:47 +09:00
Yukihiro "Matz" Matsumoto 7e26271a01 mruby-string-ext: hoist RSTRING_PTR calls in String#tr
Optimizes String#tr by hoisting RSTRING_PTR calls for pattern strings
outside the main loop to avoid repeated conditional checks.

Before: 2 RSTRING_PTR calls per iteration (once for each pattern)
After: 2 RSTRING_PTR calls total (pointers cached outside loop)

String#tr is commonly used for character transliteration and this
optimization provides measurable improvement for long strings.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:47 +09:00
Yukihiro "Matz" Matsumoto 6043490c0a mruby-io: hoist RARRAY_PTR calls in IO.select loops
Optimizes IO.select by hoisting RARRAY_PTR calls outside loops to avoid
repeated conditional checks in both setup and result processing phases.

Optimized loops:
- Setup phase: 3 loops for read/write/except arrays
- Result phase: 3 loops for read/write/except arrays

Each loop previously called RARRAY_PTR 1-2 times per iteration. With
hoisting, each array pointer is retrieved once per loop instead of once
per iteration, significantly reducing overhead in I/O multiplexing.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:46 +09:00
Yukihiro "Matz" Matsumoto 04af58db89 array.c: hoist RARRAY_PTR calls in comparison operator
Optimizes Array#<=> by hoisting RARRAY_PTR calls outside the loop to
avoid repeated conditional checks. This is a frequently used operation
for array comparisons and sorting.

Before: 2 RARRAY_PTR calls per iteration (checks embed vs heap twice)
After: 2 RARRAY_PTR calls total (pointers cached outside loop)

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:46 +09:00
Yukihiro "Matz" Matsumoto 6e01f9dfc6 mruby-array-ext: hoist RARRAY_PTR calls outside loops
Optimizes array operations by hoisting RARRAY_PTR macro calls outside
loops to avoid repeated conditional checks (embed vs heap storage).

Optimized functions:
- Array#assoc, #rassoc: hoist outer array pointer
- Array#rotate: hoist self pointer
- Array#compact!: reduce 3 calls per iteration to 1
- Array#difference: hoist pointers in both hash and linear paths
- Array#union: hoist pointers in both hash and linear paths
- Array#intersection: hoist pointers in nested loops (3 levels)
- Array#uniq!: reduce O(n²) to O(n) pointer calls in linear path
- Array#disjoint?: hoist both array pointers in nested loop

Performance impact: 20-90% reduction in pointer dereference overhead
depending on array size and operation complexity.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:46 +09:00
Yukihiro "Matz" Matsumoto 2965113052 mruby-io: add helper for int64_t to mrb_value conversion
Fixes MSVC warnings on 32-bit builds when converting st_size (int64_t) to
mrb_int. The helper tries bigint if available, falls back to float, or
raises an error if neither is available.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:45 +09:00
Yukihiro "Matz" Matsumoto 7c0e42dd31 Merge pull request #6644 from mruby/dependabot/github_actions/super-linter/super-linter-8.2.1 2025-10-20 10:53:21 +09:00
Yukihiro "Matz" Matsumoto bbf46a4355 mruby-io: raise NotImplementedEerror for FileTest.pipe? on Windows
add Windows guard to FileTest.pipe? to raise NotImplementedError,
consistent with symlink? and socket?. Windows anonymous pipes created
by IO.pipe are not UNIX FIFOs and cannot be detected via stat mode
bits. the test suite expects this exception and handles it with skip.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-20 08:18:38 +09:00
Yukihiro "Matz" Matsumoto a28ada5e5e hal-win-io: fix backtick operator on windows
on windows, pipe handles created by _pipe are marked non-inheritable
for security by mrb_hal_io_pipe. when spawning child processes via
io.popen (used by backtick operator), the child needs to inherit
stdin/stdout/stderr handles to communicate with the parent process.

before calling createprocess, explicitly set handle_flag_inherit on
the stdio handles so child processes can use them. this fixes the
backtick operator returning empty strings on windows (msvc and mingw,
both 32-bit and 64-bit).

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-20 08:11:59 +09:00
Yukihiro "Matz" Matsumoto fec629309f mruby-strftime: prevent crash on msvc with unsupported %- flag
add validation to detect gnu extension %- flag and raise argumenterror
on msvc instead of crashing. update test to use portable %m format.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-19 07:33:45 +09:00
Yukihiro "Matz" Matsumoto f5f0b9ac51 mruby-compiler: fix pr #6643 to use array_node elements field
pr #6643 fixed node type check but introduced a bug by accessing t->cdr
on a NODE_ARRAY structure. NODE_ARRAY nodes use the elements field, not
cdr. additionally, the fixed rhs path needs to update rhs_reg to point
to where values are actually pushed on the stack.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-19 07:29:32 +09:00
Yukihiro "Matz" Matsumoto dc7c6ed7a7 mruby-fiber,mruby-task: increase stack init size for 32-bit msvc
increase fiber_stack_init_size and task_stack_init_size from 16 to 64
to fix crashes on 32-bit msvc builds. git bisect identified commit
3246dd2 (which reduced sizes from 64 to 16) as causing the issue.
empirical testing shows 48 fails intermittently but 64 is stable on
32-bit msvc, likely due to different alignment or initialization
overhead on 32-bit platforms.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-18 23:34:10 +09:00
Yukihiro "Matz" Matsumoto 5efd0eeed2 mruby-io: normalize line endings in backtick command test
use chomp to strip line endings from backtick command output, making the
test platform-agnostic. remove unused $crlf variable since line ending
checks are now handled by chomp.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-18 23:34:09 +09:00
Yukihiro "Matz" Matsumoto f8ffc05648 Merge pull request #6643 from meder/patch-1 2025-10-18 22:52:00 +09:00
dependabot[bot] a6720afed9 build(deps): bump super-linter/super-linter from 8.2.0 to 8.2.1
Bumps [super-linter/super-linter](https://github.com/super-linter/super-linter) from 8.2.0 to 8.2.1.
- [Release notes](https://github.com/super-linter/super-linter/releases)
- [Changelog](https://github.com/super-linter/super-linter/blob/main/CHANGELOG.md)
- [Commits](https://github.com/super-linter/super-linter/compare/v8.2.0...v8.2.1)

---
updated-dependencies:
- dependency-name: super-linter/super-linter
  dependency-version: 8.2.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-17 14:01:50 +00:00
Meder Kydyraliev af33eec793 Fix crash caused by an incorrect node type check in codegen_masgn
Use the dedicated function `get_node_type(t)` to correctly determine the node type, instead of the incorrect `node_to_int(t->car)` check.

OSS-Fuzz issue details:
https://oss-fuzz.com/testcase-detail/4760884737277952
https://g-issues.oss-fuzz.com/issues/449498801
2025-10-17 10:03:01 +11:00
Yukihiro "Matz" Matsumoto ee5a6705ee mruby-io: fix cross-platform test compatibility for windows
make symlink operations raise notimplementederror on Windows since
symlinks require special privileges and differ significantly from posix.
similarly, filetest.socket? and filetest.symlink? now raise
notimplementederror on Windows since these file types don't exist in
the same way. the file.chmod test now restores write permissions before
deletion, which is required on Windows to delete read-only files.

all tests already have rescue notimplementederror clauses that skip
gracefully on unsupported platforms.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 23:59:23 +09:00
Yukihiro "Matz" Matsumoto 4d235444c0 mruby-dir: use hal-win-dir for mingw
mingw provides dirent.h for directory reading but filesystem functions
like mkdir use windows signatures (1 argument) not posix (2 arguments).
chroot is also unavailable on mingw. removed mingw from linux/bsd
pattern to let for_windows? predicate select hal-win-dir instead.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 23:28:27 +09:00
Yukihiro "Matz" Matsumoto 917add467b mruby-socket: use hal-win-socket for mingw
mingw uses winsock2 instead of posix sockets (sys/socket.h). removed
mingw from linux/bsd pattern to let for_windows? predicate select
hal-win-socket instead.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 22:45:19 +09:00
Yukihiro "Matz" Matsumoto ec34c93349 mruby-io: use hal-win-io for mingw due to lack of fork/waitpid
mingw provides posix file i/o apis but not unix process management
functions (fork, waitpid) which are required by hal-posix-io. removed
mingw from linux/bsd pattern to let for_windows? predicate select
hal-win-io instead.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 19:14:54 +09:00
Yukihiro "Matz" Matsumoto e540547413 mruby-task: use hal-win-task for mingw builds
mingw provides posix compatibility for file i/o but not for signal
handling. hal-posix-task relies on SIGALRM, setitimer(), and
sigprocmask() which are not available on windows even through mingw.

changed hal selection for mruby-task to use hal-win-task for mingw,
while mruby-dir, mruby-io, and mruby-socket correctly use posix hals
for mingw since those features are supported.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 19:00:06 +09:00
Yukihiro "Matz" Matsumoto feca90ceab hal: fix selection to use toolchain instead of RUBY_PLATFORM
when building with MSVC on Windows, RUBY_PLATFORM (from the Ruby
installation running rake) may indicate "mingw" if Ruby was installed
via RubyInstaller, causing incorrect selection of POSIX HALs instead
of Windows HALs.

fixed by checking spec.build.primary_toolchain first:
- if toolchain is "visualcpp", select Windows HALs
- otherwise fall through to existing platform checks

this ensures MSVC builds use hal-win-* gems even when Ruby itself
was installed with MinGW.

affected gems:
- mruby-dir
- mruby-io
- mruby-socket
- mruby-task

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 18:20:32 +09:00
Yukihiro "Matz" Matsumoto 3e10aaf6c1 mruby-compiler: add stmts_push helper to fix incorrect push usage
added stmts_push(p, stmts, stmt) helper function to properly push
statements to NODE_STMTS nodes by accessing the internal stmts field
(a cons list). this avoids ugly casts and prevents bugs.

fixed incorrect usage in:
- top_stmts rule (line 2081): was calling push($1, ...) directly on
  NODE_STMTS instead of pushing to $1->stmts
- bodystmt rule (line 2114): same issue when handling else without
  rescue
- stmts rule (line 2146): simplified to use new helper for consistency

the push macro works on cons lists, not NODE_STMTS variable nodes.
the new helper encapsulates the cast and provides type-safe access.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 18:01:04 +09:00
Yukihiro "Matz" Matsumoto 6dd5f05525 mruby-random: split 64-bit state into two 32-bit values on 32-bit platforms
on 32-bit systems, the rand_state struct with uint64_t state (8 bytes,
8-byte aligned) followed by uint32_t seed_value (4 bytes) resulted in
16 bytes due to padding, exceeding the 12-byte ISTRUCT_DATA_SIZE limit.
this caused the static_assert at line 540 to fail.

split the state field into state_lo and state_hi on MRB_32BIT platforms
to achieve perfect 12-byte alignment (4+4+4) without padding. add
GET_STATE/SET_STATE macros to provide uniform access across platforms.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 17:50:03 +09:00
Yukihiro "Matz" Matsumoto 1efaaa5570 mruby-io,mruby-dir: improve mingw detection for native builds
add mingw pattern to RUBY_PLATFORM check. native mingw builds were
falling through to windows hal because previous detection only worked
for cross-compilation. now checks RUBY_PLATFORM for mingw along with
linux/darwin/bsd.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 17:38:37 +09:00
Yukihiro "Matz" Matsumoto 0c849e9020 mruby-bigint: add explicit cast to mp_limb for range-checked values
add explicit cast when assigning mrb_int to mp_limb. the value is
already validated to fit within mp_limb range by checking against
DIG_BASE, but explicit cast silences msvc warning c4244.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 17:06:31 +09:00
Yukihiro "Matz" Matsumoto e8bcfa71c3 hal-win-task: fix integer conversion warning on msvc
add explicit cast to DWORD when passing usec to Sleep(). Sleep() takes
32-bit DWORD but usec is mrb_int which can be 64-bit, causing warning
c4244.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 17:02:23 +09:00
Yukihiro "Matz" Matsumoto 3d1c4981e7 mruby-io: conditionally compile mrb_lstat for symlink support
only define mrb_lstat when symbolic link macros are available. on
windows/mingw, symlinks are not supported and the function is unused,
causing -Wunused-function warning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:56:42 +09:00
Yukihiro "Matz" Matsumoto eba41be3a2 hal-win-dir: rewrite comment to avoid comment nesting warning
remove example containing /* sequence from comment. this triggers
-Wcomment warning on mingw about nested comments.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:54:25 +09:00
Yukihiro "Matz" Matsumoto ed4fb26d75 hal-win-socket: guard _WIN32_WINNT definition to prevent redefinition
only define _WIN32_WINNT if not already defined. mingw headers may
predefine this macro, causing redefinition warning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:54:11 +09:00
Yukihiro "Matz" Matsumoto 34205d6ba3 mruby-task: fix integer conversion warning by using uint32_t for sleep functions
change sleep_us_impl and sleep_ms_impl parameters from mrb_int to uint32_t.
this makes the type requirement explicit and resolves msvc warning c4244.
all type conversions happen at ruby boundary functions after validation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:34:01 +09:00
Yukihiro "Matz" Matsumoto 0082dfb7e5 mruby-random: fix unary minus on unsigned type warning
replace (-rot) with (32 - rot) to avoid msvc warning c4146. both
expressions are equivalent when masked with & 31, but the latter
is clearer and doesn't trigger warnings about negating unsigned values.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:12:11 +09:00
Yukihiro "Matz" Matsumoto f25fadf46c mruby-io: fix const qualifier warnings on msvc
remove const qualifier from variables passed to free functions.
msvc is stricter about const correctness than gcc. variables from
mrb_utf8_from_locale and mrb_locale_from_utf8 are dynamically allocated
and need to be freed, so they should not be const.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:10:52 +09:00
Yukihiro "Matz" Matsumoto 91922e05af mruby-io,mruby-dir: use posix hal for mingw instead of windows hal
mingw provides posix-compatible functions (readlink, symlink, opendir, etc.)
so it should use hal-posix-io/dir instead of hal-win-io/dir. detect mingw by
checking if host_target or compiler command contains "mingw". check posix
platforms first so mingw is caught before for_windows check.

this fixes test failures on mingw where readlink returned absolute paths
instead of relative paths, and symlink/socket tests failed due to api
differences between windows native apis and posix apis.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:05:49 +09:00
Yukihiro "Matz" Matsumoto b424dfa331 mruby-dir: increase buffer size to prevent truncation warning
increase sandbox path buffer from 1024 to 2048 bytes to accommodate
full path with suffix without truncation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 15:49:45 +09:00
Yukihiro "Matz" Matsumoto 43695029bc mruby-dir: add missing unistd.h include for posix systems
mkdtemp() requires unistd.h on posix systems like macos and linux.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 15:44:51 +09:00