Skip to content

Commit ce609fd

Browse files
committed
Workaround for a SEGV in Ruby 3.2
rb_errinfo() crashes when the cause of the last longjmp is not an exception.
1 parent 8649a63 commit ce609fd

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

ext/jsonnet/callbacks.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
*/
1212
#define RUBYJSONNET_GLOBAL_ESCAPE_MAGIC "\x07\x03\x0c:rubytag:\x07\x03\x0c:"
1313

14+
/* Copied from vm_core.h in Ruby. State variables in global escapes have
15+
* this value when an exception is raised.
16+
*
17+
* TODO(yugui) Find a better way to distinguish "raise" from "throw".
18+
* It is not a very good idea to depend on the implementation details of Ruby.
19+
*/
20+
#define RUBY_TAG_RAISE 0x6
21+
1422
/*
1523
* callback support in VM
1624
*/
@@ -44,9 +52,9 @@ invoke_callback(VALUE args)
4452
static VALUE
4553
rescue_callback(int state, const char *fmt, ...)
4654
{
47-
VALUE err = rb_errinfo();
48-
if (rb_obj_is_kind_of(err, rb_eException)) {
49-
VALUE msg = rb_protect(rubyjsonnet_format_exception, rb_errinfo(), NULL);
55+
if (state == RUBY_TAG_RAISE) {
56+
VALUE err = rb_errinfo();
57+
VALUE msg = rb_protect(rubyjsonnet_format_exception, err, NULL);
5058
if (msg == Qnil) {
5159
va_list ap;
5260
va_start(ap, fmt);

test/test_vm.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class TestVM < Test::Unit::TestCase
5454
end
5555
end
5656

57-
5857
test 'Jsonnet::VM#evaluate evaluates snippet' do
5958
vm = Jsonnet::VM.new
6059
result = vm.evaluate(<<-EOS, filename: 'example.snippet')

0 commit comments

Comments
 (0)