LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2004, 03:25 PM   #1
leiyang
LQ Newbie
 
Registered: Aug 2004
Posts: 10

Rep: Reputation: 0
Segmentation fault for free()


Hi friends,

I am writing a c code and have been bugged by this segmentation fault for a while.

What I did is simply like:

-----------------------------------------------------------------------------------------------------
.........
unsigned long blocksize = 2048;
char *in_buffer
char *out_buffer;
if(( in_buffer= malloc(blocksize)) == NULL)
{
fprintf(stderr, "*** Can't malloc(%ld) forbuffer.\n",blocksize);
return NULL;
}

if(( out_buffer= malloc(2*blocksize)) == NULL)
{
fprintf(stderr, "*** Can't malloc(%ld) forbuffer.\n",blocksize);
free(in_buffer);
return NULL;
}

loop: until all the data are read from file
{
//read a block of data from a file to in_buffer
// do some data processing with in_buffer
//write the result to out_buffer
//memcpy out_buffer to list
}

free(in_buffer);
free(out_buffer);

return list;
......
-----------------------------------------------------------------------------------------------------------------

I've debugged with gdb to see where the segmentation fault happens, it is at free(in_buffer).
I've verified that the value for in_buffer after malloc() and before free() is the same. Or in other words, in_buffer is a valid pointer allocated by malloc.

And the SF only happens when the file is large, although block size could be small.
Means that for both small (2KB) and large(5MB) files, block size are the same. However, only large files could cause SF.

Could anyone please point me out what could possibly be the reason?

TIA!
Lei
 
Old 08-24-2004, 03:48 PM   #2
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Well, it could be that you have another thread trying to read the data after you've free it.

The only other thing I can think of is that you are overrunning the buffer or a buffer somewhere. If you do, sometimes the results are very weird.

I always do an "if (ptr) free(ptr)". I think its a good habbit to get into.

Doesn't always work though.

Btw, just because the pointer contains an address doesn't mean it valid. Try setting your pointers to NULL before you use them. Also, try taking the assignment out of the if statement:

ptr = malloc();
if (ptr == NULL)
{
}
etc...


Not:

if ((ptr = malloc())==NULL)
{
}
etc...


My teacher said this was a bad thing to do. Even if he is wrong, and there is nothing wrong with the 2nd statement, it still looks messy. A few extra lines wont hurt anyone!
 
Old 08-24-2004, 03:52 PM   #3
leiyang
LQ Newbie
 
Registered: Aug 2004
Posts: 10

Original Poster
Rep: Reputation: 0
"Btw, just because the pointer contains an address doesn't mean it valid. Try setting your pointers to NULL before you use them."

Well, this I agree, but as I've mentioned, the address is the same with what it is initially allocated, and this code isn't so wrong because it works with small files.

Thanks!
Lei
 
Old 08-24-2004, 04:36 PM   #4
apertran
LQ Newbie
 
Registered: Aug 2004
Distribution: Slackware
Posts: 7

Rep: Reputation: 0
just a stupid brute force guess - how about calloc? does it change the outcome?
would be interesting to see what operations in_buffer is exposed to inside that loop.

Last edited by apertran; 08-24-2004 at 04:42 PM.
 
Old 08-24-2004, 05:27 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you're overflowing a buffer. use valgrind.
 
Old 08-25-2004, 10:06 AM   #6
leiyang
LQ Newbie
 
Registered: Aug 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by apertran
just a stupid brute force guess - how about calloc? does it change the outcome?
would be interesting to see what operations in_buffer is exposed to inside that loop.
No, calloc won't help

I am trying with Valgrind.

Thanks!
Lei
 
Old 08-25-2004, 10:07 AM   #7
leiyang
LQ Newbie
 
Registered: Aug 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by infamous41md
you're overflowing a buffer. use valgrind.
What is the difference between stack buffer overflow and heap buffer overflow? Which case I am most likely to be in now?

Thanks a lot!
Lei
 
Old 08-25-2004, 10:09 AM   #8
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
malloc always allocates memory from heap. so your problem lies there. I guess you should post your code so that others can better understand the problem.
 
Old 08-25-2004, 10:18 AM   #9
leiyang
LQ Newbie
 
Registered: Aug 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by Kumar
malloc always allocates memory from heap. so your problem lies there. I guess you should post your code so that others can better understand the problem.
Well, I'd like to, but unfortunately that piece of code is not written by me and I don't have the right to post it I am only authorized to use it as a function in my project. Also the code is not a trivial one and is a bit long.

Thanks for your suggestions, anyway. I'll try to debug with Valgrind first and see what I can do with it.

Lei
 
Old 08-25-2004, 10:41 AM   #10
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
When you successfuly call malloc() and return the value for a pointer, most malloc implmentations store the length of the buffer allocated at the physical address ptr - 1; This group of bytes is called a descriptor - in Windows it's BSTR - (on 32 bit machines: 4 byte longword of buffer length, 4 byte longword for the pointer to the start of the buffer).

So, if you execute ptr++ -- or something writes a value into the memory at the address just before the physical address of ptr, you break free(), because it tries to mess with a buffer of the wrong size.
 
Old 08-25-2004, 10:53 AM   #11
leiyang
LQ Newbie
 
Registered: Aug 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by jim mcnamara
When you successfuly call malloc() and return the value for a pointer, most malloc implmentations store the length of the buffer allocated at the physical address ptr - 1; This group of bytes is called a descriptor - in Windows it's BSTR - (on 32 bit machines: 4 byte longword of buffer length, 4 byte longword for the pointer to the start of the buffer).

So, if you execute ptr++ -- or something writes a value into the memory at the address just before the physical address of ptr, you break free(), because it tries to mess with a buffer of the wrong size.
That's sound reasonable, but I did print out 4 bytes before the physical address of ptr, after malloc and before free, and they are the same.

Thanks,
Lei
 
Old 08-25-2004, 11:53 AM   #12
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
There are other data structures involved - I simplified it.
You can always download linux source and find out exactly what malloc and free are doing.
 
Old 08-25-2004, 03:11 PM   #13
leiyang
LQ Newbie
 
Registered: Aug 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks folks, I fixed the problem, finally.

It is because when I malloc memory for the buffer, the size is smaller than actually needed, my estimation of buffer length is not right So the guess of buffer overflow is correct! And this is not directly related with file size.

Valgrind is really cool....

Cheers,
Lei

Last edited by leiyang; 08-25-2004 at 03:29 PM.
 
Old 08-25-2004, 04:52 PM   #14
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Thumbs up

 
Old 08-25-2004, 04:52 PM   #15
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Thumbs up

 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault??? max_rsr Programming 2 04-15-2005 08:47 PM
in C, segmentation fault on free, and about double free Miaire Programming 3 03-16-2005 11:52 AM
Segmentation fault dand Linux - Newbie 2 12-16-2004 04:45 PM
segmentation fault ! wwnn1 Programming 7 06-18-2002 09:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration