From 53491084865df3620193304d896d048c1acf5882 Mon Sep 17 00:00:00 2001 From: Hung-Te Lin Date: Thu, 28 Apr 2022 17:26:32 +0800 Subject: 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 Change-Id: I0dd0105971a80d857a1b05d9680b34b42dbff7e6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3612645 Reviewed-by: Julius Werner Commit-Queue: Julius Werner --- futility/updater_archive.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/futility/updater_archive.c b/futility/updater_archive.c index 65b448d9..11b4be1e 100644 --- a/futility/updater_archive.c +++ b/futility/updater_archive.c @@ -191,6 +191,7 @@ static int archive_fallback_read_file(void *handle, const char *fname, 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 @@ static int archive_zip_read_file(void *handle, const char *fname, 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 @@ static int archive_walk(struct archive *ar, void *arg, * 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. */ -- cgit v1.2.1