It turns out that realloc(ptr, 0) --> free(ptr) is not actually required by the C standard (whereas realloc(NULL, size) --> malloc(size) is).

Therefore, explicitly model the behaviour expected by our libraries (that realloc of 0 size is equivalent to free).

svn path=/trunk/netsurf/; revision=10524
This commit is contained in:
John Mark Bell 2010-04-30 07:00:58 +00:00
parent b579b0deb3
commit 000e6ad3de
14 changed files with 76 additions and 30 deletions

View File

@ -3754,5 +3754,10 @@ uint32 ami_popup_hook(struct Hook *hook,Object *item,APTR reserved)
static void *myrealloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}

View File

@ -1170,6 +1170,11 @@ bool cookies_update(const char *domain, const struct cookie_data *data)
static void *myrealloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}

View File

@ -60,6 +60,11 @@ static nserror nscss_import(hlcache_handle *handle,
*/
static void *myrealloc(void *ptr, size_t size, void *pw)
{
if (size == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, size);
}

View File

@ -51,6 +51,11 @@ bool verbose_log = false;
static void *netsurf_lwc_alloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}

View File

@ -323,6 +323,11 @@ fb_browser_window_redraw(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw)
static void *myrealloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}

View File

@ -537,6 +537,11 @@ void nsgtk_check_homedir(void)
*/
void *nsgtk_hubbub_realloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}

View File

@ -50,6 +50,24 @@ struct box_duplicate_llist {
};
static struct box_duplicate_llist *box_duplicate_last = NULL;
/**
* Allocator
*
* \param ptr Pointer to reallocate, or NULL for new allocation
* \param size Number of bytes requires
* \param pw Allocation context
* \return Pointer to allocated block, or NULL on failure
*/
void *box_style_alloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}
/**
* Destructor for box nodes which own styles
*

View File

@ -294,7 +294,7 @@ extern const char *TARGET_BLANK;
#define UNKNOWN_WIDTH INT_MAX
#define UNKNOWN_MAX_WIDTH INT_MAX
void *box_style_alloc(void *ptr, size_t len, void *pw);
struct box * box_create(css_computed_style *style, bool style_owned,
char *href, const char *target, char *title,
char *id, void *context);

View File

@ -813,12 +813,6 @@ bool box_construct_text(xmlNode *n, struct content *content,
return true;
}
static void *ns_css_computed_style_alloc(void *ptr, size_t len, void *pw)
{
return realloc(ptr, len);
}
/**
* Get the style for an element.
*
@ -842,7 +836,7 @@ css_computed_style *box_get_style(struct content *c,
(uint8_t *) s, strlen(s),
c->data.html.encoding, content__get_url(c),
c->data.html.quirks != BINDING_QUIRKS_MODE_NONE,
ns_css_computed_style_alloc, c);
box_style_alloc, NULL);
xmlFree(s);
@ -853,7 +847,7 @@ css_computed_style *box_get_style(struct content *c,
/* Select partial style for element */
partial = nscss_get_style(c, n, CSS_PSEUDO_ELEMENT_NONE,
CSS_MEDIA_SCREEN, inline_style,
ns_css_computed_style_alloc, c);
box_style_alloc, NULL);
/* No longer need inline style */
if (inline_style != NULL)

View File

@ -75,19 +75,6 @@ static bool calculate_table_row(struct columns *col_info,
unsigned int *start_column);
static bool box_normalise_inline_container(struct box *cont, struct content *c);
/**
* Allocator
*
* \param ptr Pointer to reallocate, or NULL for new allocation
* \param size Number of bytes requires
* \param pw Allocation context
* \return Pointer to allocated block, or NULL on failure
*/
static void *myrealloc(void *ptr, size_t len, void *pw)
{
return talloc_realloc_size(pw, ptr, len);
}
/**
* Ensure the box tree is correctly nested by adding and removing nodes.
*
@ -162,7 +149,7 @@ bool box_normalise_block(struct box *block, struct content *c)
assert(block->style != NULL);
style = nscss_get_blank_style(c, block->style,
myrealloc, c);
box_style_alloc, NULL);
if (style == NULL)
return false;
@ -256,7 +243,7 @@ bool box_normalise_table(struct box *table, struct content * c)
assert(table->style != NULL);
style = nscss_get_blank_style(c, table->style,
myrealloc, c);
box_style_alloc, NULL);
if (style == NULL) {
free(col_info.spans);
return false;
@ -335,7 +322,8 @@ bool box_normalise_table(struct box *table, struct content * c)
assert(table->style != NULL);
style = nscss_get_blank_style(c, table->style, myrealloc, c);
style = nscss_get_blank_style(c, table->style,
box_style_alloc, NULL);
if (style == NULL) {
free(col_info.spans);
return false;
@ -351,7 +339,7 @@ bool box_normalise_table(struct box *table, struct content * c)
row_group->type = BOX_TABLE_ROW_GROUP;
style = nscss_get_blank_style(c, row_group->style,
myrealloc, c);
box_style_alloc, NULL);
if (style == NULL) {
box_free(row_group);
free(col_info.spans);
@ -463,7 +451,7 @@ bool box_normalise_table_spans(struct box *table, struct span_info *spans,
style = nscss_get_blank_style(c,
table_row->style,
myrealloc, c);
box_style_alloc, NULL);
if (style == NULL)
return false;
@ -564,7 +552,7 @@ bool box_normalise_table_row_group(struct box *row_group,
assert(row_group->style != NULL);
style = nscss_get_blank_style(c, row_group->style,
myrealloc, c);
box_style_alloc, NULL);
if (style == NULL)
return false;
@ -632,7 +620,7 @@ bool box_normalise_table_row_group(struct box *row_group,
assert(row_group->style != NULL);
style = nscss_get_blank_style(c, row_group->style,
myrealloc, c);
box_style_alloc, NULL);
if (style == NULL) {
return false;
}
@ -691,7 +679,7 @@ bool box_normalise_table_row(struct box *row,
assert(row->style != NULL);
style = nscss_get_blank_style(c, row->style,
myrealloc, c);
box_style_alloc, NULL);
if (style == NULL)
return false;

View File

@ -106,6 +106,11 @@ static const char empty_document[] =
*/
static void *myrealloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}

View File

@ -128,6 +128,7 @@ static hubbub_tree_handler tree_handler = {
static void *myrealloc(void *ptr, size_t len, void *pw)
{
/* talloc_realloc_size(pw, ptr, 0) == talloc_free(ptr) */
return talloc_realloc_size(pw, ptr, len);
}

View File

@ -273,6 +273,11 @@ static void ro_gui_view_source_bounce(wimp_message *message);
static void *myrealloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}

View File

@ -2284,6 +2284,11 @@ void gui_cert_verify(const char *url, const struct ssl_cert_info *certs,
static void *myrealloc(void *ptr, size_t len, void *pw)
{
if (len == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, len);
}