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
This commit is contained in:
Patrick Monnerat 2018-04-04 15:28:28 +02:00
parent 256b80fe81
commit 82dfdac5f7
2 changed files with 21 additions and 24 deletions

View file

@ -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;
}
/*