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 <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>
diff --git a/host/lib/include/host_misc.h b/host/lib/include/host_misc.h
index 9759d44..0249420 100644
--- a/host/lib/include/host_misc.h
+++ b/host/lib/include/host_misc.h
@@ -44,6 +44,7 @@
/**
* 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 ebc4eac..6c0e0ef 100644
--- a/host/lib21/host_misc.c
+++ b/host/lib21/host_misc.c
@@ -42,11 +42,12 @@
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);