mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-11 18:50:34 +00:00
Fix adb device ip parsing
The parser assumed that its input was a NUL-terminated string, but it was not the case: it is just the raw output of "adb devices ip route". In practice, it was harmless, since the output always ended with '\n' (which was replaced by '\0' on truncation), but it was incorrect nonetheless. Always write a '\0' at the end of the buffer, and explicitly parse as a NUL-terminated string. For that purpose, avoid the error-prone sc_str_truncate() util function.
This commit is contained in:
parent
5b3ae2cb2f
commit
5d6bd8f9cd
4 changed files with 43 additions and 22 deletions
|
@ -8,7 +8,7 @@ static void test_get_ip_single_line() {
|
|||
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||
"192.168.12.34\r\r\n";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(ip);
|
||||
assert(!strcmp(ip, "192.168.12.34"));
|
||||
free(ip);
|
||||
|
@ -18,7 +18,7 @@ static void test_get_ip_single_line_without_eol() {
|
|||
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||
"192.168.12.34";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(ip);
|
||||
assert(!strcmp(ip, "192.168.12.34"));
|
||||
free(ip);
|
||||
|
@ -28,7 +28,7 @@ static void test_get_ip_single_line_with_trailing_space() {
|
|||
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||
"192.168.12.34 \n";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(ip);
|
||||
assert(!strcmp(ip, "192.168.12.34"));
|
||||
free(ip);
|
||||
|
@ -40,7 +40,7 @@ static void test_get_ip_multiline_first_ok() {
|
|||
"10.0.0.0/24 dev rmnet proto kernel scope link src "
|
||||
"10.0.0.2\r\n";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(ip);
|
||||
assert(!strcmp(ip, "192.168.1.2"));
|
||||
free(ip);
|
||||
|
@ -52,7 +52,7 @@ static void test_get_ip_multiline_second_ok() {
|
|||
"192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
||||
"192.168.1.3\r\n";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(ip);
|
||||
assert(!strcmp(ip, "192.168.1.3"));
|
||||
free(ip);
|
||||
|
@ -62,7 +62,15 @@ static void test_get_ip_no_wlan() {
|
|||
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||
"192.168.12.34\r\r\n";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(!ip);
|
||||
}
|
||||
|
||||
static void test_get_ip_no_wlan_without_eol() {
|
||||
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||
"192.168.12.34";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(!ip);
|
||||
}
|
||||
|
||||
|
@ -70,7 +78,7 @@ static void test_get_ip_truncated() {
|
|||
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||
"\n";
|
||||
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route, sizeof(ip_route));
|
||||
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
||||
assert(!ip);
|
||||
}
|
||||
|
||||
|
@ -84,5 +92,6 @@ int main(int argc, char *argv[]) {
|
|||
test_get_ip_multiline_first_ok();
|
||||
test_get_ip_multiline_second_ok();
|
||||
test_get_ip_no_wlan();
|
||||
test_get_ip_no_wlan_without_eol();
|
||||
test_get_ip_truncated();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue