From 793d466edfa715e29b1d6141f42a7a837c4f446a Mon Sep 17 00:00:00 2001 From: John Tytgat Date: Sun, 25 May 2008 15:51:30 +0000 Subject: - introduction of SLEN() - enforce result of url_normalize() being NULL on failure - plug memory leak when url_normalize() fails svn path=/trunk/netsurf/; revision=4200 --- content/fetchers/fetch_data.c | 2 +- riscos/gui.c | 8 ++++---- utils/url.c | 16 +++++++++++----- utils/utils.h | 8 ++++++++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/content/fetchers/fetch_data.c b/content/fetchers/fetch_data.c index f6d817e64..1cc4b411a 100644 --- a/content/fetchers/fetch_data.c +++ b/content/fetchers/fetch_data.c @@ -158,7 +158,7 @@ static bool fetch_data_process(struct fetch_data_context *c) } /* skip the data: part */ - params = c->url + sizeof("data:") - 1; + params = c->url + SLEN("data:"); /* find the comma */ if ( (comma = strchr(params, ',')) == NULL) { diff --git a/riscos/gui.c b/riscos/gui.c index f7173fe81..6e9aa28e6 100644 --- a/riscos/gui.c +++ b/riscos/gui.c @@ -1998,10 +1998,10 @@ char *path_to_url(const char *path) return NULL; } - memcpy(url, "file://", sizeof("file://")-1); + memcpy(url, "file://", SLEN("file://")); if (__unixify(buffer, __RISCOSIFY_NO_REVERSE_SUFFIX, - url + sizeof("file://")-1, - 1 - spare + 10 - (sizeof("file://")-1), + url + SLEN("file://"), + 1 - spare + 10 - SLEN("file://"), 0) == NULL) { LOG(("__unixify failed: %s", buffer)); free(buffer); @@ -2011,7 +2011,7 @@ char *path_to_url(const char *path) free(buffer); buffer = NULL; /* We don't want '/' to be escaped. */ - url_err = url_escape(url, sizeof("file://")-1, false, "/", &escurl); + url_err = url_escape(url, SLEN("file://"), false, "/", &escurl); free(url); url = NULL; if (url_err != URL_FUNC_OK) { LOG(("url_escape failed: %s", url)); diff --git a/utils/url.c b/utils/url.c index a9b46ad0c..e81f3f7bd 100644 --- a/utils/url.c +++ b/utils/url.c @@ -115,7 +115,9 @@ bool url_host_is_ip_address(const char *host) { * Normalize a URL. * * \param url an absolute URL - * \param result pointer to pointer to buffer to hold cleaned up url + * \param result pointer to pointer to buffer to hold cleaned up url. Caller + * gets ownership of pointer to buffer value. On failure the + * pointer to buffer value will be NULL. * \return URL_FUNC_OK on success * * If there is no scheme, http:// is added. The scheme and host are @@ -142,7 +144,8 @@ url_func_result url_normalize(const char *url, char **result) /* allocate sufficiently large buffer for new URL */ len = strlen(url); - bufsize = len + sizeof("http://")-1 + sizeof("/")-1 + 1; /* 'http://' + '/' + '\0' */ + /* "+ 1" for the terminating NUL character. */ + bufsize = len + 1 + SLEN("http://") + SLEN("/"); /* work out how much extra to leave for internal whitespace */ for(i = 0; i < len; i++) { if(isspace(url[i])) bufsize += 2; /* ' ' -> '%20' */ @@ -176,18 +179,21 @@ url_func_result url_normalize(const char *url, char **result) * (RFC regex too fussy to tolerate above WSP problems) */ if ((m = regexec(&url_re, norm, 10, match, 0))) { LOG(("url '%s' failed to match regex", url)); + free(norm); + *result = NULL; return URL_FUNC_FAILED; } if (match[URL_RE_SCHEME].rm_so == -1) { /* scheme missing: add http:// and reparse */ /* LOG(("scheme missing: using http"));*/ - memmove(norm + sizeof("http://")-1, norm, len + 1); - memcpy(norm, "http://", sizeof("http://")-1); /* do NOT copy null */ - len += 7; + memmove(norm + SLEN("http://"), norm, len + 1); + memcpy(norm, "http://", SLEN("http://")); /* do NOT copy NUL */ + len += SLEN("http://"); if ((m = regexec(&url_re, norm, 10, match, 0))) { LOG(("url '%s' failed to match regex", norm)); free(norm); + *result = NULL; return URL_FUNC_FAILED; } } diff --git a/utils/utils.h b/utils/utils.h index 8f3516e1d..4eb921e3f 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -40,6 +40,14 @@ #define max(x,y) (((x)>(y))?(x):(y)) #endif +/** + * Calculate length of constant C string. + * + * \param x a constant C string. + * \return the length of C string without its terminating NUL accounted. + */ +#define SLEN(x) (sizeof((x)) - 1) + enum query_response { QUERY_CONTINUE, QUERY_YES, -- cgit v1.2.3