Segmentation Fault from return

Segmentation Fault from return

Post by Grahame M. Kel » Tue, 19 Oct 1999 04:00:00





Quote:

> I'm getting a segmentation fault on an application I'm developing and I
> have no clue what is causing it.  I'm a code pig newbie, so any
> help/suggestions are most welcome.  Here's an excerpt of the problem:

<Snipped>

I am not a programmer per say, but seeing your calling the Do_get_double
recursively I would suggest that maybe rc should be a "static int".

Cheers, Grahame
----------------------------------------------
Anti-Spamming Enabled in FQDN.
Email: gmkelly (at) zip (dot) com (dot) au
Sydney Linux User Group - Member
http://www.slug.org.au
----------------------------------------------

 
 
 

Segmentation Fault from return

Post by Alain Coetmeu » Tue, 19 Oct 1999 04:00:00



Quote:

>Hi everybody,

>I'm getting a segmentation fault on an application I'm developing and I
>have no clue what is causing it.  I'm a code pig newbie, so any
>help/suggestions are most welcome.  Here's an excerpt of the problem:

>Using RedHat Linux with egcs-1.1.2-24 on a 2.2.10 kernel.

maybe am I paranoid, but
I have similar unlogical crash with egcs-1.1.2/gcc-2.91-66 & up, including
gcc-2.95.1, that do not happend with egcs-1.1.1/gcc-2.91-60...
it crash in unlogical place like in destructor, iostream,... with the strack frame
broken... with vtable that change values between each stack frame,
because all my code have been extensively tested on many platforms
and even with purify, I'm accusing  gcc-2.91-66 & gcc-2.95-1 but
I'm not sure of anything...

anyway, if you are lucky this is a simple problem with
pointers, array overload, malloc ... but I've seen nothing
in the code you've shown.

anyway if you don't use memory auditing tools like purify
memory may be corrupted before and the crash may be unrelated to
those lines of code...

 
 
 

Segmentation Fault from return

Post by fred smit » Tue, 19 Oct 1999 04:00:00


: Hi everybody,

: I'm getting a segmentation fault on an application I'm developing and I
: have no clue what is causing it.  I'm a code pig newbie, so any
: help/suggestions are most welcome.  Here's an excerpt of the problem:

Whenever a C program chokes on a return statement the most likely
reason is that the function you're returning from (or a function called
by it) has trashed the stack. The stack contains the address to return
to in the function that called this one, and if it gets damagedthe
return statement will return to the bogus address, somewhere "off in
the weeds", a place where there is unlikly to be valid executable code.

You don't sow us the source for Do_get_string(), but it seems likely
that that is where your problem is, or else it may be in a misuse of
the "struct client_t *c" whose we also don't see.

Fred

: Using RedHat Linux with egcs-1.1.2-24 on a 2.2.10 kernel.

: A section of my code calls a function named Do_get_double -

:>    int rc;
:>
:>    ...
:>
:>        /* wait for the player to send back an answer */
:>    printf("Starting...\n");
:>    rc = Do_get_double(c, x);
:>    printf("We're back in Do_coords_dialog with an rc of %d.\n", rc);
:>
:>    ...

: Do_get_double calls Do_get_string which is set up to fail with an error
: code of 3 (S_ERROR) -

:>int Do_get_double(struct client_t *c, double *theDouble)
:>{
:>    char tmpDouble[SZ_NUMBER];
:>    int rc;
:>
:>    rc = Do_get_string(c, tmpDouble, SZ_NUMBER);
:>    printf("Here I am with a return code of %d.\n", rc);
:>
:>    if (rc == S_ERROR) {
:>        printf("(3) Do_get_string failed with %d.\n", rc);
:>        return S_ERROR;
:>    }
:>    else if (rc == S_TIMEOUT) {
:>        printf("(2) Do_get_string failed with %d.\n", rc);
:>        return S_TIMEOUT;
:>    }
:>    else if (rc == S_CANCEL) {
:>        printf("(1) Do_get_string failed with %d.\n", rc);
:>        return S_CANCEL;
:>    }
:>
:>    *theDouble = floor(strtod(tmpDouble, NULL));
:>
:>    return S_NORM;
:>}

: In an include file, I have:

:>/* socket return values */
:>#define S_NORM                  0       /* socket returned data */
:>#define S_CANCEL                1       /* player canceled selection */
:>#define S_TIMEOUT               2       /* player ran out of time */
:>#define S_ERROR                 3       /* socket is closed */

: When I run the code, here's what I get -

: Starting...
: Here I am with a return code of 3.
: (3) Do_get_string failed with 3.
: Segmentation fault (core dumped)

: So, the program seems to be segmenting on the return statement.  I have
: removed the constants and the passed by reference double, but the
: problem persists.  In addition, the print statements have somtimes
: returned strange values for rc in Do_get_double.

: Judging from previous articles, using g++ may solve this problem, but I
: wanted make sure I wasn't doing something stupid of that there isn't an
: easier workaround.

: Could I get some sage advice from any of the wize and powerful Linux
: gurus?  Thanks in advance.

: Brian

--

               But God demonstrates his own love for us in this:
                         While we were still sinners,
                              Christ died for us.
------------------------------- Romans 5:8 (niv) ------------------------------

 
 
 

Segmentation Fault from return

Post by Brian Kell » Thu, 21 Oct 1999 04:00:00


Hi again everyone,

I want to thank everyone for their ideas and suggestions.  They all
sent me in directions I hadn't considered and eventually got me to the
problem solution.

Fred, you were exactly correct.  In one of the functions called by the
procedure that crashes when it returns, I called a strcpy which
corrupted every frame of the stack below the variable's scope.  After
some digging, I discovered that the stack of this thread was
overflowing and just had to modify the thread's parameters.

Thanks again for everyone's input!

Brian



> Whenever a C program chokes on a return statement the most likely
> reason is that the function you're returning from (or a function called
> by it) has trashed the stack. The stack contains the address to return
> to in the function that called this one, and if it gets damagedthe
> return statement will return to the bogus address, somewhere "off in
> the weeds", a place where there is unlikly to be valid executable code.

> You don't sow us the source for Do_get_string(), but it seems likely
> that that is where your problem is, or else it may be in a misuse of
> the "struct client_t *c" whose we also don't see.

> Fred

 
 
 

1. Segmentation Fault from return

< some code fragments clipped >

Could you post the shortest complete program you can which reproduces
the problem?

It's most likely some sort of memory trashing.  Overrunning tmpDouble
or something similar.

--
Kelly R. Burkhart

Just enjoy the Ozzy and keep your mouth shut
   - George Plimpton (on SNL)

2. DISPLAY variable auto-set

3. SndConfig returns Segmentation fault (core dumpted)

4. How do I configure a SLIP connection (VAX/750 4.3bsd)

5. Compiling *** VIM 5.3 *** Segmentation Fault..what is Seg-Fault..MEM Bounds?

6. ipxbridge

7. Page Faults/Segmentation Faults??

8. speech recognition using Linux ???

9. "Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

10. segmentation fault ?

11. Get "Segmentation fault (core dumped)" but no core file found

12. Segmentation Faults and Bus Errors

13. Dosemu0.52 : Segmentation fault?