GCC gives SEGFAULT... but GDB runs

Discussion in 'C Programming' started by sethukr@gmail.com, Mar 2, 2007.

  1. Guest

    Hi everybody,
    While i'm compiling the following program in GCC, it gives
    "segmentation fault"

    But GDB doesn't give any "segmentation fault"

    What's the reason for this??

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    main()
    {

    char name1[4];
    int age1;
    struct emp
    {
    char name[4];
    int age;
    };

    struct emp *e1=malloc(sizeof(struct emp));

    printf("enter name and age\n");
    scanf("%s%d",name1,&age1);
    printf("name1=%sage=%d\n",name1,age1);
    e1->age=age1;
    printf("\n%d",e1->age);
    }

    Thanks in advance,
    Sethu
    , Mar 2, 2007
    #1
    1. Advertising

  2. ais523 Guest

    On Mar 2, 2:20 pm, "" <> wrote:
    > Hi everybody,
    > While i'm compiling the following program in GCC, it gives
    > "segmentation fault"
    >
    > But GDB doesn't give any "segmentation fault"
    >
    > What's the reason for this??

    (snip)
    > char name1[4];

    (snip)
    > scanf("%s%d",name1,&age1);

    (snip)
    > Thanks in advance,
    > Sethu


    You're using %s, with no maximum length limit, on scanf, so if the
    user enters any more than 3 characters (most names are longer than
    that) then your program will start behaving in an undefined manner;
    segfaulting and appearing to execute normally are two possibilities,
    and it seems you've encountered both. (If you search the comp.lang.c
    archives, you'll see some more surprising possibilities as to what has
    happened in similar circumstances; according to the C standards,
    anything could happen). When scanf'ing in strings, you absolutely must
    put a length limit (as in, "%3s") on the read, or you have no method
    of preventing undefined behaviour. (You should probably make the
    string somewhat larger than 3 chars + NUL, though).
    --
    ais523
    ais523, Mar 2, 2007
    #2
    1. Advertising

  3. CBFalconer Guest

    "" wrote:
    >
    > While i'm compiling the following program in GCC, it gives
    > "segmentation fault"


    If the compiler stops with a segmentation fault, it hasn't
    generated an output file, and you have discovered a compiler bug.
    I suspect your description is inadequate.

    >
    > But GDB doesn't give any "segmentation fault"


    GDB isn't a compiler. I would expect it to refuse to run the
    program.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Mar 2, 2007
    #3
  4. In article <>,
    CBFalconer <> wrote:
    >"" wrote:
    >>
    >> While i'm compiling the following program in GCC, it gives
    >> "segmentation fault"

    >
    >If the compiler stops with a segmentation fault, it hasn't
    >generated an output file, and you have discovered a compiler bug.
    >I suspect your description is inadequate.
    >
    >>
    >> But GDB doesn't give any "segmentation fault"

    >
    >GDB isn't a compiler. I would expect it to refuse to run the
    >program.


    In fact, as you point out earlier, "the program" would not exist, since
    GCC segfaulted (per the OP's description) and, presumably, did not
    create any output file.
    Kenny McCormack, Mar 2, 2007
    #4
  5. Guest

    On 2 Mar, 14:20, "" <> wrote:
    > Hi everybody,
    > While i'm compiling the following program in GCC, it gives
    > "segmentation fault"


    I presume you mean that you can compile (and link) the program with
    (the) GCC (tool chain) but it fails with "segmentation fault" when you
    run it. You should express yourself more clearly. It would also be
    good to tell us what data you entered...

    > But GDB doesn't give any "segmentation fault"


    By this I presume you mean that if you then try to run the program
    with the GDB debugger, it runs successfully.

    > What's the reason for this??


    I think ais523 has identified where the segmentation fault probably
    comes from.

    The reason that it runs without failure under GDB's control isn't
    really a C question but
    <Offtopic>
    I expect that the way that data is laid out in memory when GDB runs
    the program is different in such a way that the buffer overrun doesn't
    immediately cause failure.
    </Offtopic>
    , Mar 2, 2007
    #5
  6. santosh Guest

    wrote:
    > Hi everybody,
    > While i'm compiling the following program in GCC, it gives
    > "segmentation fault"
    >
    > But GDB doesn't give any "segmentation fault"
    >
    > What's the reason for this??
    >
    > #include<stdio.h>
    > #include<string.h>


    You're not using any function from string.h

    > #include<stdlib.h>
    > main()


    Write this as int main(void)

    > {
    >
    > char name1[4];


    Isn't this rather small for holding a name? If an array write
    overflows, anything can happen.

    > int age1;
    > struct emp
    > {
    > char name[4];


    Similarly, if you must use a static array, use a more sane size like
    32 or thereabouts.

    > int age;
    > };
    >
    > struct emp *e1=malloc(sizeof(struct emp));
    >
    > printf("enter name and age\n");
    > scanf("%s%d",name1,&age1);


    The %s specifier tells scanf to scan an arbitrarily long sequence of
    characters into the corresponding array. If more characters than the
    array can hold are scanned, scanf will keep writing past the end of
    the array, thus corrupting memory and invoking undefined behaviour.

    If you must at all use scanf for reading strings, use a length
    specifier to tell it to stop at a certain point. For example you
    could've said:

    scanf("%3s", name1);

    A better way to input a line is to use fgets. scanf is not needed if
    no parsing and conversion of input is required. In your example, you
    could've done:

    fgets(name1, sizeof(name1), stdin);

    Do man 3 fgets or read your standard library's documentation for
    properly using fgets.

    > printf("name1=%sage=%d\n",name1,age1);


    Use a tab or a newline between name1 and age1.

    > e1->age=age1;
    > printf("\n%d",e1->age);


    And terminate printf's output with a newline to force a write to the
    screen. Otherwise output may appear delayed.

    > }
    santosh, Mar 2, 2007
    #6
  7. On Mar 2, 9:20 am, "" <> wrote:
    > Hi everybody,
    > While i'm compiling the following program in GCC, it gives
    > "segmentation fault"
    >
    > But GDB doesn't give any "segmentation fault"
    >
    > What's the reason for this??
    >
    > #include<stdio.h>
    > #include<string.h>
    > #include<stdlib.h>
    > main()
    > {
    >
    > char name1[4];
    > int age1;
    > struct emp
    > {
    > char name[4];
    > int age;
    > };
    >
    > struct emp *e1=malloc(sizeof(struct emp));
    >
    > printf("enter name and age\n");
    > scanf("%s%d",name1,&age1);
    > printf("name1=%sage=%d\n",name1,age1);
    > e1->age=age1;
    > printf("\n%d",e1->age);
    >
    > }
    >
    > Thanks in advance,
    > Sethu




    As pointed out the reason for the undefined behaviour
    (sefault when run outside of GDB, works correctly
    inside of GDB (undefined behaviour means anything
    can happen, including what you expect))
    is that you have allocated insufficient storage
    for name1. But you also need to make sure that
    whatever limit you put on the size of name1 you
    do not try to put more characters in. In the
    words of the Great Prophet Henry Spencer:

    Thou shalt check the array bounds of all strings (indeed, all
    arrays),
    for surely where thou typest``foo'' someone someday shall type
    ``supercalifragilisticexpialidocious''

    Why the difference between the behaviour inside and
    outside GDB? Who knows? Possibly:

    GDB put some extra space after data1,
    space it might need to use at some
    point (or maybe not). When you ran outside of GDB
    there was no extra space, so your program tried
    to write to memory it did not own, thus causing
    your operating system to segfault ( a GOOD THING,
    there are much worse things than a segfault.)

    This type of behaviour is frequently seen. Often when
    a program is compiled in debug mode everything is fine,
    but compile in optimized mode (in general less forgiving
    of buffer overruns) and the program segfaults.

    Usually the senario is something like this: "Well after
    one year, the 10 member team has finished the product.
    We'll just switch to optimized mode for the shipping version
    ..... ARGGHHHH!!!!"

    - William Hughes
    William Hughes, Mar 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surendra
    Replies:
    0
    Views:
    508
    Surendra
    Mar 23, 2006
  2. seba
    Replies:
    1
    Views:
    1,443
    mlimber
    Mar 22, 2007
  3. Timothy Madden
    Replies:
    1
    Views:
    1,617
    Timothy Madden
    Sep 17, 2009
  4. Don
    Replies:
    60
    Views:
    1,348
    Phred Phungus
    Mar 19, 2010
  5. Andrey Vul
    Replies:
    8
    Views:
    683
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page