GCC gives SEGFAULT... but GDB runs

S

sethukr

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
 
A

ais523

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).
 
C

CBFalconer

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.
 
K

Kenny McCormack

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.


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.
 
M

mark_bluemel

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>
 
S

santosh

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.
 
W

William Hughes

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top