summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHung-Te Lin <hungte@chromium.org>2020-09-24 15:54:39 +0800
committerCommit Bot <commit-bot@chromium.org>2020-11-02 19:39:38 +0000
commit4875ae40161ce7b7af6d9296864219ad5a27d20a (patch)
tree3bd24828c086b6f1d0b9a5851b70951be8d7dd5e
parent4421fe553a85e573ca54e92735874e2bcc87f224 (diff)
downloadvboot-stabilize-13505.85.B.tar.gz
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 <hungte@chromium.org> Change-Id: Ib6294969fb325b9b7899e6295fb1817ad91a9952 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2426092 Reviewed-by: Joel Kitching <kitching@chromium.org> (cherry picked from commit 7c6bf3080a20077f1da49bc383297ac33bce35f8) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2514762 Reviewed-by: Evan Green <evgreen@chromium.org> Commit-Queue: Karthikeyan Ramasubramanian <kramasub@chromium.org> Tested-by: Karthikeyan Ramasubramanian <kramasub@chromium.org>
-rw-r--r--host/lib/include/host_misc.h1
-rw-r--r--host/lib21/host_misc.c3
2 files changed, 3 insertions, 1 deletions
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);