Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault on mpca_lang instanciation #8

Closed
oleiade opened this issue Apr 22, 2014 · 4 comments
Closed

Segfault on mpca_lang instanciation #8

oleiade opened this issue Apr 22, 2014 · 4 comments

Comments

@oleiade
Copy link

oleiade commented Apr 22, 2014

Hi!

I've been working around the examples in the readme (which by the way are not really up to date), and buildyourownlisp. Updating mpc today lead me to get a segfault on my mpca_lang instanciation.

It seems to be a problem with a NULL pointer manipulation, but don't really have the time to dig in it right now, any clue?

example source

#include <stdio.h>
#include <stdlib.h>

#include "mpc.h"

int                     main(int argc, char** argv) {
        mpc_parser_t    *Expr = mpc_new("expr");
        mpc_parser_t    *Value = mpc_new("value");
        mpc_parser_t    *Maths = mpc_new("maths");
        mpc_result_t    result;
        char*           input = "123";

        mpca_lang(MPCA_LANG_DEFAULT,
        "                                       \
        expression  : <value> ;                 \
        value       : /[0-9]+/ | <expression> ; \
        maths       : /^/ <expression> /$/ ;    \
        ",
        Expr, Value, Maths);

        if (mpc_parse("input", input, Value, &result)) {
                mpc_ast_print(result.output);
                mpc_ast_delete(result.output);
        } else {
                mpc_err_print(result.error);
                mpc_err_delete(result.error);
        }

        mpc_cleanup(3, Expr, Value, Maths);
}

gdb session

(gdb) b main
Breakpoint 1 at 0x400dfc: file src/main.c, line 7.
(gdb) r
Starting program: /home/oleiade/Dev/Sandbox/C/config/./bin/debug/config 

Breakpoint 1, main (argc=1, argv=0x7fffffffe148) at src/main.c:7
7           mpc_parser_t    *Expr = mpc_new("expr");
(gdb) n
8           mpc_parser_t    *Value = mpc_new("value");
(gdb) 
9           mpc_parser_t    *Maths = mpc_new("maths");
(gdb) 
11          char*           input = "123";
(gdb) 
13          mpca_lang(MPCA_LANG_DEFAULT,
(gdb) 

Program received signal SIGSEGV, Segmentation fault.
0x0000000000409e46 in mpca_grammar_find_parser (x=0x618c10 "expression", st=0x7fffffffdf40) at src/mpc.c:2886
2886          if (p->name && strcmp(p->name, x) == 0) { return p; }
@orangeduck
Copy link
Owner

Hey,

You have a mismatched name. You declare a parser called expr, but then you define a parser called expression instead. It is crashing when looking for the parser called expr passed in by the variable arguments.

Because variable arguments in C suck, unfortunately I don't know if there is a way to detect this and give an error rather than crash but I'll check it out, and if not I'll make it clearer in the documentation.

Many Thanks,

Dan

@oleiade
Copy link
Author

oleiade commented Apr 23, 2014

Oh, you're perfectly right. My bad.

Now I realize it happened to me a lot while working on lipstick, the lisp resulting from buildyourownlisp :)

Anyway, I think letting the program segfault rather than dropping an error is a bad design decision, as SIGSEV tells the system "hey dude, your program is pretty fucked up, I won't let it run". If you open an issue about it, I'd be glad to participate and help :)

Thanks for your help

@orangeduck
Copy link
Owner

You're correct it isn't a very good way to deal with the error. Unfortunately because you can't find our how many variable arguments are passed in by the user, you can't tell if either they've got a naming wrong or there are just more variable arguments to come.

I did actually have a solution to this in mind. If you pass in NULL as the final variable argument I think you get a proper error not just a crash, but I need to double check and update the documentation to mention this

@orangeduck
Copy link
Owner

Okay. In the newest version you should now get a nice error message if you NULL terminate the list of parsers to mpca_lang. I've also updated the examples and the README to use this convention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants