Skip to content

Commit

Permalink
Fix "ip route" parsing
Browse files Browse the repository at this point in the history
If a line did not end with '\r', then the final `\n' was replaced by
'\0' for parsing the current line. This `\0` was then mistakenly
considered as the end of the whole "ip route" output, so the remaining
lines were not parsed, causing "scrcpy --tcpip" to fail in some cases.

To fix the issue, read the final character of the current line before it
is (possibly) overwritten by '\0'.
  • Loading branch information
rom1v committed Apr 2, 2023
1 parent f77e1c4 commit 669e9a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 6 additions & 5 deletions app/src/adb/adb_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions app/tests/test_adb_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 669e9a8

Please sign in to comment.