segmentation fault assistance

This is a discussion on segmentation fault assistance within the C Programming forums, part of the General Programming Boards category; I got the following when I ran gdb on a program, SimpleDemo, which indicated a segmentation fault: (gdb) run Starting ...

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

    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
    21,409
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    8
    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 05:23 AM. Reason: highlight frame

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

    set args arg1 arg2

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > #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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  9. #9
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    8
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    8
    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, 07:06 AM
  2. Segmentation Fault
    By Ajay Bahadur in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2007, 11:27 AM
  3. Segmentation Fault :(
    By DarkDot in forum C++ Programming
    Replies: 39
    Last Post: 04-07-2007, 04:16 AM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21