From 82dfdac5f797d2e906b94aef3f82b6a80353a575 Mon Sep 17 00:00:00 2001 From: Patrick Monnerat Date: Wed, 4 Apr 2018 15:28:28 +0200 Subject: cookie: fix and optimize 2nd top level domain name extraction This fixes a segfault occurring when a name of the (invalid) form "domain..tld" is processed. test46 updated to cover this case. Follow-up to commit c990ead. Ref: https://github.com/curl/curl/pull/2440 --- lib/cookie.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'lib/cookie.c') diff --git a/lib/cookie.c b/lib/cookie.c index 027fa0977..ec3b1bf9a 100644 --- a/lib/cookie.c +++ b/lib/cookie.c @@ -246,26 +246,23 @@ pathmatched: static const char *get_top_domain(const char * const domain, size_t *outlen) { size_t len; - const char *first, *last; + const char *first = NULL, *last; if(!domain) return NULL; len = strlen(domain); - first = memchr(domain, '.', len); last = memrchr(domain, '.', len); + if(last) { + first = memrchr(domain, '.', (size_t) (last - domain)); + if(first) + len -= (size_t) (++first - domain); + } if(outlen) *outlen = len; - if(first == last) - return domain; - - first = memrchr(domain, '.', (size_t)(last - domain - 1)); - if(outlen) - *outlen = len - (size_t)(first - domain) - 1; - - return first + 1; + return first? first: domain; } /* -- cgit v1.2.3