[PATCH 20/30] libfile: null-terminate read_file of wchar_t buffer

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Nov 22 00:47:22 PST 2021


read_file always nul-terminates the buffer, which is useful when
slurping ASCII text into a variable. This doesn't work as well for
UCS-2, because we need two aligned nuls there. Fix this.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 lib/libfile.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index 40b1d8bb2717..6b373f05ca72 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -213,7 +213,8 @@ again:
 		goto again;
 	}
 
-	buf = calloc(read_size + 1, 1);
+	/* ensure wchar_t nul termination */
+	buf = calloc(ALIGN(read_size, 2) + 2, 1);
 	if (!buf) {
 		ret = -ENOMEM;
 		errno = ENOMEM;
-- 
2.30.2




More information about the barebox mailing list