public inbox for [email protected]  
help / color / mirror / Atom feed
From: Chao Li <[email protected]>
To: Postgres hackers <[email protected]>
Subject: Trivial Fix: use palloc_array/repalloc_array for BufFile file arrays
Date: Thu, 25 Dec 2025 11:12:07 +0800
Message-ID: <CAEoWx2m1Vo97Jg9=K7JAZ0xdkg5D=GkgOxZR1=EW7mUfy008fw@mail.gmail.com> (raw)

Hi Hackers,

I noticed this error while working on [1].

In BufFile, the fields is claimed as an array:
```
struct BufFile
{
    File *files; /* palloc'd array with numFiles entries */
```

However, it’s allocated by palloc_object():
```
file->files = palloc_object(File);
```

And reallocated by repalloc():
```
    file->files = (File *) repalloc(file->files,
        (file->numFiles + 1) * sizeof(File));
```

This trivial patch just changes to use palloc_array/repalloc_array, which
makes the intent clearer.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/


Attachments:

  [application/octet-stream] v1-0001-Use-palloc_array-repalloc_array-for-BufFile-file-.patch (2.0K, 3-v1-0001-Use-palloc_array-repalloc_array-for-BufFile-file-.patch)
  download | inline diff:
From d1a92c4aead89e27e4dfd5c892365a7a8973f5db Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Thu, 25 Dec 2025 11:00:26 +0800
Subject: [PATCH v1] Use palloc_array/repalloc_array for BufFile file arrays

No functional change intended.

Author: Chao Li <[email protected]>
---
 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 85b316d879d..c9d5ac8c993 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.39.5 (Apple Git-154)



view thread (5+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Trivial Fix: use palloc_array/repalloc_array for BufFile file arrays
  In-Reply-To: <CAEoWx2m1Vo97Jg9=K7JAZ0xdkg5D=GkgOxZR1=EW7mUfy008fw@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox