Thread: segmentation fault assistance

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    11

    segmentation fault assistance

    I got the following when I ran gdb on a program, SimpleDemo, which indicated a segmentation fault:




    (gdb) run
    Starting program: /home/bill/asm/SimpleDemo


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


    I was expecting to get info on where the program created the fault but it seems it was caused in one of the c libraries. Not sure where to go with this as I was following along with a debug tutorial and am now in (for me) uncharted waters.

    SimpleDemo.c:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int add(int x, int y)
    {
            int z =10;
    
    
            z = x + y;
            return z;
    }
    
    
    main(int argc, char **argv)
    {
            int a = atoi(argv[1]);
            int b = atoi(argv[2]);
            int c;
            char buffer[100];
    
    
            gets(buffer);
            puts(buffer);
    
    
            c = add(a,b);
    
    
            printf("Sum of %d+%d = %d\n",a, b, c);
    
    
            exit(0);
    
    
    }
    Thanks,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,519
    I noticed:
    • You did not check that argv[1] and argv[2] existed, e.g., by checking argc.
    • You used gets, which is vulnerable to buffer overflow. You should use say, fgets instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    11
    Hello laserlight,

    Thanks for your response. I'm not sure what you mean when you say "check argc".

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to run this program with two command-line arguments:
    > prog 10 25

    In the program, you should check main's argc ("argument count") parameter. argv[0] holds the program name, so the arguments start at argv[1]. If you're expecting 2 arguments, then you want an argc of 3.
    Code:
    int main(int argc, char **argv)
    {
        if (argc != 3) {
            printf("Usage: prog n m\n");
            exit(1);
        }
        // ...
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by mack99 View Post
    I was expecting to get info on where the program created the fault but it seems it was caused in one of the c libraries. Not sure where to go with this as I was following along with a debug tutorial and am now in (for me) uncharted waters.
    Use the bt (backtrace) command in gdb:
    Code:
    $ gdb -q ./test
    Reading symbols from test...done.
    (gdb) r
    Starting program: test 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x00167590 in __GI_____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, 
        group=0, loc=0x2d48c0) at strtol_l.c:298
    298    strtol_l.c: No such file or directory.
    (gdb) bt
    #0  0x00167590 in __GI_____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, 
        group=0, loc=0x2d48c0) at strtol_l.c:298
    #1  0x00167347 in __GI_strtol (nptr=0x0, endptr=0x0, base=10) at strtol.c:110
    #2  0x0016464f in atoi (nptr=0x0) at atoi.c:28
    #3  0x080484e4 in main (argc=1, argv=0xbffff304) at test.c:17
    (gdb) help bt
    Print backtrace of all stack frames, or innermost COUNT frames.
    With a negative argument, print outermost -COUNT frames.
    Use of the 'full' qualifier also prints the values of the local variables.
    So your program crashes on line 17.
    You're are only interested in the innermost frame of your own code, which in this case is frame #3.

    See also Debugging with GDB - Examining the Stack

    Bye, Andreas
    Last edited by AndiPersti; 08-08-2012 at 06:23 AM. Reason: highlight frame

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,669
    Also, to set the arguments in gdb, after starting gdb at the prompt type

    set args arg1 arg2

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,969
    > #2 0x0016464f in atoi (nptr=0x0) at atoi.c:28
    You can also tell from the call to atoi() that you passed it a NULL pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    11
    Thanks everyone for the advice and help with this. Greatly appreciated !

  9. #9
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    11
    Thanks again. I understand now why the program didn't work. To learn more I found an exercise that gives an example (show_args.c).

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main(int argc, char **argv)
    {
    	while(argc--)
    		printf("%s\n", *argv++);
    	
    	exit(EXIT_SUCCESS);
    
    
    }
    When I ran:

    show_args abcde text hello

    I got:

    show_args:command not found.

    I also tried ./show_args abcde text hello and got the same response.

    When I tried: echo $PATH I got:

    /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games



    Not sure what is going on ?

    Thanks.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,969
    Did you compile it?

    What executable name did you specify when you compiled it?

    gcc -o myprogname source.c

    Then you would type in
    ./myprogname

    The default is usually a.out
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    11
    Thanks Salem. I must have made a mistake in compiling. This works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By gums in forum C Programming
    Replies: 11
    Last Post: 05-03-2007, 08:06 AM
  2. Segmentation Fault
    By Ajay Bahadur in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2007, 12:27 PM
  3. Segmentation Fault :(
    By DarkDot in forum C++ Programming
    Replies: 39
    Last Post: 04-07-2007, 05:16 AM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM
Website Security Test