flex和bison and readline:奇怪的段错误

时间:2013-01-29 06:33:39

标签: bison readline flex-lexer

GNU gdb (GDB) 7.5-ubuntu
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /path/to/drcalc...(no debugging symbols found)...done.
(gdb) r
Starting program: /path/to/cdrcalc 

Program received signal SIGSEGV, Segmentation fault.
0xb7e606b6 in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) 

有人可以帮忙吗?我的代码可以在https://github.com/dramforever/drcalc/分支readline

下载

2 个答案:

答案 0 :(得分:2)

您应该使用 -g 标志进行编译,因此gdb能够显示更多调试信息。如果您这样做,当尝试使用 sh_line 时,您会看到程序在 inp_readline 函数中崩溃,这是NULL。

原因是您将 sh_line 0(将被解释为NULL指针)作为其初始值,然后在 inp_readline 中检查是否 sh_line 非NULL,在这种情况下,您释放旧字符串并使用 readline 读取新字符串。但如果它是NULL,它在开始时没有做任何事情,所以当你到达 strlen(sh_line)时它仍然是NULL,并且 strlen 崩溃。 / p>

修改

在原文中,它说

if (sh_line) free(sh_line);sh_line=0; 
sh_line = readline(sh_Prompt);

但你添加了一些大括号,所以它改为

if (sh_line) {free(sh_line);sh_line=0;
sh_line = readline(sh_Prompt);}

这就是 readline 永远不会被调用的原因。

答案 1 :(得分:0)

(我是提问者!)
我对readline一无所知。所以一个好的功能定义将起作用 我选择了https://github.com/jterrace/craq/blob/master/gmp-4.3.1/demos/calc/calcread.c