Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > GCC gives SEGFAULT... but GDB runs

Reply
Thread Tools

GCC gives SEGFAULT... but GDB runs

 
 
sethukr@gmail.com
Guest
Posts: n/a
 
      03-02-2007
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

 
Reply With Quote
 
 
 
 
ais523
Guest
Posts: n/a
 
      03-02-2007
On Mar 2, 2:20 pm, "(E-Mail Removed)" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      03-02-2007
"(E-Mail Removed)" 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>


 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      03-02-2007
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> wrote:
>"(E-Mail Removed)" 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.

 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      03-02-2007
On 2 Mar, 14:20, "(E-Mail Removed)" <(E-Mail Removed)> 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>

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

> }


 
Reply With Quote
 
William Hughes
Guest
Posts: n/a
 
      03-02-2007
On Mar 2, 9:20 am, "(E-Mail Removed)" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Debugging with gdb: pystack macro for gdb to show python stack trace Timothy Madden Python 1 09-17-2009 11:47 AM
GDB - no debugging symbols found (GDB Vs GCC?) seba C++ 1 03-22-2007 02:02 PM
Capturing gdb output in pyhton script using gdb -batch -x arguments Surendra Python 0 03-23-2006 02:47 PM
gdb and gcc with IDE? Davy C Programming 6 10-24-2005 11:29 PM
Python Program runs but itself but does not run under IDLE Ollie Python 0 10-02-2003 04:05 PM



Advertisments