Skip to content

Commit

Permalink
trurl: fix misbehavior on empty query param
Browse files Browse the repository at this point in the history
Add several tests to verify.

Reported-by: Peter Schilling
Fixes #351
  • Loading branch information
bagder committed Sep 9, 2024
1 parent f352391 commit e0c2c7f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
73 changes: 73 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2783,5 +2783,78 @@
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"http://example.org/?a=&b=1"
]
},
"expected": {
"stdout": "http://example.org/?a=&b=1\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"http://example.org/?a=1&b="
]
},
"expected": {
"stdout": "http://example.org/?a=1&b=\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"http://example.org/?a=1&b=&c=2"
]
},
"expected": {
"stdout": "http://example.org/?a=1&b=&c=2\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"http://example.org/?a=1&b=&c=2",
"--json"
]
},
"expected": {
"stdout":[
{
"url": "http://example.org/?a=1&b=&c=2",
"parts": {
"scheme": "http",
"host": "example.org",
"path": "/",
"query": "a=1&b=&c=2"
},
"params": [
{
"key": "a",
"value": "1"
},
{
"key": "b",
"value": ""
},
{
"key": "c",
"value": "2"
}
]
}
],
"stderr": "",
"returncode": 0
}
}
]
30 changes: 22 additions & 8 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1378,24 +1378,38 @@ static struct string *memdupzero(char *source, size_t len, bool *modified)
else {
int llen;
int rlen;
int rightside;

/* decode both sides */
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;

/* length on the right side of '=': */
rightside = (int)len - (int)(sep - source) - 1;

if(rightside) {
right = decodequery(sep + 1,
(int)len - (int)(sep - source) - 1, &rlen);
if(!right)
goto error;
}
else {
right = NULL;
rlen = 0;
}

/* encode both sides again */
el = encodequery(left, llen);
if(!el)
goto error;
er = encodequery(right, rlen);
if(!er)
goto error;
if(right) {
er = encodequery(right, rlen);
if(!er)
goto error;
}

encode = curl_maprintf("%s=%s", el, er);
encode = curl_maprintf("%s=%s", el, er ? er : "");
if(!encode)
goto error;
}
Expand Down

0 comments on commit e0c2c7f

Please sign in to comment.