mirror of
https://github.com/netsurf-browser/netsurf
synced 2024-12-27 14:29:42 +03:00
- 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
This commit is contained in:
parent
e89c346573
commit
793d466edf
@ -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) {
|
||||
|
@ -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));
|
||||
|
16
utils/url.c
16
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;
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user