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 --- utils/url.c | 16 +++++++++++----- utils/utils.h | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'utils') 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