segfault with rb_eval_string()

  • Thread starter Elias Athanasopoulos
  • Start date
E

Elias Athanasopoulos

Hello!

I have a segfault when I use rb_eval_string() through a shared
library.

elathan@velka:~/TRuby/tmp> ruby -v
ruby 1.9.0 (2004-05-25) [i686-linux]
elathan@velka:~/TRuby/tmp> cat bug.c
#include "ruby.h"

int doit(void)
{
rb_eval_string("puts");
}

elathan@velka:~/TRuby/tmp> gcc bug.c -shared -o libbug.so \
-I/usr/local/lib/ruby/1.9/i686-linux/ -L/usr/local/include \
-lruby-static -ldl -lcrypt -lm

elathan@velka:~/TRuby/tmp> cat runit.c
int main(void)
{
doit();
}
elathan@velka:~/TRuby/tmp> gcc runit.c -lbug -L. -o runit
elathan@velka:~/TRuby/tmp> ./runit
Segmentation fault

elathan@velka:~/TRuby/tmp> gdb runit
GNU gdb 5.3
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i586-suse-linux"...
(gdb) run
Starting program: /home/elathan/TRuby/tmp/runit
Error while mapping shared library sections:
libbug.so: Success.

Program received signal SIGSEGV, Segmentation fault.
0x400a75ff in ?? ()
(gdb) where
#0 0x400a75ff in ?? ()
#1 0x40068f9c in ?? ()
#2 0x40050531 in ?? ()
#3 0x4004e52b in ?? ()
#4 0x08048469 in main ()
#5 0x4011e8ae in __libc_start_main () from /lib/libc.so.6
(gdb)

Regards,
 
Y

Yukihiro Matsumoto

Hi,

In message "segfault with rb_eval_string()"

|I have a segfault when I use rb_eval_string() through a shared
|library.

You are calling Ruby function without initialization.

matz.
 
E

Elias Athanasopoulos

Hi,

In message "segfault with rb_eval_string()"

|I have a segfault when I use rb_eval_string() through a shared
|library.

You are calling Ruby function without initialization.

Thank you, I used ruby_init() before rb_eval_string() and it worked.
However, when the string to be evaluated is not a valid Ruby command
the interpreter segfaults, instead of just printing the error.

For an rb_eval_string("puts '"):

elathan@velka:~/TRuby/tmp> ./runit
(eval): (eval): compile error (SyntaxError)
(eval): unterminated string meets end of file(eval): [BUG] Segmentation fault
ruby 1.9.0 (2004-05-25) [i686-linux]

Aborted

I would like to be able to handle the error.

Regards,
 
E

Elias Athanasopoulos

E> For an rb_eval_string("puts '"):

Use rb_eval_string_protect()

Thanks. BTW, why error_print() is not exported in the
Ruby headers. Is there another function in the API to dump
the latest error of the interpreter?

Regards,
 
T

ts

E> Thanks. BTW, why error_print() is not exported in the
E> Ruby headers. Is there another function in the API to dump
E> the latest error of the interpreter?

You have $!, $@, rb_backtrace()


Guy Decoux
 
T

Tim Sutherland

Hello!

I have a segfault when I use rb_eval_string() through a shared
library.

elathan@velka:~/TRuby/tmp> ruby -v
ruby 1.9.0 (2004-05-25) [i686-linux]
elathan@velka:~/TRuby/tmp> cat bug.c
#include "ruby.h"

int doit(void)
{
rb_eval_string("puts");
}

elathan@velka:~/TRuby/tmp> gcc bug.c -shared -o libbug.so \
-I/usr/local/lib/ruby/1.9/i686-linux/ -L/usr/local/include \
-lruby-static -ldl -lcrypt -lm

elathan@velka:~/TRuby/tmp> cat runit.c
int main(void)
{
doit();
}
elathan@velka:~/TRuby/tmp> gcc runit.c -lbug -L. -o runit
elathan@velka:~/TRuby/tmp> ./runit
Segmentation fault

(I'm on the newsgroup, perhaps this has already been answered on the mailing
list.)

Before calling any other Ruby functions, try:

#ifdef _WIN32
NtInitialize(&argc, &argv);
#endif
#if defined(__MACOS__) && defined(__MWERKS__)
argc = ccommand(&argv);
#endif

ruby_init();

You might be able to get away with just using ruby_init(), but e.g.
NtInitialize calls the win32 function StartSockets() to "Initialize
Winsock".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,301
Latest member
SusannaCgx

Latest Threads

Top