util/amdfwtool/amdfwtool.c: Check for negative return

File open function <open()> will return -1 if there's any error. Check that
the return is greater or equal to 0 before using fstat(). Print error message
and exit if there's an error.

This fixes CIDs 1353018, 1353020, 1353027 and 1353028

BUG=b:72062481
TEST=Build no errors

Change-Id: I77d6973d1ad1eadb93922866e618038045be5937
Signed-off-by: Richard Spiegel <richard.spiegel@silverbackltd.com>
Reviewed-on: https://review.coreboot.org/23303
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
This commit is contained in:
Richard Spiegel 2018-01-17 09:58:13 -07:00 committed by Martin Roth
parent 8bd8cd3a22
commit bff4545ccf
1 changed files with 10 additions and 0 deletions

View File

@ -292,6 +292,11 @@ static uint32_t integrate_firmwares(char *base, uint32_t pos, uint32_t *romsig,
for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
if (fw_table[i].filename != NULL) { if (fw_table[i].filename != NULL) {
fd = open(fw_table[i].filename, O_RDONLY); fd = open(fw_table[i].filename, O_RDONLY);
if (fd < 0) {
printf("Error: %s\n", strerror(errno));
free(base);
exit(1);
}
fstat(fd, &fd_stat); fstat(fd, &fd_stat);
switch (fw_table[i].type) { switch (fw_table[i].type) {
@ -349,6 +354,11 @@ static uint32_t integrate_psp_firmwares(char *base, uint32_t pos,
pspdir[4+4*i+0] = fw_table[i].type; pspdir[4+4*i+0] = fw_table[i].type;
fd = open(fw_table[i].filename, O_RDONLY); fd = open(fw_table[i].filename, O_RDONLY);
if (fd < 0) {
printf("Error: %s\n", strerror(errno));
free(base);
exit(1);
}
fstat(fd, &fd_stat); fstat(fd, &fd_stat);
pspdir[4+4*i+1] = (uint32_t)fd_stat.st_size; pspdir[4+4*i+1] = (uint32_t)fd_stat.st_size;