Skip to content

Commit 3001161

Browse files
committed
fastboot: sanitize getvar input and make it more robust
Assuming the first part of the output always contains the variable is wrong. Signed-off-by: Alexander Martinz <amartinz@shiftphones.com>
1 parent 3240639 commit 3001161

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/fastboot.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,49 @@ test("getvar()", async t => {
344344
});
345345

346346
test("getDeviceName()", async t => {
347-
const [[fastboot_true], [fastboot_error]] = fake()(
347+
const [
348+
[fastboot_true],
349+
[fastboot_tab_at_beginning],
350+
[fastboot_whitespaces_all_around],
351+
[fastboot_waiting],
352+
[fastboot_waiting_with_tab],
353+
[fastboot_waiting_with_whitespace],
354+
[fastboot_weird_output],
355+
[fastboot_error]
356+
] = fake()(
348357
["", "product: FP2\nFinished. Total time: 0.000s", 0],
358+
["", "\tproduct: sdm845\nFinished. Total time: 0.001s", 0],
359+
["", " product: lahaina \nFinished. Total time: 0.001s", 0],
360+
[
361+
"",
362+
"< waiting for any device >\nproduct: axolotl\nFinished. Total time: 0.001s",
363+
0
364+
],
365+
[
366+
"",
367+
"< waiting for any device >\n\tproduct: otter \t \nFinished. Total time: 0.001s",
368+
0
369+
],
370+
[
371+
"",
372+
"< waiting for any device >\n product: FP5 \nFinished. Total time: 0.001s",
373+
0
374+
],
375+
[
376+
"",
377+
"\t\n\n \n< waiting for any device >\nThis\nis\npretty\nweird\output\n\n product: qcm6490 \nFinished. Total time: 0.001s",
378+
0
379+
],
349380
["error", "", 0]
350381
);
351382
await Promise.all([
352383
t.is(await fastboot_true.getDeviceName(), "FP2"),
384+
t.is(await fastboot_tab_at_beginning.getDeviceName(), "sdm845"),
385+
t.is(await fastboot_whitespaces_all_around.getDeviceName(), "lahaina"),
386+
t.is(await fastboot_waiting.getDeviceName(), "axolotl"),
387+
t.is(await fastboot_waiting_with_tab.getDeviceName(), "otter"),
388+
t.is(await fastboot_waiting_with_whitespace.getDeviceName(), "FP5"),
389+
t.is(await fastboot_weird_output.getDeviceName(), "qcm6490"),
353390
t.throwsAsync(fastboot_error.getDeviceName(), {
354391
message: 'Unexpected getvar return: "error"'
355392
})

src/fastboot.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,34 @@ export class Fastboot extends Tool {
409409

410410
/** get bootloader var */
411411
async getvar(variable: string): Promise<string> {
412+
// This could be multiple things, such as when `variable == product`:
413+
// - `product: sdm845\nFinished. Total time: 0.001s`
414+
// - `< waiting for any device >\nproduct: sdm845\nFinished. Total time: 0.001s`
415+
// - `< waiting for any device >\n\tproduct: sdm845\nFinished. Total time: 0.001s`
416+
// - `< waiting for any device >\n product: sdm845\nFinished. Total time: 0.001s`
412417
const result = await this.exec("getvar", variable);
413-
const [name, value] = result
418+
419+
// Split result into multiple lines
420+
const resultParts = result
421+
// replace CR line endings with LF
414422
.replace(/\r\n/g, "\n")
415-
.split("\n")[0]
416-
.split(": ");
423+
// split by new lines
424+
.split("\n")
425+
// remove whitespaces
426+
.map(element => element.trim());
427+
428+
// check for lines starting with the wanted variable, e.g.: `product`
429+
const resultPart = resultParts.find(element =>
430+
element.startsWith(variable)
431+
);
432+
433+
const [name, value] = resultPart
434+
? resultPart.split(": ")
435+
: resultParts && resultParts.length
436+
? // for backwards compatibility return the first line as name, if it exists
437+
[resultParts[0], ""]
438+
: // otherwise just return empty name and value
439+
["", ""];
417440

418441
if (name !== variable) {
419442
throw this.error(

0 commit comments

Comments
 (0)