surf-adblock

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 7f8481c1af8ddf550336c6ee273a3ffb1e9fb1fb
parent aefcf5d53e906a9525e58ecc9ce9e661faef20d9
Author: Quentin Rameau <quinq@fifth.space>
Date:   Sun, 17 Jul 2016 19:47:56 +0200

use a fixed-size buffer for domain name extraction

We know per RFC 1035 (2.3.4. Size limits) [1] that the maximum data
storage length for a domain name is 255 bytes.

[1] https://tools.ietf.org/rfc/rfc1035.txt

Diffstat:
surf-adblock.c | 27+++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/surf-adblock.c b/surf-adblock.c @@ -735,22 +735,23 @@ newpage(WebKitWebPage *page) static void documentloaded(WebKitWebPage *wp, Page *p) { + char domain[256]; WebKitDOMDocument *doc = webkit_web_page_get_dom_document(wp); WebKitDOMHTMLElement *body = webkit_dom_document_get_body(doc); WebKitDOMElement *el; String sitecss; struct filterrule *r; - const char *uri = webkit_web_page_get_uri(p->webpage); - char *domain; + const char *s, *uri = webkit_web_page_get_uri(p->webpage); size_t len; if (!uri || (strncmp(uri, "http://", sizeof("http://") - 1) && strncmp(uri, "https://", sizeof("https://") - 1))) return; - domain = strstr(uri, "://") + sizeof("://") - 1; - if (!(domain = westrndup(domain, strcspn(domain, "/")))) - return; + s = strstr(uri, "://") + sizeof("://") - 1; + len = strcspn(s, "/"); + memcpy(domain, s, len); + domain[len] = '\0'; printf("uri: %s\n", uri); printf("domain: %s\n", domain); @@ -790,8 +791,6 @@ documentloaded(WebKitWebPage *wp, Page *p) WEBKIT_DOM_NODE(el), NULL); } #endif - - free(domain); free(sitecss.data); } @@ -799,19 +798,21 @@ static gboolean sendrequest(WebKitWebPage *wp, WebKitURIRequest *req, WebKitURIResponse *res, Page *p) { + char domain[256]; struct filterrule *r; - const char *uri = webkit_web_page_get_uri(p->webpage), + const char *s, *uri = webkit_web_page_get_uri(p->webpage), *requri = webkit_uri_request_get_uri(req); - char *domain; + size_t len; if (!uri || !strcmp(requri, uri) || (strncmp(uri, "http://", sizeof("http://") - 1) && strncmp(uri, "https://", sizeof("https://") - 1))) return FALSE; - domain = strstr(uri, "://") + sizeof("://") - 1; - if (!(domain = westrndup(domain, strcspn(domain, "/")))) - return FALSE; + s = strstr(uri, "://") + sizeof("://") - 1; + len = strcspn(s, "/"); + memcpy(domain, s, len); + domain[len] = '\0'; /* match rules */ for (r = rules; r; r = r->next) { @@ -822,11 +823,9 @@ sendrequest(WebKitWebPage *wp, WebKitURIRequest *req, fprintf(stderr, "blocked: %s, %s\n", domain, requri); - free(domain); return TRUE; } } - free(domain); return FALSE; }