aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPatrick Monnerat <patrick@monnerat.net>2018-04-04 15:28:28 +0200
committerPatrick Monnerat <patrick@monnerat.net>2018-04-04 15:28:28 +0200
commit82dfdac5f797d2e906b94aef3f82b6a80353a575 (patch)
treed54fe59494f19ed8de89b593d72b1829daee38f0 /lib
parent256b80fe81ad6de63e7fcffbef5e2ce554319ae8 (diff)
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
Diffstat (limited to 'lib')
-rw-r--r--lib/cookie.c17
1 files changed, 7 insertions, 10 deletions
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;
}
/*