util/cbfstool/common.c: Deduplicate buffer_create() logic

BUG=b:207808292,b:231152447
TEST=builds with vboot_ref uprev.

Change-Id: Id7d9b6f5254b08720eebb37151e12ee68ed7f8d7
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/65145
Reviewed-by: Julius Werner <jwerner@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Edward O'Callaghan 2022-06-15 11:00:33 +10:00 committed by Julius Werner
parent 774dcffc36
commit 01dbba5e3d
1 changed files with 5 additions and 5 deletions

View File

@ -46,17 +46,17 @@ int buffer_from_file_aligned_size(struct buffer *buffer, const char *filename,
perror(filename); perror(filename);
return -1; return -1;
} }
buffer->offset = 0;
off_t file_size = get_file_size(fp); off_t file_size = get_file_size(fp);
if (file_size < 0) { if (file_size < 0) {
fprintf(stderr, "could not determine size of %s\n", filename); fprintf(stderr, "could not determine size of %s\n", filename);
fclose(fp); fclose(fp);
return -1; return -1;
} }
buffer->size = ALIGN_UP(file_size, size_granularity); if (buffer_create(buffer, ALIGN_UP(file_size, size_granularity), filename)) {
buffer->name = strdup(filename); fprintf(stderr, "could not allocate buffer\n");
buffer->data = (char *)malloc(buffer->size); fclose(fp);
assert(buffer->data); return -1;
}
if (fread(buffer->data, 1, file_size, fp) != (size_t)file_size) { if (fread(buffer->data, 1, file_size, fp) != (size_t)file_size) {
fprintf(stderr, "incomplete read: %s\n", filename); fprintf(stderr, "incomplete read: %s\n", filename);
fclose(fp); fclose(fp);