Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Inexplicable buffer crash

Programming


Tags
solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 02-04-2016   -   Original Discussion by Corona688
disedorgue's Unix or Linux Image
disedorgue disedorgue is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 12 December 2017, 7:18 PM EST
Location: France
Posts: 464
Thanks: 37
Thanked 179 Times in 162 Posts
Hi, could you test with:

Code:
typedef struct {
 FILE *fp;
 int type;
 char buf[4096];
 } wrap;

Regards.
Sponsored Links
    #9  
Old Unix and Linux 02-04-2016   -   Original Discussion by Corona688
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 December 2017, 10:16 AM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
Already did. It crashes.
Sponsored Links
    #10  
Old Unix and Linux 02-04-2016   -   Original Discussion by Corona688
achenle's Unix or Linux Image
achenle achenle is offline Forum Advisor  
Registered User
 
Join Date: Jun 2009
Last Activity: 5 December 2017, 1:02 PM EST
Posts: 1,008
Thanks: 3
Thanked 154 Times in 146 Posts
Quote:
Originally Posted by Corona688 View Post
OS is Linux, compile command is gcc. It crashes three different versions in three machines and two architectures.
Same compile command on each? Any optimizations? Can you add code to print out the actual value of the pointer right after allocation and right before freeing it?

Or you can valgrind it.

And what exact version(s) of GCC, Linux, etc. are you using? Along with the two architectures.
    #11  
Old Unix and Linux 02-04-2016   -   Original Discussion by Corona688
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 December 2017, 10:16 AM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
Quote:
Originally Posted by achenle View Post
Same compile command on each?
Yes.

Quote:
Any optimizations?
I tried adding -m32 to see if a different architecture would help. It didn't.

I also added -ggdb to see if I could trace what was happening, but it remains a mystery.

Quote:
Can you add code to print out the actual value of the pointer right after allocation and right before freeing it?
Omitted for brevity, I had printed the values of every pointer almost continuously throughout the program; they don't change.

Its like fgets() has decided to just leap into incorrect memory and mangle it. Linux
Sponsored Links
    #12  
Old Unix and Linux 02-04-2016   -   Original Discussion by Corona688
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 December 2017, 10:16 AM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
I've tested and found that memset(d->buf, 100,0); instead of fgets does not crash. It's not the act of setting those 100 bytes that causes the crash, it may be related to the file pointer somehow.
Sponsored Links
    #13  
Old Unix and Linux 02-04-2016   -   Original Discussion by Corona688
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 December 2017, 10:16 AM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
OK, that was weird.


Code:
        wrap *wrap=malloc(sizeof(wrap));

This, folks, is why you don't ever name your variable the same name as a type.

size of structure: 4K and a bit.
size of pointer: 8 bytes.
sizeof(wrap): ambiguous.

Solved.
The Following 3 Users Say Thank You to Corona688 For This Useful Post:
durden_tyler (02-05-2016), jim mcnamara (02-04-2016), migurus (02-04-2016)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unixware Crash ... danilosevilla SCO 2 05-06-2011 10:06 AM
HP-UX Disk crash? hoff Filesystems, Disks and Memory 7 09-07-2007 09:09 AM
crash dump csreenivas Solaris 1 06-20-2007 05:07 PM
crash of my machine toufik Solaris 0 06-18-2006 06:30 AM
HP-UX system crash help please!!! efrenba HP-UX 1 05-19-2005 06:11 PM



All times are GMT -4. The time now is 02:55 PM.