From 1664683fa96942edd5270f217779a2e395e308a1 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Tue, 31 Mar 2009 12:23:12 +0000 Subject: Tidy browser_window_refresh_url_bar a bit, ensure the sizes all make sense, that we don't allocate unnecessarily, etc. svn path=/trunk/netsurf/; revision=7007 --- desktop/browser.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'desktop/browser.c') diff --git a/desktop/browser.c b/desktop/browser.c index c5a52f061..9d96db3ed 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -1099,24 +1099,29 @@ void browser_window_refresh_url_bar(struct browser_window *bw, const char *url, const char *frag) { char *url_buf; - int len = strlen(url); - - len += ((frag) ? strlen(frag) : 0); - - url_buf = malloc(len + 3 /* '#' + '\0' */); - if (url_buf) { - if (frag) { - snprintf(url_buf, len + 2, "%s#%s", url, frag); - *(url_buf + len + 2) = '\0'; - } else { - snprintf(url_buf, len + 1, "%s", url); - *(url_buf + len + 1) = '\0'; - } - gui_window_set_url(bw->window, url_buf); - free(url_buf); - } else { - warn_user("NoMemory", 0); - } + + assert(bw); + assert(url); + + if (frag == NULL) { + /* With no fragment, we may as well pass url straight through + * saving a malloc, copy, free cycle. + */ + gui_window_set_url(bw->window, url); + } else { + url_buf = malloc(strlen(url) + 1 /* # */ + + strlen(frag) + 1 /* \0 */); + if (url_buf != NULL) { + /* This sprintf is safe because of the above size + * calculation, thus we don't need snprintf + */ + sprintf(url_buf, "%s#%s", url, frag); + gui_window_set_url(bw->window, url_buf); + free(url_buf); + } else { + warn_user("NoMemory", 0); + } + } } /** -- cgit v1.2.3