Fix redundant memset after palloc0 in heap_form_minimal_tuple()
Hi Hackers,
While studying the other patch, I happened to notice this problem
in heap_form_minimal_tuple().
The current code does both palloc0() and memset(0)
in heap_form_minimal_tuple():
```
/*
* Allocate and zero the space needed.
*/
mem = palloc0(len + extra);
memset(mem, 0, extra);
tuple = (MinimalTuple) (mem + extra);
```
That looks like an oversight of a0942f4.
To fix the problem, my first impression was to delete the memset(). But
looking at a0942f4, I found a couple of other places that do the same
pattern: palloc(len+extra) then memset(0, extra), so I think the correct
fix should be changing the palloc0 to palloc.
Best regards,
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v1-0001-Fix-redundant-zero-initialization-in-heap_form_mi.patchapplication/octet-stream; name=v1-0001-Fix-redundant-zero-initialization-in-heap_form_mi.patchDownload
From 310a8f6a700abc82f37934dc58808af25e077882 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 4 Nov 2025 16:45:40 +0800
Subject: [PATCH v1] Fix redundant zero-initialization in
heap_form_minimal_tuple()
heap_form_minimal_tuple() performed both palloc0() and memset(0),
resulting in unnecessary double zeroing. This appears to have been
introduced by commit a0942f4 as an oversight.
While simply removing the memset() would also correct the issue,
similar patterns elsewhere allocate with palloc() and then memset()
only the required "extra" bytes. To keep the code consistent with
those cases, replace palloc0() with palloc() here.
Author: Chao Li <lic@highgo.com>
---
src/backend/access/common/heaptuple.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/backend/access/common/heaptuple.c b/src/backend/access/common/heaptuple.c
index 1173a6d81b5..fff2f8802f8 100644
--- a/src/backend/access/common/heaptuple.c
+++ b/src/backend/access/common/heaptuple.c
@@ -1501,7 +1501,7 @@ heap_form_minimal_tuple(TupleDesc tupleDescriptor,
/*
* Allocate and zero the space needed.
*/
- mem = palloc0(len + extra);
+ mem = palloc(len + extra);
memset(mem, 0, extra);
tuple = (MinimalTuple) (mem + extra);
--
2.39.5 (Apple Git-154)
On Tue, 2025-11-04 at 17:05 +0800, Chao Li wrote:
The current code does both palloc0() and memset(0)
in heap_form_minimal_tuple():
Thank you, fixed.
To fix the problem, my first impression was to delete the memset().
But looking at a0942f4, I found a couple of other places that do the
same pattern: palloc(len+extra) then memset(0, extra), so I think the
correct fix should be changing the palloc0 to palloc.
The palloc0() is important because heap_fill_tuple() skips over
alignment bytes. So I just removed the memset().
Regards,
Jeff Davis