Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e0c2c7f

Browse files
committedSep 9, 2024··
trurl: fix misbehavior on empty query param
Add several tests to verify. Reported-by: Peter Schilling Fixes #351
1 parent f352391 commit e0c2c7f

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed
 

‎tests.json

+73
Original file line numberDiff line numberDiff line change
@@ -2783,5 +2783,78 @@
27832783
"stderr": "",
27842784
"returncode": 0
27852785
}
2786+
},
2787+
{
2788+
"input": {
2789+
"arguments": [
2790+
"http://example.org/?a=&b=1"
2791+
]
2792+
},
2793+
"expected": {
2794+
"stdout": "http://example.org/?a=&b=1\n",
2795+
"stderr": "",
2796+
"returncode": 0
2797+
}
2798+
},
2799+
{
2800+
"input": {
2801+
"arguments": [
2802+
"http://example.org/?a=1&b="
2803+
]
2804+
},
2805+
"expected": {
2806+
"stdout": "http://example.org/?a=1&b=\n",
2807+
"stderr": "",
2808+
"returncode": 0
2809+
}
2810+
},
2811+
{
2812+
"input": {
2813+
"arguments": [
2814+
"http://example.org/?a=1&b=&c=2"
2815+
]
2816+
},
2817+
"expected": {
2818+
"stdout": "http://example.org/?a=1&b=&c=2\n",
2819+
"stderr": "",
2820+
"returncode": 0
2821+
}
2822+
},
2823+
{
2824+
"input": {
2825+
"arguments": [
2826+
"http://example.org/?a=1&b=&c=2",
2827+
"--json"
2828+
]
2829+
},
2830+
"expected": {
2831+
"stdout":[
2832+
{
2833+
"url": "http://example.org/?a=1&b=&c=2",
2834+
"parts": {
2835+
"scheme": "http",
2836+
"host": "example.org",
2837+
"path": "/",
2838+
"query": "a=1&b=&c=2"
2839+
},
2840+
"params": [
2841+
{
2842+
"key": "a",
2843+
"value": "1"
2844+
},
2845+
{
2846+
"key": "b",
2847+
"value": ""
2848+
},
2849+
{
2850+
"key": "c",
2851+
"value": "2"
2852+
}
2853+
]
2854+
}
2855+
],
2856+
"stderr": "",
2857+
"returncode": 0
2858+
}
27862859
}
27872860
]

‎trurl.c

+22-8
Original file line numberDiff line numberDiff line change
@@ -1378,24 +1378,38 @@ static struct string *memdupzero(char *source, size_t len, bool *modified)
13781378
else {
13791379
int llen;
13801380
int rlen;
1381+
int rightside;
13811382

13821383
/* decode both sides */
13831384
left = decodequery(source, (int)(sep - source), &llen);
13841385
if(!left)
13851386
goto error;
1386-
right = decodequery(sep + 1,
1387-
(int)len - (int)(sep - source) - 1, &rlen);
1388-
if(!right)
1389-
goto error;
1387+
1388+
/* length on the right side of '=': */
1389+
rightside = (int)len - (int)(sep - source) - 1;
1390+
1391+
if(rightside) {
1392+
right = decodequery(sep + 1,
1393+
(int)len - (int)(sep - source) - 1, &rlen);
1394+
if(!right)
1395+
goto error;
1396+
}
1397+
else {
1398+
right = NULL;
1399+
rlen = 0;
1400+
}
1401+
13901402
/* encode both sides again */
13911403
el = encodequery(left, llen);
13921404
if(!el)
13931405
goto error;
1394-
er = encodequery(right, rlen);
1395-
if(!er)
1396-
goto error;
1406+
if(right) {
1407+
er = encodequery(right, rlen);
1408+
if(!er)
1409+
goto error;
1410+
}
13971411

1398-
encode = curl_maprintf("%s=%s", el, er);
1412+
encode = curl_maprintf("%s=%s", el, er ? er : "");
13991413
if(!encode)
14001414
goto error;
14011415
}

0 commit comments

Comments
 (0)
Please sign in to comment.