futility: updater: always add a \0 when reading files from archives
To simplify parsing text files in future we want the archive_read_file
to always return a NULL terminated string on success.
BUG=None
TEST=make; run test
BRANCH=None
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Change-Id: I0dd0105971a80d857a1b05d9680b34b42dbff7e6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3612645
Reviewed-by: Julius Werner <jwerner@chromium.org>
Commit-Queue: Julius Werner <jwerner@chromium.org>
diff --git a/futility/updater_archive.c b/futility/updater_archive.c
index 65b448d..11b4be1 100644
--- a/futility/updater_archive.c
+++ b/futility/updater_archive.c
@@ -191,6 +191,7 @@
VB2_DEBUG("Reading %s\n", path);
*data = NULL;
*size = 0;
+ /* vb2_read_file already has an extra '\0' in the end. */
r = vb2_read_file(path, data, size) != VB2_SUCCESS;
if (mtime) {
if (stat(path, &st) == 0)
@@ -305,12 +306,13 @@
ERROR("Failed to open entry in ZIP: %s\n", fname);
return 1;
}
- *data = (uint8_t *)malloc(stat.size);
+ *data = (uint8_t *)malloc(stat.size + 1);
if (*data) {
if (zip_fread(fp, *data, stat.size) == stat.size) {
if (mtime)
*mtime = stat.mtime;
*size = stat.size;
+ (*data)[stat.size] = '\0';
} else {
ERROR("Failed to read entry in zip: %s\n", fname);
free(*data);
@@ -448,6 +450,8 @@
* Reads a file from archive.
* If entry name (fname) is an absolute path (/file), always read
* from real file system.
+ * The returned data must always have one extra (not included by size) '\0' in
+ * the end of the allocated buffer for C string processing.
* Returns 0 on success (data and size reflects the file content),
* otherwise non-zero as failure.
*/