From 58f07c98be7415eddb0ec5c6cbaff71538f01020 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Thu, 25 Dec 2025 11:00:26 +0800
Subject: [PATCH v2] buffile: fix misuse of palloc_object() and use array
 allocation helpers

buffile.c allocated arrays of File using palloc_object(File), which is
intended for allocating a single object rather than an array. Replace
these calls with palloc_array(File, 1) to correctly express that the
allocation is for an array.

While here, also replace open-coded repalloc() size calculations such
as sizeof(File) * n with repalloc_array(), which improves readability
and avoids manual size computations.

This is mostly a refactoring change, except for correcting the misuse of
palloc_object().

Author: Chao Li <lic@highgo.com>
Reviewed-by:
Discussion: https://postgr.es/m/CAEoWx2m1Vo97Jg9=K7JAZ0xdkg5D=GkgOxZR1=EW7mUfy008fw@mail.gmail.com
---
 src/backend/storage/file/buffile.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index ddf3a410d6f..cbd89f6b6fe 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -140,7 +140,7 @@ makeBufFile(File firstfile)
 {
 	BufFile    *file = makeBufFileCommon(1);
 
-	file->files = palloc_object(File);
+	file->files = palloc_array(File, 1);
 	file->files[0] = firstfile;
 	file->readOnly = false;
 	file->fileset = NULL;
@@ -171,8 +171,7 @@ extendBufFile(BufFile *file)
 
 	CurrentResourceOwner = oldowner;
 
-	file->files = (File *) repalloc(file->files,
-									(file->numFiles + 1) * sizeof(File));
+	file->files = repalloc_array(file->files, File, file->numFiles + 1);
 	file->files[file->numFiles] = pfile;
 	file->numFiles++;
 }
@@ -271,7 +270,7 @@ BufFileCreateFileSet(FileSet *fileset, const char *name)
 	file = makeBufFileCommon(1);
 	file->fileset = fileset;
 	file->name = pstrdup(name);
-	file->files = palloc_object(File);
+	file->files = palloc_array(File, 1);
 	file->files[0] = MakeNewFileSetSegment(file, 0);
 	file->readOnly = false;
 
@@ -910,8 +909,7 @@ BufFileAppend(BufFile *target, BufFile *source)
 	if (target->resowner != source->resowner)
 		elog(ERROR, "could not append BufFile with non-matching resource owner");
 
-	target->files = (File *)
-		repalloc(target->files, sizeof(File) * newNumFiles);
+	target->files = repalloc_array(target->files, File, newNumFiles);
 	for (i = target->numFiles; i < newNumFiles; i++)
 		target->files[i] = source->files[i - target->numFiles];
 	target->numFiles = newNumFiles;
-- 
2.50.1 (Apple Git-155)

