diff --git a/app/src/adb/adb_parser.c b/app/src/adb/adb_parser.c index ab121347..e7358403 100644 --- a/app/src/adb/adb_parser.c +++ b/app/src/adb/adb_parser.c @@ -204,6 +204,7 @@ sc_adb_parse_device_ip(char *str) { while (str[idx_line] != '\0') { char *line = &str[idx_line]; size_t len = strcspn(line, "\n"); + bool is_last_line = line[len] == '\0'; // The same, but without any trailing '\r' size_t line_len = sc_str_remove_trailing_cr(line, len); @@ -215,12 +216,12 @@ sc_adb_parse_device_ip(char *str) { return ip; } - idx_line += len; - - if (str[idx_line] != '\0') { - // The next line starts after the '\n' - ++idx_line; + if (is_last_line) { + break; } + + // The next line starts after the '\n' + idx_line += len + 1; } return NULL; diff --git a/app/tests/test_adb_parser.c b/app/tests/test_adb_parser.c index d95e7ef2..362b254f 100644 --- a/app/tests/test_adb_parser.c +++ b/app/tests/test_adb_parser.c @@ -217,6 +217,18 @@ static void test_get_ip_multiline_second_ok(void) { free(ip); } +static void test_get_ip_multiline_second_ok_without_cr(void) { + char ip_route[] = "10.0.0.0/24 dev rmnet proto kernel scope link src " + "10.0.0.3\n" + "192.168.1.0/24 dev wlan0 proto kernel scope link src " + "192.168.1.3\n"; + + char *ip = sc_adb_parse_device_ip(ip_route); + assert(ip); + assert(!strcmp(ip, "192.168.1.3")); + free(ip); +} + static void test_get_ip_no_wlan(void) { char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src " "192.168.12.34\r\r\n"; @@ -259,6 +271,7 @@ int main(int argc, char *argv[]) { test_get_ip_single_line_with_trailing_space(); test_get_ip_multiline_first_ok(); test_get_ip_multiline_second_ok(); + test_get_ip_multiline_second_ok_without_cr(); test_get_ip_no_wlan(); test_get_ip_no_wlan_without_eol(); test_get_ip_truncated();