Fix adb get-serialno parsing

The function assumed that the raw output of "adb get-serialno" 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:
Romain Vimont 2022-02-06 12:18:17 +01:00
parent 2ea12f73db
commit 8d540e83c7

View file

@ -417,7 +417,7 @@ sc_adb_get_serialno(struct sc_intr *intr, unsigned flags) {
} }
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 get-serialno", flags); bool ok = process_check_success_intr(intr, pid, "adb get-serialno", flags);
@ -429,7 +429,10 @@ sc_adb_get_serialno(struct sc_intr *intr, unsigned flags) {
return NULL; return NULL;
} }
sc_str_truncate(buf, r, " \r\n"); assert((size_t) r < sizeof(buf));
buf[r] = '\0';
size_t len = strcspn(buf, " \r\n");
buf[len] = '\0';
return strdup(buf); return strdup(buf);
} }