# Rb\_require() causes segfault

**URL:** <https://www.ruby-forum.com/t/rb-require-causes-segfault/147439>\
**Category:** Ruby\
**Created:** [September 1, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/rb-require-causes-segfault/147439 "2008-09-01T20:48:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [September 1, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/rb-require-causes-segfault/147439/1 "2008-09-01T20:48:27Z")

</div>

Hi,

calling rb\_require() with a file name that will not be found,  
causes a segmentation fault to be raised:

ruby: [BUG] Segmentation fault  
ruby 1.8.6 (2008-08-11) [i386-freebsd7]

Abort trap: 6 (core dumped)

It’s the same with Linux (both gcc). I googled and found some  
answers that say: “Link with the -rdynamic flag.” That did not  
help.

Could anyone give me a hint what is going on here? At least a  
point to a working example should suffice.

Thanks in advance,

Bertram

```
                       Here's my code:

```

* * *

#include \<ruby.h\>

VALUE call\_require( VALUE name)  
{  
if (0) rb\_raise( rb\_eStandardError, “don’t do it”);  
return rb\_require( (char \*) name);  
}

VALUE rescue\_require( VALUE data, VALUE err)  
{  
printf(“Error: [[[%s]]]\n”, RSTRING(rb\_obj\_as\_string(err))-\>ptr);  
return Qnil;  
}

void sure\_require( char \*name)  
{  
rb\_rescue( &call\_require, (VALUE) name, &rescue\_require, Qnil);  
}

int main( int argc, char \*argv)  
{  
ruby\_init();  
ruby\_init\_loadpath();  
sure\_require( “nonexistent”);  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [September 2, 2008, 4:50am UTC](https://www.ruby-forum.com/t/rb-require-causes-segfault/147439/2 "2008-09-02T04:50:11Z")

</div>

Hi,

Am Dienstag, 02. Sep 2008, 03:42:29 +0900 schrieb Bertram S.:

> calling rb\_require() with a file name that will not be found,  
> causes a segmentation fault to be raised:

I’m still about to convince my colleagues that Ruby is the  
programming language of concern. Failing in this subject would  
spoil this whole project.

Probably I could compile Ruby with debugging information and  
search the cause on my own. This is exactly the subject I  
unlearned since I use Ruby.

So please, if somebody could give me a slight hint. It would save  
me so much time and tension.

Thanks in advance.

Bertram

---

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [September 2, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/rb-require-causes-segfault/147439/3 "2008-09-02T12:42:19Z")

</div>

Hi,

Am Dienstag, 02. Sep 2008, 03:42:29 +0900 schrieb Bertram S.:

> calling rb\_require() with a file name that will not be found,  
> causes a segmentation fault to be raised:
> 
> ruby: [BUG] Segmentation fault  
> ruby 1.8.6 (2008-08-11) [i386-freebsd7]
> 
> Abort trap: 6 (core dumped)

OK, I start to debug it.

The segfault happens in rb\_rescue2()/eval.c, line 5501 at a  
JUMP\_TAG() statement. This is when the LoadError exception is  
being handled, not in the actual rb\_require() itself.

When I comment in the rb\_raise() call below, the segfault won’t  
happen.

Would this information help somebody enlighten me what is going on  
here?

Thanks in advance.

Bertram

```
                       Here's my code:

```

* * *

#include \<ruby.h\>

VALUE call\_require( VALUE name)  
{  
if (0) rb\_raise( rb\_eStandardError, “don’t do it”);  
return rb\_require( (char \*) name);  
}

VALUE rescue\_require( VALUE data, VALUE err)  
{  
printf(“Error: [[[%s]]]\n”, RSTRING(rb\_obj\_as\_string(err))-\>ptr);  
return Qnil;  
}

void sure\_require( char \*name)  
{  
rb\_rescue( &call\_require, (VALUE) name, &rescue\_require, Qnil);  
}

int main( int argc, char \*argv)  
{  
ruby\_init();  
ruby\_init\_loadpath();  
sure\_require( “nonexistent”);  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [September 2, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/rb-require-causes-segfault/147439/4 "2008-09-02T22:11:43Z")

</div>

Hi,

Am Dienstag, 02. Sep 2008, 19:36:05 +0900 schrieb Bertram S.:

> The segfault happens in rb\_rescue2()/eval.c, line 5501 at a  
> JUMP\_TAG() statement. This is when the LoadError exception is  
> being handled, not in the actual rb\_require() itself.

I just debugged into the JUMP\_TAG() macro. Indeed the `prot\_tag’  
pointer is NULL. Is this a bug to be reported?

I tried 1.8.6-p114 and 1.8.6-p287 versions.

Thanks in advance.

Bertram

```
                       Here's my code:

```

* * *

#include \<ruby.h\>

VALUE call\_require( VALUE name)  
{  
if (0) rb\_raise( rb\_eStandardError, “don’t do it”);  
return rb\_require( (char \*) name);  
}

VALUE rescue\_require( VALUE data, VALUE err)  
{  
printf(“Error: [[[%s]]]\n”, RSTRING(rb\_obj\_as\_string(err))-\>ptr);  
return Qnil;  
}

void sure\_require( char \*name)  
{  
rb\_rescue( &call\_require, (VALUE) name, &rescue\_require, Qnil);  
}

int main( int argc, char \*argv)  
{  
ruby\_init();  
ruby\_init\_loadpath();  
sure\_require( “nonexistent”);  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [September 3, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/rb-require-causes-segfault/147439/5 "2008-09-03T13:23:40Z")

</div>

Hi,

Am Mittwoch, 03. Sep 2008, 12:05:40 +0900 schrieb Nobuyoshi N.:

> At Wed, 3 Sep 2008 05:05:52 +0900,  
> Bertram S. wrote in [ruby-talk:313696]:
> 
> > I just debugged into the JUMP\_TAG() macro. Indeed the `prot\_tag’  
> > pointer is NULL. Is this a bug to be reported?
> 
> No thank you.

Sorry. I was just irritated about getting absolutely no answer on  
such a simple question.

> > void sure\_require( char \*name)  
> > {  
> > rb\_rescue( &call\_require, (VALUE) name, &rescue\_require, Qnil);
> 
> use rb\_protect() instead of rb\_rescue(), the latter catches  
> only StandardError and its subclasses.

I didn’t realize that LoadError is not a subclass of  
StandardError. Now it is obvious to me.

```
rb_rescue2( &call_require, (VALUE) name, &rescue_require, Qnil,
                    rb_eLoadError, (VALUE) 0);

```

T-h-a-n-k y-o-u!

Bertram

---

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [September 3, 2008, 5:11am UTC](https://www.ruby-forum.com/t/rb-require-causes-segfault/147439/6 "2008-09-03T05:11:36Z")

</div>

Hi,

At Wed, 3 Sep 2008 05:05:52 +0900,  
Bertram S. wrote in [ruby-talk:313696]:

> I just debugged into the JUMP\_TAG() macro. Indeed the `prot\_tag’  
> pointer is NULL. Is this a bug to be reported?

No thank you.

> void sure\_require( char \*name)  
> {  
> rb\_rescue( &call\_require, (VALUE) name, &rescue\_require, Qnil);

use rb\_protect() instead of rb\_rescue(), the latter catches  
only StandardError and its subclasses.
