Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trurl: fix memory leaks on errors #335

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1319,41 +1319,48 @@ static char *encodequery(char *str, size_t len)
static struct string *memdupzero(char *source, size_t len, bool *modified)
{
struct string *ret = calloc(1, sizeof(struct string));
char *left = NULL;
char *right = NULL;
char *el = NULL;
char *er = NULL;
char *encode = NULL;
if(!ret)
return NULL;

if(len) {
char *sep = memchr(source, '=', len);
char *encode = NULL;
int olen;
if(!sep) { /* no '=' */
char *decode = decodequery(source, (int)len, &olen);
if(decode)
encode = encodequery(decode, olen);
else
goto error;
curl_free(decode);
}
else {
int llen;
int rlen;

/* decode both sides */
char *left = decodequery(source, (int)(sep - source), &llen);
char *right = decodequery(sep + 1,
(int)len - (int)(sep - source) - 1, &rlen);
left = decodequery(source, (int)(sep - source), &llen);
if(!left)
goto error;
right = decodequery(sep + 1,
(int)len - (int)(sep - source) - 1, &rlen);
if(!right)
goto error;
/* encode both sides again */
char *el;
char *er;
if(!left || !right)
return NULL;
el = encodequery(left, llen);
if(!el)
goto error;
er = encodequery(right, rlen);
if(!el || !er)
return NULL;
if(!er)
goto error;

encode = curl_maprintf("%s=%s", el, er);
curl_free(left);
curl_free(right);
free(el);
free(er);
if(!encode)
goto error;
}
olen = (int)strlen(encode);

Expand All @@ -1362,7 +1369,19 @@ static struct string *memdupzero(char *source, size_t len, bool *modified)
ret->str = encode;
ret->len = olen;
}
curl_free(left);
curl_free(right);
free(el);
free(er);
return ret;
error:
curl_free(left);
curl_free(right);
free(el);
free(er);
free(encode);
free(ret);
return NULL;
}

/* URL decode the pair and return it in an allocated chunk */
Expand Down Expand Up @@ -1625,10 +1644,10 @@ static char *canonical_path(const char *path)

/* Then URL encode it again */
npath = curl_easy_escape(NULL, opath, olen);
curl_free(opath);
if(!npath)
return NULL;

curl_free(opath);
ndupe = curl_maprintf("%s%s%s", dupe ? dupe : "", npath, sl ? "/": "");
curl_free(npath);
}
Expand Down