1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-01 22:21:16 +02:00

flashrom: rewrite flashbuses_to_text()

The previous implementation had no error handling, as a result the
flashrom could crash if the computer ran out of memory. The new
version returns NULL in such cases.

Also, rewrite lots of `if` conditions to one cycle, store a name of
buses and `enum chipbustype` in an array by using a custom struct.

The caller always expected a non-null value, so change its behavior to
handle a possible null value or use the `?` symbol. As far as `free()`
can handle null pointers, do nothing with such callers.

TEST=ninja test

Change-Id: I59b9044c99b4ba6c00d8c97f1e91af09d70dce2c
Signed-off-by: Alexander Goncharov <chat@joursoir.net>
Ticket: https://ticket.coreboot.org/issues/408
Reviewed-on: https://review.coreboot.org/c/flashrom/+/73039
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
This commit is contained in:
Alexander Goncharov
2023-02-14 19:31:44 +04:00
committed by Anastasia Klimchuk
parent 590a621e16
commit 5affc221f3
4 changed files with 54 additions and 23 deletions

10
print.c
View File

@ -103,6 +103,10 @@ static int print_supported_chips(void)
} while (1);
s = flashbuses_to_text(chip->bustype);
if (s == NULL) {
msg_gerr("Out of memory!\n");
return 1;
}
maxtypelen = max(maxtypelen, strlen(s));
free(s);
}
@ -265,6 +269,12 @@ static int print_supported_chips(void)
msg_ginfo(" ");
s = flashbuses_to_text(chip->bustype);
if (s == NULL) {
msg_gerr("Out of memory!\n");
free(ven);
free(dev);
return 1;
}
msg_ginfo("%s", s);
for (i = strlen(s); i < maxtypelen; i++)
msg_ginfo(" ");