diff options
author | Evan Benn <evanbenn@chromium.org> | 2022-12-01 17:42:23 +1100 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-12-11 12:00:44 +0000 |
commit | 601a164a534d80952cf81d68597a1a1fac0da65a (patch) | |
tree | 0800d3a9d19fb877f4f1d4bd6693fb92b14d2343 /futility | |
parent | 33e13362b17e39507a815c4a57153eed657085bb (diff) | |
download | vboot-601a164a534d80952cf81d68597a1a1fac0da65a.tar.gz |
futility: Store errno before printing strerror
errno was being printed and then errno was returned from the function.
This can be incorrect if the print function changes errno. Instead store
errno, print and return the same value.
BUG=b:260531154
BRANCH=None
TEST=FEATURES=test emerge-grunt vboot_reference
TEST=futility gbb -s --flags 0x0 /tmp/bios /tmp/bios2
Change-Id: I5016ac31e56c4a0f16f89a2a52087ba64833d28a
Signed-off-by: Evan Benn <evanbenn@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/4075306
Commit-Queue: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Diffstat (limited to 'futility')
-rw-r--r-- | futility/cmd_gbb_utility.c | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/futility/cmd_gbb_utility.c b/futility/cmd_gbb_utility.c index c3dec20d..41610633 100644 --- a/futility/cmd_gbb_utility.c +++ b/futility/cmd_gbb_utility.c @@ -261,26 +261,28 @@ static int write_to_file(const char *msg, const char *filename, fp = fopen(filename, "wb"); if (!fp) { + r = errno; fprintf(stderr, "ERROR: Unable to open %s for writing: %s\n", - filename, strerror(errno)); + filename, strerror(r)); errorcnt++; - return errno; + return r; } /* Don't write zero bytes */ if (size && 1 != fwrite(start, size, 1, fp)) { - fprintf(stderr, "ERROR: Unable to write to %s: %s\n", - filename, strerror(errno)); - errorcnt++; r = errno; + fprintf(stderr, "ERROR: Unable to write to %s: %s\n", filename, + strerror(r)); + errorcnt++; } if (0 != fclose(fp)) { - fprintf(stderr, "ERROR: Unable to close %s: %s\n", - filename, strerror(errno)); - errorcnt++; + int e = errno; + fprintf(stderr, "ERROR: Unable to close %s: %s\n", filename, + strerror(e)); if (!r) - r = errno; + r = e; + errorcnt++; } if (!r && msg) @@ -299,17 +301,18 @@ static int read_from_file(const char *msg, const char *filename, fp = fopen(filename, "rb"); if (!fp) { + r = errno; fprintf(stderr, "ERROR: Unable to open %s for reading: %s\n", - filename, strerror(errno)); + filename, strerror(r)); errorcnt++; - return errno; + return r; } if (0 != fstat(fileno(fp), &sb)) { - fprintf(stderr, "ERROR: can't fstat %s: %s\n", - filename, strerror(errno)); - errorcnt++; r = errno; + fprintf(stderr, "ERROR: can't fstat %s: %s\n", filename, + strerror(r)); + errorcnt++; goto done_close; } @@ -328,20 +331,21 @@ static int read_from_file(const char *msg, const char *filename, /* It's okay if we read less than size. That's just the max. */ count = fread(start, 1, size, fp); if (ferror(fp)) { + r = errno; fprintf(stderr, "ERROR: Read %zu/%" PRIi64 " bytes from %s: %s\n", - count, sb.st_size, filename, strerror(errno)); + count, sb.st_size, filename, strerror(r)); errorcnt++; - r = errno; } done_close: if (0 != fclose(fp)) { - fprintf(stderr, "ERROR: Unable to close %s: %s\n", - filename, strerror(errno)); + int e = errno; + fprintf(stderr, "ERROR: Unable to close %s: %s\n", filename, + strerror(e)); errorcnt++; if (!r) - r = errno; + r = e; } if (!r && msg) |