当试图从c代码调用ruby regex时崩溃

n53p2ov0  于 2023-03-22  发布在  Ruby
关注(0)|答案(1)|浏览(167)

这篇文章是我上一篇文章的延续:(通过C API从C代码调用ruby正则表达式不起作用)
我做了一些修改,现在我用“*”调用rb_reg_regcomp。

#include <ruby.h>
#include "ruby/re.h"

#define MAX_INPUT_SIZE 1000

int main(int argc, char** argv) {
    VALUE x;
    char string[MAX_INPUT_SIZE];
    int result;
    result = ruby_setup();
    ruby_init();

    ruby_init_loadpath();



    memset(string, 0, MAX_INPUT_SIZE);

    fgets(string, MAX_INPUT_SIZE, stdin);

    if (string[MAX_INPUT_SIZE-2]) {
        return 0;
    }

    //printf("thing");
    x = rb_str_new_cstr("*");
    rb_reg_regcomp(x);



    

    return 0;

}

现在,当我运行这个程序,然后按回车键,我在gdb中得到:

Program received signal SIGSEGV, Segmentation fault.
0x000055555565bef6 in rb_ec_tag_jump (st=st@entry=RUBY_TAG_RAISE, ec=<optimized out>) at ../eval_intern.h:161
161     ec->tag->state = st;
(gdb) where
#0  0x000055555565bef6 in rb_ec_tag_jump (st=st@entry=RUBY_TAG_RAISE, ec=<optimized out>) at ../eval_intern.h:161
#1  0x0000555555661fe0 in rb_longjmp (ec=ec@entry=0x6160000000d0, tag=tag@entry=6, mesg=<optimized out>, mesg@entry=140737288676920, cause=<optimized out>, cause@entry=36) at ../eval.c:658
#2  0x000055555566231d in rb_exc_exception (mesg=mesg@entry=140737288676920, tag=tag@entry=6, cause=cause@entry=36) at ../vm_core.h:1866
#3  0x0000555555668628 in rb_exc_raise (mesg=mesg@entry=140737288676920) at ../eval.c:684
#4  0x00005555559387a5 in rb_reg_raise_str (err=<optimized out>, options=0, str=140737288677040) at ../re.c:3300
#5  rb_reg_init_str (options=0, s=140737288677040, re=140737288677000) at ../re.c:3300
#6  rb_reg_new_str (options=0, s=140737288677040) at ../re.c:3291
#7  rb_reg_regcomp (str=140737288677040) at ../re.c:3373
#8  0x000055555565aca1 in main () at ../eval.c:856

如何从c代码中正确地调用ruby正则表达式函数,从而不会发生崩溃?提前感谢!
编辑:我从源代码编译了ruby库。我使用的是commit a8 e7 fee 80129 b 0 ba 360 c2671582117 c8 e18 a6464。
Edit 2:我知道“*”不是有效的正则表达式,但是程序的最初目的是让用户输入他们自己的正则表达式,然后让ruby代码编译正则表达式。这段代码将用于模糊器,模糊ruby正则表达式解析器以查找其中的错误,因此程序应该能够优雅地处理无效的正则表达式字符串,而不是崩溃。
Edit 3:从rb_str_new_cstr调用中删除换行符。仍然崩溃。

egdjgwm8

egdjgwm81#

是的,在阅读文档后,我修复了代码。这段代码(我认为)工作得很好:

#include <ruby.h>
#include "ruby/re.h"

#define MAX_INPUT_SIZE 120



VALUE handle_error(VALUE obj1) {

    return 0;
}

VALUE dangerous_func(VALUE x)
{
    /* code that could raise an exception */
    int thing;

    thing = rb_reg_regcomp(x);
    printf("Regex return value: %d\n", thing);
    return thing;
}

int main(int argc, char** argv) {
    VALUE x;

    VALUE result;

    int state = 0;
    char string[MAX_INPUT_SIZE];
    ruby_setup();
    ruby_init();

    ruby_init_loadpath();



    state = 0;

    memset(string, 0, MAX_INPUT_SIZE);

    fgets(string, MAX_INPUT_SIZE, stdin);

    if (string[MAX_INPUT_SIZE-2]) {
        return 0;
    }

    x = rb_str_new_cstr(string);

    
    result = rb_protect(dangerous_func, x, &state);

    printf("result %d\n", state);

    return 0;

}

感谢@pmacfarlane提供的信息!

相关问题