From 000e6ad3dea9fe04759c7d27ea9ae500eccdc4bc Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Fri, 30 Apr 2010 07:00:58 +0000 Subject: 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 --- amiga/gui.c | 5 +++++ beos/beos_gui.cpp | 5 +++++ css/css.c | 5 +++++ desktop/netsurf.c | 5 +++++ framebuffer/gui.c | 5 +++++ gtk/gtk_gui.c | 5 +++++ render/box.c | 18 ++++++++++++++++++ render/box.h | 2 +- render/box_construct.c | 10 ++-------- render/box_normalise.c | 30 +++++++++--------------------- render/html.c | 5 +++++ render/hubbub_binding.c | 1 + riscos/gui.c | 5 +++++ windows/gui.c | 5 +++++ 14 files changed, 76 insertions(+), 30 deletions(-) diff --git a/amiga/gui.c b/amiga/gui.c index 3ba3d8957..249f80c89 100755 --- a/amiga/gui.c +++ b/amiga/gui.c @@ -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); } diff --git a/beos/beos_gui.cpp b/beos/beos_gui.cpp index cb41dfa07..471f4e9ea 100644 --- a/beos/beos_gui.cpp +++ b/beos/beos_gui.cpp @@ -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); } diff --git a/css/css.c b/css/css.c index f051d52b9..7fb17519b 100644 --- a/css/css.c +++ b/css/css.c @@ -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); } diff --git a/desktop/netsurf.c b/desktop/netsurf.c index a69844f52..36832bb8c 100644 --- a/desktop/netsurf.c +++ b/desktop/netsurf.c @@ -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); } diff --git a/framebuffer/gui.c b/framebuffer/gui.c index 287652d6e..10a2b329d 100644 --- a/framebuffer/gui.c +++ b/framebuffer/gui.c @@ -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); } diff --git a/gtk/gtk_gui.c b/gtk/gtk_gui.c index 3a749c67e..724c59c7d 100644 --- a/gtk/gtk_gui.c +++ b/gtk/gtk_gui.c @@ -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); } diff --git a/render/box.c b/render/box.c index 9bb7e59fe..e61dee8ff 100644 --- a/render/box.c +++ b/render/box.c @@ -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 * diff --git a/render/box.h b/render/box.h index d1a1994f1..b6e344af1 100644 --- a/render/box.h +++ b/render/box.h @@ -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); diff --git a/render/box_construct.c b/render/box_construct.c index f7d92079b..153bdd75b 100644 --- a/render/box_construct.c +++ b/render/box_construct.c @@ -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) diff --git a/render/box_normalise.c b/render/box_normalise.c index 8af97535a..e082a9a6f 100644 --- a/render/box_normalise.c +++ b/render/box_normalise.c @@ -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; diff --git a/render/html.c b/render/html.c index 8d64f9515..caada3c96 100644 --- a/render/html.c +++ b/render/html.c @@ -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); } diff --git a/render/hubbub_binding.c b/render/hubbub_binding.c index fc0a31944..bc509b4eb 100644 --- a/render/hubbub_binding.c +++ b/render/hubbub_binding.c @@ -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); } diff --git a/riscos/gui.c b/riscos/gui.c index 302587833..d3148529c 100644 --- a/riscos/gui.c +++ b/riscos/gui.c @@ -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); } diff --git a/windows/gui.c b/windows/gui.c index 047b2b9be..187430a3c 100644 --- a/windows/gui.c +++ b/windows/gui.c @@ -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); } -- cgit v1.2.3