Skip to content

Commit 1cfdb1c

Browse files
committed
Fix memory leak in BufFileCreateShared().
Also this commit unifies some duplicated code in makeBufFile() and BufFileOpenShared() into new function makeBufFileCommon(). Author: Antonin Houska Reviewed-By: Thomas Munro, Tatsuo Ishii Discussion: https://postgr.es/m/16139.1529049566%40localhost
1 parent ff03112 commit 1cfdb1c

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/backend/storage/file/buffile.c

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,35 +99,46 @@ struct BufFile
9999
char buffer[BLCKSZ];
100100
};
101101

102+
static BufFile *makeBufFileCommon(int nfiles);
102103
static BufFile *makeBufFile(File firstfile);
103104
static void extendBufFile(BufFile *file);
104105
static void BufFileLoadBuffer(BufFile *file);
105106
static void BufFileDumpBuffer(BufFile *file);
106107
static int BufFileFlush(BufFile *file);
107108
static File MakeNewSharedSegment(BufFile *file, int segment);
108109

109-
110110
/*
111-
* Create a BufFile given the first underlying physical file.
112-
* NOTE: caller must set isInterXact if appropriate.
111+
* Create BufFile and perform the common initialization.
113112
*/
114113
static BufFile *
115-
makeBufFile(File firstfile)
114+
makeBufFileCommon(int nfiles)
116115
{
117116
BufFile *file = (BufFile *) palloc(sizeof(BufFile));
118117

119-
file->numFiles = 1;
120-
file->files = (File *) palloc(sizeof(File));
121-
file->files[0] = firstfile;
122-
file->offsets = (off_t *) palloc(sizeof(off_t));
123-
file->offsets[0] = 0L;
118+
file->numFiles = nfiles;
119+
file->offsets = (off_t *) palloc0(sizeof(off_t) * nfiles);
124120
file->isInterXact = false;
125121
file->dirty = false;
126122
file->resowner = CurrentResourceOwner;
127123
file->curFile = 0;
128124
file->curOffset = 0L;
129125
file->pos = 0;
130126
file->nbytes = 0;
127+
128+
return file;
129+
}
130+
131+
/*
132+
* Create a BufFile given the first underlying physical file.
133+
* NOTE: caller must set isInterXact if appropriate.
134+
*/
135+
static BufFile *
136+
makeBufFile(File firstfile)
137+
{
138+
BufFile *file = makeBufFileCommon(1);
139+
140+
file->files = (File *) palloc(sizeof(File));
141+
file->files[0] = firstfile;
131142
file->readOnly = false;
132143
file->fileset = NULL;
133144
file->name = NULL;
@@ -246,23 +257,12 @@ BufFileCreateShared(SharedFileSet *fileset, const char *name)
246257
{
247258
BufFile *file;
248259

249-
file = (BufFile *) palloc(sizeof(BufFile));
260+
file = makeBufFileCommon(1);
250261
file->fileset = fileset;
251262
file->name = pstrdup(name);
252-
file->numFiles = 1;
253263
file->files = (File *) palloc(sizeof(File));
254264
file->files[0] = MakeNewSharedSegment(file, 0);
255-
file->offsets = (off_t *) palloc(sizeof(off_t));
256-
file->offsets[0] = 0L;
257-
file->isInterXact = false;
258-
file->dirty = false;
259-
file->resowner = CurrentResourceOwner;
260-
file->curFile = 0;
261-
file->curOffset = 0L;
262-
file->pos = 0;
263-
file->nbytes = 0;
264265
file->readOnly = false;
265-
file->name = pstrdup(name);
266266

267267
return file;
268268
}
@@ -283,7 +283,6 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name)
283283
File *files;
284284
int nfiles = 0;
285285

286-
file = (BufFile *) palloc(sizeof(BufFile));
287286
files = palloc(sizeof(File) * capacity);
288287

289288
/*
@@ -317,16 +316,8 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name)
317316
(errcode_for_file_access(),
318317
errmsg("could not open BufFile \"%s\"", name)));
319318

320-
file->numFiles = nfiles;
319+
file = makeBufFileCommon(nfiles);
321320
file->files = files;
322-
file->offsets = (off_t *) palloc0(sizeof(off_t) * nfiles);
323-
file->isInterXact = false;
324-
file->dirty = false;
325-
file->resowner = CurrentResourceOwner; /* Unused, can't extend */
326-
file->curFile = 0;
327-
file->curOffset = 0L;
328-
file->pos = 0;
329-
file->nbytes = 0;
330321
file->readOnly = true; /* Can't write to files opened this way */
331322
file->fileset = fileset;
332323
file->name = pstrdup(name);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy