Fix adb connect parsing
The function assumed that the raw output of "adb connect" was a NUL-terminated string, but it is not the case. It this output did not end with a space or a new line character, then sc_str_truncate() would write '\0' over the last character. Even worse, if the output was empty, then sc_str_truncate() would write out-of-bounds. Avoid the error-prone sc_str_truncate() util function.
This commit is contained in:
parent
8d540e83c7
commit
6d41c53b61
1 changed files with 6 additions and 2 deletions
|
@ -339,7 +339,7 @@ sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||||
// case of failure. As a workaround, check if its output starts with
|
// case of failure. As a workaround, check if its output starts with
|
||||||
// "connected".
|
// "connected".
|
||||||
char buf[128];
|
char buf[128];
|
||||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
|
||||||
sc_pipe_close(pout);
|
sc_pipe_close(pout);
|
||||||
|
|
||||||
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
|
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
|
||||||
|
@ -351,11 +351,15 @@ sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert((size_t) r < sizeof(buf));
|
||||||
|
buf[r] = '\0';
|
||||||
|
|
||||||
ok = !strncmp("connected", buf, sizeof("connected") - 1);
|
ok = !strncmp("connected", buf, sizeof("connected") - 1);
|
||||||
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
|
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
|
||||||
// "adb connect" also prints errors to stdout. Since we capture it,
|
// "adb connect" also prints errors to stdout. Since we capture it,
|
||||||
// re-print the error to stderr.
|
// re-print the error to stderr.
|
||||||
sc_str_truncate(buf, r, "\r\n");
|
size_t len = strcspn(buf, "\r\n");
|
||||||
|
buf[len] = '\0';
|
||||||
fprintf(stderr, "%s\n", buf);
|
fprintf(stderr, "%s\n", buf);
|
||||||
}
|
}
|
||||||
return ok;
|
return ok;
|
||||||
|
|
Loading…
Reference in a new issue