Reputation: 43
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)
Anybody can help? My code can be downloaded at https://github.com/dramforever/drcalc/ branch readline
Upvotes: 1
Views: 319
Reputation: 43
(I am the asker!)
I don't know anything about readline. so a good function definetion will work.
I chose https://github.com/jterrace/craq/blob/master/gmp-4.3.1/demos/calc/calcread.c
Upvotes: 0
Reputation: 27672
You should compile with the -g flag, so gdb is able to display more debug information. If you do that, you will see that the program crashes in the function inp_readline, when trying to use sh_line, which is NULL.
The reason is that you give sh_line 0 (which will be interpreted as a NULL pointer) as its initial value, and then in inp_readline you check if sh_line is non-NULL, and in that case you free the old string and read a new one with readline. But if it is NULL, which it is at the start, nothing is done, so when you get to strlen(sh_line) it is still NULL, and strlen crashes.
EDIT:
In the original, it says
if (sh_line) free(sh_line);sh_line=0;
sh_line = readline(sh_Prompt);
but you added some braces so it instead says
if (sh_line) {free(sh_line);sh_line=0;
sh_line = readline(sh_Prompt);}
That's why readline never gets called.
Upvotes: 2