Re: realloc(): invalid next size
- From: mwojcik@xxxxxxxxxxx (Michael Wojcik)
- Date: 11 Apr 2006 15:34:07 GMT
In article <e1g4q3$d81e$1@xxxxxxxxxxxxxxxxx>, Deephay <tudoxxx@xxxxxxxxx> writes:
I have a program that used the realloc() function to change the
allocated size of a buffer, the program works with some arguments, but
with some other arguments, it will show me the error message like:
*** glibc detected *** realloc(): invalid next size: 0x0804c3a8 ***
glibc is off-topic for comp.lang.c, but this is glibc telling you
that you have invoked Undefined Behavior. Probably you have
corrupted glibc's housekeeping data by performing an illegal
operation on an object with dynamic storage duration, such as writing
past the end of an allocated area.
and then I inserted a perror("realloc") to see what happend, it says that:
What will happen is that you will get a meaningless message written
to stderr. realloc does not set errno, so perror will report whatever
happens to have already been in errno.
the realloc() is in a loop:
for (m = 0; m < len; m++) {
if (isspace(data[m]) || ispunct(data[m]) ||
isdigit(data[m]))
printf("%c", data[m]);
else {
p = min(strcspn(&data[m], " "),
strcspn(&data[m], "\t"),
strcspn(&data[m], "\r"),
strcspn(&data[m], "\n"));
key = realloc(key, p);
Wrong for two reasons. You failed to check whether realloc
succeeded, and if it failed, you just lost the old value of "key"
and so introduced a memory leak.
The result of realloc should always be stored in a temporary
variable and should be checked for null. If it is null, remember
to free the old value:
char *newkey;
newkey = realloc(key, p);
if (! newkey)
{
free(key);
[perform error handling]
}
key = newkey;
strncpy(key, &data[m], p);
key[p] = '\0';
You just wrote past the end of the allocated area. Since key is an
area of p bytes, valid indices are 0 through p-1, inclusive.
and the "key" is already malloced before the loop:
char *key = malloc(1);
Did you check to see whether malloc succeeded?
Any suggestion could be helpful, thx very much!
Read the comp.lang.c FAQ (http://www.c-faq.com is one source). Get a
copy of the C standard. C makes little effort to protect you from
yourself; you will only produce reliable, correct C code by learning
the language and its pitfalls.
--
Michael Wojcik michael.wojcik@xxxxxxxxxxxxxx
The lecturer was detailing a proof on the blackboard. He started to say,
"From the above it is obvious that ...". Then he stepped back and thought
deeply for a while. Then he left the room. We waited. Five minutes
later he returned smiling and said, "Yes, it is obvious", and continued
to outline the proof. -- John O'Gorman
.
- Follow-Ups:
- Re: realloc(): invalid next size
- From: tudoxxx
- Re: realloc(): invalid next size
- From: Pedro Graca
- Re: realloc(): invalid next size
- References:
- realloc(): invalid next size
- From: Deephay
- realloc(): invalid next size
- Prev by Date: Re: String array
- Next by Date: Re: realloc(): invalid next size
- Previous by thread: Re: realloc(): invalid next size
- Next by thread: Re: realloc(): invalid next size
- Index(es):
Relevant Pages
|