Skip to content

Commit

Permalink
parse_uri: Complete commit cf95355
Browse files Browse the repository at this point in the history
Fix a regression introduced with the last fix, where a password like
"65536" would get rejected as "invalid URI port" due to the temporary
parsing of the port in case the URI does not contain a "user" part at
all.

Add enough unit tests to cover all these URI formatting corner-cases.
  • Loading branch information
liviuchircu committed Sep 15, 2022
1 parent cf95355 commit 9f39981
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
48 changes: 25 additions & 23 deletions parser/parse_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,11 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
int i;
#endif

#define case_port( ch, var) \
#define case_port( ch, var, ovf_check1, ovf_check2) \
case ch: \
(var)=(var)*10+ch-'0'; \
if ((var) > USHRT_MAX) \
if (ovf_check1) \
(var)=(var)*10+ch-'0'; \
if (ovf_check2 && (var) > USHRT_MAX) \
goto error_bad_port; \
break

Expand Down Expand Up @@ -778,16 +779,16 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
found_user=1; /* there is no user part */
s=p+1;
break;
case_port('0', port_no);
case_port('1', port_no);
case_port('2', port_no);
case_port('3', port_no);
case_port('4', port_no);
case_port('5', port_no);
case_port('6', port_no);
case_port('7', port_no);
case_port('8', port_no);
case_port('9', port_no);
case_port('0', port_no, port_no < INT_MAX / 10, 0);
case_port('1', port_no, port_no < INT_MAX / 10, 0);
case_port('2', port_no, port_no < INT_MAX / 10, 0);
case_port('3', port_no, port_no < INT_MAX / 10, 0);
case_port('4', port_no, port_no < INT_MAX / 10, 0);
case_port('5', port_no, port_no < INT_MAX / 10, 0);
case_port('6', port_no, port_no < INT_MAX / 10, 0);
case_port('7', port_no, port_no < INT_MAX / 10, 0);
case_port('8', port_no, port_no < INT_MAX / 10, 0);
case_port('9', port_no, port_no < INT_MAX / 10, 0);
case '[':
case ']':
case ':':
Expand Down Expand Up @@ -873,16 +874,16 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
state=URI_HEADERS;
s=p+1;
break;
case_port('0', port_no);
case_port('1', port_no);
case_port('2', port_no);
case_port('3', port_no);
case_port('4', port_no);
case_port('5', port_no);
case_port('6', port_no);
case_port('7', port_no);
case_port('8', port_no);
case_port('9', port_no);
case_port('0', port_no, 1, 1);
case_port('1', port_no, 1, 1);
case_port('2', port_no, 1, 1);
case_port('3', port_no, 1, 1);
case_port('4', port_no, 1, 1);
case_port('5', port_no, 1, 1);
case_port('6', port_no, 1, 1);
case_port('7', port_no, 1, 1);
case_port('8', port_no, 1, 1);
case_port('9', port_no, 1, 1);
case '&':
case '@':
case ':':
Expand Down Expand Up @@ -1353,6 +1354,7 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
case URI_PASSWORD:
/* this is the port, it can't be the passwd */
if (found_user) goto error_bad_port;
if (port_no > USHRT_MAX) goto error_bad_port;
uri->port.s=s;
uri->port.len=p-s;
uri->port_no=port_no;
Expand Down
17 changes: 13 additions & 4 deletions parser/test/test_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,19 @@ void test_parse_uri(void)
ok(!u.user.s, "puri-0.5");
ok(u.user.len == 0, "puri-0.6");

ok(!parse_uri(STR_L("sip:test@atlanta.org:0"), &u), "puri-0.7");
ok(!parse_uri(STR_L("sip:test@atlanta.org:65535"), &u), "puri-0.8");
ok(parse_uri(STR_L("sip:test@atlanta.org:65536"), &u), "puri-0.9");
ok(parse_uri(STR_L("sip:test@atlanta.org:55555555555555555555"), &u), "puri-0.10");
/* URI port parsing tests, with or w/o a username */
ok(!parse_uri(STR_L("sip:localhost@atlanta.org:0"), &u), "puri-0.7");
ok(!parse_uri(STR_L("sip:localhost@atlanta.org:65535"), &u), "puri-0.8");
ok(parse_uri(STR_L("sip:localhost@atlanta.org:65536"), &u), "puri-0.9");
ok(parse_uri(STR_L("sip:localhost@atlanta.org:55555555555555555555"), &u), "puri-0.10");
ok(!parse_uri(STR_L("sip:localhost:0@atlanta.org"), &u), "puri-0.11");
ok(!parse_uri(STR_L("sip:localhost:65535@atlanta.org"), &u), "puri-0.12");
ok(!parse_uri(STR_L("sip:localhost:65536@atlanta.org"), &u), "puri-0.13");
ok(!parse_uri(STR_L("sip:localhost:5555555555555@atlanta.org"), &u), "puri-0.14");
ok(!parse_uri(STR_L("sip:localhost:0"), &u), "puri-0.15");
ok(!parse_uri(STR_L("sip:localhost:65535"), &u), "puri-0.16");
ok(parse_uri(STR_L("sip:localhost:65536"), &u), "puri-0.17");
ok(parse_uri(STR_L("sip:localhost:55555555555"), &u), "puri-0.18");

in = *_str("sip:alice@atlanta.org;user=phone");
ok(parse_uri(in.s, in.len, &u) == 0, "puri-1");
Expand Down

0 comments on commit 9f39981

Please sign in to comment.