From 83346830688525a287489cc791299cbc945d4fc4 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 2 Jan 2005 03:58:21 +0000 Subject: [PATCH] [project @ 2005-01-02 03:58:20 by jmb] xcalloc/xrealloc/xstrdup-purge - Lose remaining calls (and purge the relevant functions from utils.c) svn path=/import/netsurf/; revision=1419 --- content/content.c | 30 +++++++++++++++++++++++--- content/content.h | 2 +- content/fetchcache.c | 10 ++++++--- debug/fontd.c | 24 ++++++++++++++------- desktop/browser.c | 6 +++--- gtk/font_pango.c | 4 +++- render/font.h | 4 ++-- render/html.c | 8 +++++-- riscos/401login.c | 6 +++--- riscos/font.c | 27 +++++++++++++++++++---- riscos/plotters.c | 3 +-- riscos/uri.c | 47 ++++++++++++++++++++-------------------- utils/utils.c | 51 +++++++++++--------------------------------- utils/utils.h | 7 ++---- 14 files changed, 130 insertions(+), 99 deletions(-) diff --git a/content/content.c b/content/content.c index 052a35fe7..7dd85a668 100644 --- a/content/content.c +++ b/content/content.c @@ -392,7 +392,16 @@ bool content_set_type(struct content *c, content_type type, callback = c->user_list->next->next->callback; p1 = c->user_list->next->next->p1; p2 = c->user_list->next->next->p2; - content_add_user(clone, callback, p1, p2); + if (!content_add_user(clone, callback, p1, p2)) { + c->type = CONTENT_UNKNOWN; + c->status = CONTENT_STATUS_ERROR; + content_destroy(clone); + msg_data.error = messages_get("NoMemory"); + content_broadcast(c, CONTENT_MSG_ERROR, + msg_data); + warn_user("NoMemory", 0); + return false; + } content_remove_user(c, callback, p1, p2); content_broadcast(clone, CONTENT_MSG_NEWPTR, msg_data); fetchcache_go(clone, 0, callback, p1, p2, 0, 0, false); @@ -706,24 +715,39 @@ bool content_redraw(struct content *c, int x, int y, /** * Register a user for callbacks. * + * \param c The content to register + * \param callback The callback function + * \param p1, p2 Callback private data + * \return true on success, false otherwise and error broadcast to users + * * The callback will be called with p1 and p2 when content_broadcast() is * called with the content. */ -void content_add_user(struct content *c, +bool content_add_user(struct content *c, void (*callback)(content_msg msg, struct content *c, void *p1, void *p2, union content_msg_data data), void *p1, void *p2) { struct content_user *user; + union content_msg_data msg_data; + LOG(("content %s, user %p %p %p", c->url, callback, p1, p2)); - user = xcalloc(1, sizeof(*user)); + user = calloc(1, sizeof(*user)); + if (!user) { + c->status = CONTENT_STATUS_ERROR; + msg_data.error = messages_get("NoMemory"); + content_broadcast(c, CONTENT_MSG_ERROR, msg_data); + return false; + } user->callback = callback; user->p1 = p1; user->p2 = p2; user->stop = false; user->next = c->user_list->next; c->user_list->next = user; + + return true; } diff --git a/content/content.h b/content/content.h index cb5d411e6..da565c711 100644 --- a/content/content.h +++ b/content/content.h @@ -276,7 +276,7 @@ bool content_redraw(struct content *c, int x, int y, int width, int height, int clip_x0, int clip_y0, int clip_x1, int clip_y1, float scale, unsigned long background_colour); -void content_add_user(struct content *c, +bool content_add_user(struct content *c, void (*callback)(content_msg msg, struct content *c, void *p1, void *p2, union content_msg_data data), void *p1, void *p2); diff --git a/content/fetchcache.c b/content/fetchcache.c index bdc04eb29..81c608b9e 100644 --- a/content/fetchcache.c +++ b/content/fetchcache.c @@ -84,8 +84,10 @@ struct content * fetchcache(const char *url, if (!post_urlenc && !post_multipart) { if ((c = content_get(url1)) != NULL) { free(url1); - content_add_user(c, callback, p1, p2); - return c; + if (!content_add_user(c, callback, p1, p2)) + return NULL; + else + return c; } } @@ -93,7 +95,9 @@ struct content * fetchcache(const char *url, free(url1); if (!c) return NULL; - content_add_user(c, callback, p1, p2); + if (!content_add_user(c, callback, p1, p2)) { + return NULL; + } if (!post_urlenc && !post_multipart) c->fresh = true; diff --git a/debug/fontd.c b/debug/fontd.c index 2fc3ec93b..a8974e540 100644 --- a/debug/fontd.c +++ b/debug/fontd.c @@ -41,23 +41,27 @@ unsigned long nsfont_width(struct font_data *font, const char * text, return length * 10; } -void nsfont_position_in_string(struct font_data* font, const char* text, +bool nsfont_position_in_string(struct font_data* font, const char* text, size_t length, unsigned long x, int* char_offset, int* pixel_offset) { - assert(font != 0 && text != 0); + assert(font != 0 && text != 0); - *char_offset = x / 10; - *pixel_offset = x; + *char_offset = x / 10; + *pixel_offset = x; - return; + return true; } struct font_set *nsfont_new_set() { - struct font_set *set = xcalloc(1, sizeof(*set)); + struct font_set *set; unsigned int i; + set = calloc(1, sizeof(*set)); + if (!set) + return NULL; + for (i = 0; i < FONT_FAMILIES * 4; i++) set->font[i] = 0; @@ -101,7 +105,9 @@ struct font_data *nsfont_open(struct font_set *set, struct css_style *style) if (data->size == size) return data; - data = xcalloc(1, sizeof(*data)); + data = calloc(1, sizeof(*data)); + if (!data) + return NULL; data->size = size; data->space_width = nsfont_width(data, " ", sizeof(" ")-1); @@ -155,9 +161,11 @@ char *nsfont_split(struct font_data *data, const char * text, } -void nsfont_paint(struct font_data *data, const char *text, +bool nsfont_paint(struct font_data *data, const char *text, size_t length, int xpos, int ypos, void *trfm) { assert(data != NULL); assert(text != NULL); + + return true; } diff --git a/desktop/browser.c b/desktop/browser.c index 1f87fc7dc..dbab40852 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -1095,7 +1095,7 @@ void browser_window_textarea_click(struct browser_window *bw, * Consecutive BR may not be present. These constraints are satisfied * by using a 0-length INLINE for blank lines. */ - int char_offset, pixel_offset, new_scroll_y; + int char_offset = 0, pixel_offset = 0, new_scroll_y; struct box *inline_container, *text_box; inline_container = textarea->children; @@ -1464,8 +1464,8 @@ void browser_window_input_click(struct browser_window* bw, int box_x, int box_y, int x, int y) { - size_t char_offset; - int pixel_offset, dx = 0; + size_t char_offset = 0; + int pixel_offset = 0, dx = 0; struct box *text_box = input->children->children; int uchars; unsigned int offset; diff --git a/gtk/font_pango.c b/gtk/font_pango.c index 16408530f..bd0eda997 100644 --- a/gtk/font_pango.c +++ b/gtk/font_pango.c @@ -107,7 +107,7 @@ unsigned long nsfont_width(struct font_data *font, const char *text, } -void nsfont_position_in_string(struct font_data *font, const char *text, +bool nsfont_position_in_string(struct font_data *font, const char *text, size_t length, unsigned long x, int *char_offset, int *pixel_offset) { @@ -131,6 +131,8 @@ void nsfont_position_in_string(struct font_data *font, const char *text, *char_offset = index; *pixel_offset = PANGO_PIXELS(pos.x); + + return true; } diff --git a/render/font.h b/render/font.h index f42d96229..4a983098a 100644 --- a/render/font.h +++ b/render/font.h @@ -33,13 +33,13 @@ struct font_data *nsfont_open(struct font_set *set, struct css_style *style); void nsfont_free_set(struct font_set *set); unsigned long nsfont_width(struct font_data *font, const char *text, size_t length); -void nsfont_position_in_string(struct font_data *font, const char *text, +bool nsfont_position_in_string(struct font_data *font, const char *text, size_t length, unsigned long x, int *char_offset, int *pixel_offset); char *nsfont_split(struct font_data *font, const char *text, size_t length, unsigned int width, unsigned int *used_width); -void nsfont_paint(struct font_data *font, const char *str, +bool nsfont_paint(struct font_data *font, const char *str, size_t length, int xpos, int ypos, void *trfm); void nsfont_txtenum(struct font_data *font, const char *text, size_t length, diff --git a/render/html.c b/render/html.c index 33b2a96c2..6d934e22e 100644 --- a/render/html.c +++ b/render/html.c @@ -592,9 +592,13 @@ bool html_find_stylesheets(struct content *c, xmlNode *head) if (c->data.html.stylesheet_content[STYLESHEET_STYLE] != 0) { if (css_convert(c->data.html.stylesheet_content[STYLESHEET_STYLE], c->width, c->height)) { - content_add_user(c->data.html.stylesheet_content[STYLESHEET_STYLE], + if (!content_add_user(c->data.html.stylesheet_content[STYLESHEET_STYLE], html_convert_css_callback, - c, (void *) STYLESHEET_STYLE); + c, (void *) STYLESHEET_STYLE)) { + /* no memory */ + c->data.html.stylesheet_content[STYLESHEET_STYLE] = 0; + return false; + } } else { /* conversion failed */ c->data.html.stylesheet_content[STYLESHEET_STYLE] = 0; diff --git a/riscos/401login.c b/riscos/401login.c index 145ad63f8..4f68d463d 100644 --- a/riscos/401login.c +++ b/riscos/401login.c @@ -57,7 +57,7 @@ void gui_401login_open(struct browser_window *bw, struct content *c, char *realm ro_gui_401login_open(bw->window->window, host, realm, murl); - xfree(host); + free(host); } @@ -134,8 +134,8 @@ void get_unamepwd(void) { char *lidets = calloc(strlen(uname)+strlen(pwd)+2, sizeof(char)); if (!lidets) { - LOG(("Insufficient memory for calloc")); - warn_user("NoMemory", 0); + LOG(("Insufficient memory for calloc")); + warn_user("NoMemory", 0); return; } diff --git a/riscos/font.c b/riscos/font.c index 924559e71..0aaff3b87 100644 --- a/riscos/font.c +++ b/riscos/font.c @@ -394,6 +394,9 @@ unsigned long nsfont_width(struct font_data *font, const char *text, break; case FONTTYPE_STANDARD_LATIN1: { const char *loc_text = cnv_strn_local_enc(text, length, NULL); + if (!loc_text) + return 0; + error = xfont_scan_string((font_f)font->handle, loc_text, font_GIVEN_FONT @@ -430,8 +433,9 @@ unsigned long nsfont_width(struct font_data *font, const char *text, * \param x horizontal position in pixels * \param char_offset updated to give the offset in the string * \param pixel_offset updated to give the coordinate of the character in pixels + * \return true on success, false on failure. */ -void nsfont_position_in_string(struct font_data *font, const char *text, +bool nsfont_position_in_string(struct font_data *font, const char *text, size_t length, unsigned long x, int *char_offset, int *pixel_offset) { @@ -475,6 +479,9 @@ void nsfont_position_in_string(struct font_data *font, const char *text, case FONTTYPE_STANDARD_LATIN1: { const ptrdiff_t *back_mapP; const char *loc_text = cnv_strn_local_enc(text, length, &back_mapP); + if (!loc_text) + return false; + error = xfont_scan_string((font_f)font->handle, loc_text, font_GIVEN_BLOCK @@ -495,11 +502,14 @@ void nsfont_position_in_string(struct font_data *font, const char *text, } if (error != NULL) { LOG(("(u)font_scan_string failed : %s\n", error->errmess)); - die("nsfont_position_in_string: (u)font_scan_string failed"); +/* die("nsfont_position_in_string: (u)font_scan_string failed");*/ + return false; } *char_offset = (int)(split - text); *pixel_offset = x_out / 800; + + return true; } @@ -559,6 +569,9 @@ char *nsfont_split(struct font_data *font, const char *text, case FONTTYPE_STANDARD_LATIN1: { const ptrdiff_t *back_mapP; const char *loc_text = cnv_strn_local_enc(text, length, &back_mapP); + if (!loc_text) + return NULL; + error = xfont_scan_string((font_f)font->handle, loc_text, font_GIVEN_BLOCK @@ -591,7 +604,7 @@ char *nsfont_split(struct font_data *font, const char *text, } -void nsfont_paint(struct font_data *data, const char *text, +bool nsfont_paint(struct font_data *data, const char *text, size_t length, int xpos, int ypos, void *trfm) { os_error *error; @@ -648,6 +661,9 @@ void nsfont_paint(struct font_data *data, const char *text, break; case FONTTYPE_STANDARD_LATIN1: { const char *loc_text = cnv_strn_local_enc(text, length, NULL); + if (!loc_text) + return false; + error = xfont_paint((font_f)data->handle, loc_text, flags, xpos, ypos, NULL, trfm, 0); @@ -660,8 +676,11 @@ void nsfont_paint(struct font_data *data, const char *text, } if (error != NULL) { LOG(("(u)font_paint failed : %s\n", error->errmess)); - die("nsfont_paint: (u)font_paint failed"); + /*die("nsfont_paint: (u)font_paint failed");*/ + return false; } + + return true; } diff --git a/riscos/plotters.c b/riscos/plotters.c index 12c4b7816..7fea7affc 100644 --- a/riscos/plotters.c +++ b/riscos/plotters.c @@ -312,11 +312,10 @@ bool ro_plot_text(int x, int y, struct font_data *font, error->errnum, error->errmess)); return false; } - nsfont_paint(font, text, length, + return nsfont_paint(font, text, length, ro_plot_origin_x + x * 2, ro_plot_origin_y - y * 2, &ro_plot_trfm); - return true; } diff --git a/riscos/uri.c b/riscos/uri.c index 04edc208f..4c0790398 100644 --- a/riscos/uri.c +++ b/riscos/uri.c @@ -32,43 +32,42 @@ extern wimp_t task_handle; void ro_uri_message_received(uri_full_message_process* uri_message) { - uri_h uri_handle; - char* uri_requested; - int uri_length; + uri_h uri_handle; + char* uri_requested; + int uri_length; - uri_handle = uri_message->handle; + uri_handle = uri_message->handle; - if (!fetch_can_fetch(uri_message->uri)) return; + if (!fetch_can_fetch(uri_message->uri)) return; - uri_message->your_ref = uri_message->my_ref; - uri_message->action = message_URI_PROCESS_ACK; + uri_message->your_ref = uri_message->my_ref; + uri_message->action = message_URI_PROCESS_ACK; - xwimp_send_message(wimp_USER_MESSAGE, - (wimp_message*)uri_message, - uri_message->sender); + xwimp_send_message(wimp_USER_MESSAGE, (wimp_message*)uri_message, + uri_message->sender); - xuri_request_uri(0, 0, 0, uri_handle, &uri_length); - uri_requested = calloc((unsigned int)uri_length, sizeof(char)); + xuri_request_uri(0, 0, 0, uri_handle, &uri_length); + uri_requested = calloc((unsigned int)uri_length, sizeof(char)); - if (uri_requested == NULL) - return; + if (uri_requested == NULL) + return; - xuri_request_uri(0, uri_requested, uri_length, uri_handle, NULL); + xuri_request_uri(0, uri_requested, uri_length, uri_handle, NULL); - browser_window_create(uri_requested, NULL, 0); + browser_window_create(uri_requested, NULL, 0); - xfree(uri_requested); + free(uri_requested); } -bool ro_uri_launch(char *uri) { - +bool ro_uri_launch(char *uri) +{ uri_h uri_handle; wimp_t handle_task; uri_dispatch_flags returned; os_error *e; e = xuri_dispatch(uri_DISPATCH_INFORM_CALLER, uri, task_handle, - &returned, &handle_task, &uri_handle); + &returned, &handle_task, &uri_handle); if (e || returned & 1) { return false; @@ -77,8 +76,8 @@ bool ro_uri_launch(char *uri) { return true; } -void ro_uri_bounce(uri_full_message_return_result *message) { - +void ro_uri_bounce(uri_full_message_return_result *message) +{ char uri_buf[512]; os_error *e; @@ -87,8 +86,8 @@ void ro_uri_bounce(uri_full_message_return_result *message) { e = xuri_request_uri(0, uri_buf, sizeof uri_buf, message->handle, 0); if (e) { - LOG(("xuri_request_uri: %d: %s", e->errnum, e->errmess)); - return; + LOG(("xuri_request_uri: %d: %s", e->errnum, e->errmess)); + return; } ro_url_load(uri_buf); diff --git a/utils/utils.c b/utils/utils.c index 5f5647bf9..35e3b1c7e 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -46,40 +46,6 @@ int whitespace(const char * str) return 1; } -void * xcalloc(const size_t n, const size_t size) -{ - void * p = calloc(n, size); - if (p == 0) die("Out of memory in xcalloc()"); - return p; -} - -void * xrealloc(void * p, const size_t size) -{ - p = realloc(p, size); - if (p == 0) die("Out of memory in xrealloc()"); - return p; -} - -void xfree(void* p) -{ - if (p == 0) - fprintf(stderr, "Attempt to free NULL pointer\n"); - else - free(p); -} - -char * xstrdup(const char * const s) -{ - char *c; - if (s == NULL) - fprintf(stderr, "Attempt to strdup() NULL pointer\n"); - c = malloc(((s == NULL) ? 0 : strlen(s)) + 1); - if (c == NULL) die("Out of memory in xstrdup()"); - strcpy(c, (s == NULL) ? "" : s); - return c; -} - - /** * Replace consecutive whitespace with a single space. * @@ -223,13 +189,22 @@ char *cnv_strn_local_enc(const char *s, int length, const ptrdiff_t **back_mapPP /* Buffer at d & back_mapP can be overdimentioned but is certainly * big enough to carry the end result. */ - char *d = xcalloc(length + 1, sizeof(char)); - ptrdiff_t *back_mapP = (back_mapPP != NULL) ? xcalloc(length + 1, sizeof(ptrdiff_t)) : NULL; - char *d0 = d; + char *d, *d0; const char * const s0 = s; + ptrdiff_t *back_mapP = NULL; - if (back_mapPP != NULL) + if (back_mapPP != NULL) { + back_mapP = calloc(length + 1, sizeof(ptrdiff_t)); + if (!back_mapP) + return NULL; *back_mapPP = back_mapP; + } + + d = calloc(length + 1, sizeof(char)); + if (!d) + return NULL; + + d0 = d; while (length != 0) { int u, chars; diff --git a/utils/utils.h b/utils/utils.h index 31ee01bf3..921662a26 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -19,15 +19,12 @@ void die(const char * const error); char * strip(char * const s); int whitespace(const char * str); -void * xcalloc(const size_t n, const size_t size); -void * xrealloc(void * p, const size_t size); -void xfree(void* p); -char * xstrdup(const char * const s); char * squash_whitespace(const char * s); char *cnv_space2nbsp(const char *s); char *cnv_local_enc_str(const char *s, size_t length); char *cnv_str_local_enc(const char *s); -char *cnv_strn_local_enc(const char *s, int length, const ptrdiff_t **back_mapPP); +char *cnv_strn_local_enc(const char *s, int length, + const ptrdiff_t **back_mapPP); bool is_dir(const char *path); void regcomp_wrapper(regex_t *preg, const char *regex, int cflags); void clean_cookiejar(void);