Reason of REALLOC implementation ?
John Tytgat
John.Tytgat at aaug.net
Sun Apr 15 21:56:48 BST 2007
In !OsLib/Tools/support there are two files realloc.c and realloc.h which
implement the REALLOC() function. The file !OsLib/Tools/support/doc/realloc
says:
--8<--
realloc.c
---------
Just a portable realloc with no bugs. It's called REALLOC, though.
--8<--
Does someone know which bugs (and in which component) this document
refers too ? Bugs in a cross compiled environment ? Or bugs in RISC OS SCL ?
The strange thing is that this REALLOC() implementation is not free from
bugs either:
--8<--
/*realloc.c - portable realloc with no bugs!*/
[...]
#include <stdlib.h>
#include <string.h>
#include "realloc.h"
void *REALLOC
(
void *ptr,
size_t size
)
{
if (ptr != NULL && size != 0)
{
void *tmp;
if ((tmp = malloc (size)) == NULL)
return NULL;
memcpy (tmp, ptr, size);
free (ptr);
return tmp;
}
else if (size != 0)
return malloc (size);
else
{
/*ptr != NULL*/
free (ptr);
return NULL;
}
}
--8<--
When you make an existing malloc block bigger, the old ptr gets used
to read the new size 'size' from and this can lead to undefined behaviour.
Also when the first malloc() call above fails, there is no free() of the
old pointer done...
This REALLOC() routines is used in m.h when tracing is not enabled. If there
are no good reasons for the use of REALLOC() I would change that (back ?)
to the standard realloc() call like it is done in the OSLibSupport code
as well (cfr x__realloc() routine).
John.
--
John Tytgat, in his comfy chair at home BASS
John.Tytgat at aaug.net ARM powered, RISC OS driven
More information about the oslib-team
mailing list