127 Commits
Author SHA1 Message Date
Hiroshi SHIBATAandClaude Opus 5.5 ee3d6c987b Use rb_len_t for the lengths and indices left in long
These still held String and Array lengths and indices in long, or cast them to long. rb_len_t is still long everywhere, so the generated code does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:45:25 +09:00
Hiroshi SHIBATAandClaude Opus 5 975c2a45f1 Use rb_len_t for Array and String lengths in the VM
Argument counts stay `int` or `long`. The int range checks in proc.c and compile.c now depend on SIZEOF_VALUE so that they are kept once rb_len_t widens.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-09 14:00:15 +09:00
Jean Boussier 56abc30ec2 Righ-size more hashes
The pattern of allocating an empty hash with `rb_hash_new()` and
then appending to it is relatively wasteful, because every time
we go over the hash capacity, we'll have to `realloc`, rehash etc.

It may even get worse if https://github.com/ruby/ruby/pull/18345
lands, as right now an empty hash starts with a capacity of 8,
but it may very well start with a capacity of 2.

Hence in cases where the final hash size is easy to predict, we
should use `rb_hash_new_capa()`.
2026-08-23 11:50:40 +02:00
Shugo MaedaandClaude Fable 5 79d4e720f8 Make locals read after longjmp volatile in more setjmp users
Audit of every EC_PUSH_TAG user, following the LLVM 19 longjmp
rematerialization bug worked around in rb_fiber_start: locals and
arguments that are read on the longjmp path but not modified inside
the tag region are made volatile.  C99 7.13.2.1p3 guarantees such
unmodified locals, but LLVM 17 and 19 have been observed to
reconstruct their values incorrectly after a longjmp (cfp in
rb_iterate0, fiber in rb_fiber_start).  volatile on a parameter may
not be effective (67d0f4821f,
https://bugs.ruby-lang.org/issues/19145), so arguments are copied
into volatile locals instead.

* vm_eval.c (rb_iterate0): cfp is compared against the escaping
  frame after a longjmp; clang 17 at -O1 with -fPIC was observed to
  clobber it during the development of the ifunc invalidation for
  https://bugs.ruby-lang.org/issues/22019.  ec is read next to it to
  rewind the frame and to rethrow.

* eval_error.c (rb_ec_error_print_detailed0): opt and highlight are
  read on the path reached by a longjmp from rb_get_backtrace while
  the other locals of the function are already volatile.

* scheduler.c (rb_fiber_scheduler_unblock, fiber_interrupt): ec and
  saved_interrupt_mask are read after a longjmp to restore
  ec->interrupt_mask; cfp next to them was already volatile.

* eval.c (rb_vrescue2): r_proc and data2 are used when the raise is
  handled.  args is left as is; a va_list cannot portably be
  volatile-qualified and is memory-backed in practice.

* eval.c (rb_protect): pstate is written through after the tag is
  popped.

* eval.c (rb_ec_ensure): ec, e_proc and data2 run the ensure proc
  after a longjmp; a clobbered e_proc would miscall it.

* vm_eval.c (vm_catch_protect): stateptr is written through after
  the pop.  tag has its address taken and stays in memory.

* vm_trace.c (exec_hooks_protected): ec and list are passed to
  exec_hooks_postcheck after the pop.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-23 10:37:13 +09:00
Nobuyoshi Nakada 0d7e50a174 [Bug #22191] Report EXIT handler exceptions after END blocks
Keep exceptions raised by `EXIT` traps until `END` handlers finish.
That leaves them available for reporting during cleanup.

Print `END` handler exceptions without replaying causes already
reported from earlier exit handlers. This keeps the previous handler
exception visible while avoiding duplicate diagnostics.
2026-07-13 04:22:19 +09:00
Yusuke Endoh 25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Peter Zhu b959263b58 Fix Exception#detailed_message for GC compaction
Before this commit, the test fails with RGENGC_CHECK_MODE enabled:

    TestException#test_detailed_message_under_gc_compact_stress [test/ruby/test_exception.rb:1466]:
    <"\e[1mfoo (\e[1;4mRuntimeError\e[m\e[1m)\e[m\n" +
    "\e[1mbar\e[m\n" +
    "\e[1mbaz\e[m"> expected but was
    <"\e[1mfoo (\e[1;4mRuntimeError\e[m\e[1m)\e[m\n" +
    "\e[1m\x00\x00\x00\x00\x00\x00\x00\e[m">.
2024-01-02 12:03:31 -05:00
Nobuyoshi Nakada fe4d906f5f [Bug #19774] Fix segfault at return in END 2023-07-19 03:40:05 +09:00
Nobuyoshi Nakada dc5f938765 Prefer write_warn over write_warn2 with a string literal 2023-04-10 15:23:44 +09:00
Nobuyoshi Nakada c30cab8ce4 [Bug #19570] Propagate message encoding to decorated message 2023-04-10 15:13:26 +09:00
Nobuyoshi Nakada 2e7e153a2a [Bug #19242] Prohibit circular causes to be loaded 2022-12-20 14:12:38 +09:00
Nobuyoshi Nakada 67d0f4821f [Bug #19145] volatile on an argument may not be effective 2022-11-25 15:50:06 +09:00
Nobuyoshi Nakada 5b959e238e [Bug #19016] re-order error handling at cleanup
Build and store the error message with `#detailed_message` before
terminating all Ractors, then show the message later.
2022-11-22 00:33:19 +09:00
Nobuyoshi Nakada 12b7b85227 Use enum ruby_tag_type over int 2022-11-20 20:00:40 +09:00
S-H-GAMELINKS 1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Yusuke Endoh eda10e8102 Remove unneeded newline from detailed_message 2022-02-22 11:55:40 +09:00
Yusuke Endoh 35ff545bb6 Exception#detailed_message is added
Also, the default error printer and Exception#full_message use the
method instead of `Exception#message` to get the message string.

`Exception#detailed_message` calls `Exception#message`, decorates and
returns the result. It adds some escape sequences to highlight, and the
class name of the exception to the end of the first line of the message.

[Feature #18370]
2022-02-22 11:55:40 +09:00
Yusuke Endoh 3af316fa8c Refactor out rb_decorate_message from print_errinfo
rb_decorate_message adds bold escape sequences to message, and the class
name of exception (like " (RuntimeError)) of "message (RuntimeError)").
2022-02-22 11:55:40 +09:00
Yusuke Endoh f207f7a193 Do not escape error message
[Feature #18367]
2022-02-22 11:55:40 +09:00
Yusuke Endoh 36e31b09cd error.c: Refactoring
Factor out from rb_error_write the responsibility to check if stderr is
a tty.
2022-02-22 11:55:40 +09:00
OKURA Masafumi 8d5292f702 Trim extra whitespace in inaccesible error message
One whitespace is already included in `inaccessible_mesg_for` macro.
This extra whitespace is included since 72ff61f4a8
2021-09-01 13:18:55 +09:00
Nobuyoshi Nakada f6a080047e Limit bactrace length from Exception#full_message 2020-12-27 20:50:15 +09:00
Yusuke Endoh eee93bc933 eval_error.c: fix off-by-one error for --backtrace-limit
https://bugs.ruby-lang.org/issues/17413
2020-12-21 11:40:48 +09:00
Nobuyoshi Nakada 579645d9f8 Fixed infinite loop at error in printing cause [Bug #17033] 2020-07-15 21:39:20 +09:00
Yusuke Endoh 39365b46e2 Merge pull request #3047 from mame/suppress-backtrace
Add `--suppress-backtrace=num` option to limit the backtrace length
2020-05-15 01:22:56 +09:00
Nobuyoshi Nakada 3ef09c6e66 eval_error.c: default nil as "reverse" is now false [Feature #8661] 2020-04-21 19:29:16 +09:00
Yusuke Endoh 487d0c99d5 eval_error.c: revert the "reversed" backtrace [Feature #8661]
Now, the order is good, old-fashioned style:

```
$ ./local/bin/ruby -e 'def foo; raise; end
def bar; foo; end
def baz; bar; end
def qux; baz; end
qux
'
-e:1:in `foo': unhandled exception
	from -e:2:in `bar'
	from -e:3:in `baz'
	from -e:4:in `qux'
	from -e:5:in `<main>'
```
2020-04-21 17:39:32 +09:00
Nobuyoshi Nakada 155f64e3c4 Raise EPIPE at broken pipe for the backward compatibility
Instead of SignalException for SIGPIPE, raise `Errno::EPIPE` with
instance variable `signo` and re-send that signal at exit.
[Feature #14413]
2020-04-15 21:05:39 +09:00
Nobuyoshi Nakada 0c2d81dada Renamed ruby_finalize_{0,1}
And pass rb_execution_context_t as an argument.
2019-08-13 09:47:08 +09:00
Nobuyoshi Nakada 0c6c937904 Removed duplicate highlighting 2019-07-15 19:39:47 +09:00
git 5a42dca688 * expand tabs. 2019-07-15 16:51:18 +09:00
Nobuyoshi Nakada 5e0d27a32f Removed dead code
If `emesg` is `Qundef`, it is not a message string and then `elen`
(the length of the message) is 0.  So `emesg` cannot be `Qundef` in
the `elen != 0` block.  Pointed out by Coverity Scan.
2019-07-15 16:49:39 +09:00
nobu c6fb7c1f33 eval_error.c: should not escapce newline and tab
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66917 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-24 10:10:18 +00:00
svn 497075b3a1 * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66754 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-08 09:08:37 +00:00
nobu 50784a0a44 Defer escaping control char in error messages
* eval_error.c (print_errinfo): defer escaping control char in
  error messages until writing to stderr, instead of quoting at
  building the message.  [ruby-core:90853] [Bug #15497]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66753 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-08 09:08:31 +00:00
nobu 1849288722 Fix for circular causes
* eval_error.c (show_cause): get rid of infinite recursion on
  circular causes.  [Bug #15447]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-22 07:14:14 +00:00
mame 010a742cb6 eval_error.c (show_cause): check if cause is an Exception or not
Fixes [Bug #15447]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66475 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-21 11:44:21 +00:00
shyouhei 06e2873ed1 eval_error.c: log10(0) is ERANGE
By definition, the logarithm of 0 is negative infinity.  This is a
pole error (cf: cf: ISO/IEC 9899:1999 section 7.12.1 paragraph 3) and
of course, cannot fit into an `int` value.  We have to resort to
INT_MIN.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65743 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-15 09:03:03 +00:00
naruse 7594a8df75 fix error message and backtrace order
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65396 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-27 23:13:34 +00:00
naruse 48af6fd544 Print exception's cause like Java
Print `cause` of the exception if the exception is not caught and printed
its backtraces and error message [Feature #8257]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65393 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-27 21:45:30 +00:00
normal 62c3610583 rb_execution_context_t: pack bits, 256 -> 252 bytes (on 32-bit)
Maybe execution contexts will become more common.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65183 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-19 20:56:10 +00:00
nobu fa8b08b424 Prefer rb_fstring_lit over rb_fstring_cstr
The former states explicitly that the argument must be a literal,
and can optimize away `strlen` on all compilers.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65059 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-13 09:59:22 +00:00
nobu aa2b32ae4b eval_error.c: fix loop on exception in message
* error.c (rb_get_message): accessor to the message.

* eval_error.c (rb_ec_error_print): handle exceptions on fetching
  the message.  [Bug #14566]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-11 08:03:43 +00:00
nobu 1032f093cc eval_error.c: reset attributes
* eval_error.c (print_errinfo): reset all attributes for each
  lines before newlines.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62737 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-13 03:48:56 +00:00
nobu b07e4af869 eval_error.c: last newline
* eval_error.c (print_errinfo): do not print an empty line at the
  end when the message ends with a newline.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62736 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-13 03:40:10 +00:00
nobu 2851b2c55f eval_error.c: fix underflow
* eval_error.c (print_errinfo): get rid of negative string length.
  [ruby-core:86086] [Bug #14598]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62728 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-12 13:04:28 +00:00
nobu daad618740 eval_error.c: rb_error_write flags
* eval_error.c (rb_error_write): add highlight and reverse mode
  flags.  defaulted to rb_stderr_tty_p() if Qnil.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-23 08:39:03 +00:00
nobu 89f0419575 eval_error.c: reset in each line
* eval_error.c (print_errinfo): reset in each line, so that
  Exception#full_message ends with a newline and puts will not
  print an extra newline.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61650 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-07 05:38:37 +00:00
nobu ac4d348d45 eval_error.c: concat directly
* eval_error.c (write_warn, write_warn2): get rid of unnecessary
  intermediate string objects, concat directly.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61649 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-07 04:57:04 +00:00