Skip to content

Commit aaf68fd

Browse files
committed
net: utils: Port parsing failure in net_ipaddr_parse()
If trying to parse a string like 192.0.2.2:80/foobar and setting the length to 12 which means to parse the IP address and port, the parsing failed because it used one extra character from the string. This issue was not present if the input string was terminated after the port number. Add a test case to catch this problem. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 6798064 commit aaf68fd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

subsys/net/ip/utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,8 @@ static bool parse_ipv4(const char *str, size_t str_len,
864864
return true;
865865
}
866866

867-
memcpy(ipaddr, ptr + 1, str_len - end);
868-
ipaddr[str_len - end] = '\0';
867+
memcpy(ipaddr, ptr + 1, str_len - end - 1);
868+
ipaddr[str_len - end - 1] = '\0';
869869

870870
ret = convert_port(ipaddr, &port);
871871
if (!ret) {

tests/net/utils/src/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,21 @@ ZTEST(test_utils_fn, test_addr_parse)
532532
},
533533
.verdict = true
534534
},
535+
{
536+
.address = "192.0.2.3:80/foobar",
537+
.len = sizeof("192.0.2.3:80") - 1,
538+
.result = {
539+
.sin_family = AF_INET,
540+
.sin_port = htons(80),
541+
.sin_addr = {
542+
.s4_addr[0] = 192,
543+
.s4_addr[1] = 0,
544+
.s4_addr[2] = 2,
545+
.s4_addr[3] = 3
546+
}
547+
},
548+
.verdict = true
549+
},
535550
{
536551
.address = "192.0.2.3/foobar",
537552
.len = sizeof("192.0.2.3/foobar") - 1,

0 commit comments

Comments
 (0)