[PATCH] scripts/recordmcount: Fix uninitialized memory in file_append realloc

From: Cuishuangjin

Date: Mon Nov 02 2026 - 02:41:53 EST


The file_append() function uses realloc() to grow the buffer, but
realloc() doesn't zero the newly allocated memory. This can lead to
uninitialized data being written to the output file.

Fix this by:
1. Checking the realloc() return value for errors
2. Zeroing the newly allocated memory with memset()

Signed-off-by: cys43405 <YS.cuishuangjin@xxxxxxx>
---
scripts/recordmcount.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/scripts/recordmcount.c b/scripts/recordmcount.c
index 3e4f54799cc0..754690887f66 100644
--- a/scripts/recordmcount.c
+++ b/scripts/recordmcount.c
@@ -111,6 +111,7 @@ static ssize_t uwrite(void const *const buf, size_t const count)
size_t cnt = count;
off_t idx = 0;
void *p = NULL;
+ off_t old_size = 0;

file_updated = 1;

@@ -118,18 +119,20 @@ static ssize_t uwrite(void const *const buf, size_t const count)
off_t aoffset = (file_ptr + count) - file_end;

if (aoffset > file_append_size) {
+ old_size = file_append_size;
p = realloc(file_append, aoffset);
- if (!p)
+ if (!p) {
free(file_append);
+ perror("write");
+ file_append_cleanup();
+ mmap_cleanup();
+ return -1;
+ }
file_append = p;
file_append_size = aoffset;
+ memset(file_append + old_size, 0, aoffset - old_size);
}
- if (!file_append) {
- perror("write");
- file_append_cleanup();
- mmap_cleanup();
- return -1;
- }
+
if (file_ptr < file_end) {
cnt = file_end - file_ptr;
} else {
--
2.33.0
-------------------------------------------------------------------------------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址中列出的个人或群组。
禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。
如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above.
Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited.
If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!