From 4875ae40161ce7b7af6d9296864219ad5a27d20a Mon Sep 17 00:00:00 2001 From: Hung-Te Lin Date: Thu, 24 Sep 2020 15:54:39 +0800 Subject: lib21: always append '\0' in vb2_readfile For utilities reading text file, it is easier to process as ASCIIZ input if vb2_readfile can always return a buffer ends with '\0' so we don't need to pass and check the size. BUG=None TEST=make clean && make runtests BRANCH=None Signed-off-by: Hung-Te Lin Change-Id: Ib6294969fb325b9b7899e6295fb1817ad91a9952 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2426092 Reviewed-by: Joel Kitching (cherry picked from commit 7c6bf3080a20077f1da49bc383297ac33bce35f8) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2514762 Reviewed-by: Evan Green Commit-Queue: Karthikeyan Ramasubramanian Tested-by: Karthikeyan Ramasubramanian --- host/lib/include/host_misc.h | 1 + host/lib21/host_misc.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/host/lib/include/host_misc.h b/host/lib/include/host_misc.h index 9759d44d..0249420b 100644 --- a/host/lib/include/host_misc.h +++ b/host/lib/include/host_misc.h @@ -44,6 +44,7 @@ vb2_error_t WriteFile(const char* filename, const void *data, uint64_t size); /** * Read data from a file into a newly allocated buffer. + * The buffer will end with an extra null byte ('\0', not counted in size). * * @param filename Name of file to read from * @param data_ptr On exit, pointer to newly allocated buffer with data diff --git a/host/lib21/host_misc.c b/host/lib21/host_misc.c index ebc4eac5..6c0e0ef3 100644 --- a/host/lib21/host_misc.c +++ b/host/lib21/host_misc.c @@ -42,11 +42,12 @@ vb2_error_t vb2_read_file(const char *filename, uint8_t **data_ptr, return VB2_ERROR_READ_FILE_SIZE; } - buf = malloc(size); + buf = malloc(size + 1); if (!buf) { fclose(f); return VB2_ERROR_READ_FILE_ALLOC; } + buf[size] = '\0'; if(1 != fread(buf, size, 1, f)) { VB2_DEBUG("Unable to read file %s\n", filename); -- cgit v1.2.1