use a fixed-size buffer for domain name extraction - surf-adblock - Surf adblock web extension
 (HTM) git clone git://git.codemadness.org/surf-adblock
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 08fb7a7ce1e8ac16f40f72820b3122bcc30c2100
 (DIR) parent c6ad19fedcfc3ae813187e47b90b1008edf047ea
 (HTM) 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:
         M surf-adblock.c                      |      27 +++++++++++++--------------
       
       1 file changed, 13 insertions(+), 14 deletions(-)
       ---
 (DIR) 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;
        }