From 5f3e6212af7fefb86a081f148b3ef3991ba71a44 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 7 Nov 2024 16:24:31 +0200 Subject: [PATCH] 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 --- subsys/net/ip/utils.c | 4 ++-- tests/net/utils/src/main.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/subsys/net/ip/utils.c b/subsys/net/ip/utils.c index f130a184bcb..5025e326b80 100644 --- a/subsys/net/ip/utils.c +++ b/subsys/net/ip/utils.c @@ -864,8 +864,8 @@ static bool parse_ipv4(const char *str, size_t str_len, return true; } - memcpy(ipaddr, ptr + 1, str_len - end); - ipaddr[str_len - end] = '\0'; + memcpy(ipaddr, ptr + 1, str_len - end - 1); + ipaddr[str_len - end - 1] = '\0'; ret = convert_port(ipaddr, &port); if (!ret) { diff --git a/tests/net/utils/src/main.c b/tests/net/utils/src/main.c index a4ef8db7e0e..15a51e8ca73 100644 --- a/tests/net/utils/src/main.c +++ b/tests/net/utils/src/main.c @@ -532,6 +532,21 @@ ZTEST(test_utils_fn, test_addr_parse) }, .verdict = true }, + { + .address = "192.0.2.3:80/foobar", + .len = sizeof("192.0.2.3:80") - 1, + .result = { + .sin_family = AF_INET, + .sin_port = htons(80), + .sin_addr = { + .s4_addr[0] = 192, + .s4_addr[1] = 0, + .s4_addr[2] = 2, + .s4_addr[3] = 3 + } + }, + .verdict = true + }, { .address = "192.0.2.3/foobar", .len = sizeof("192.0.2.3/foobar") - 1,