Skip to content

Commit 08c6d42

Browse files
committed
Fix pg_file_write() error handling.
Detect fclose() failures; given "ln -s /dev/full $PGDATA/devfull", "pg_file_write('devfull', 'x', true)" now fails as it should. Don't leak a stream when fwrite() fails. Remove a born-ineffective test that aimed to skip zero-length writes. Back-patch to 9.2 (all supported versions).
1 parent 8469923 commit 08c6d42

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

contrib/adminpack/adminpack.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,22 @@ pg_file_write(PG_FUNCTION_ARGS)
136136
(ERRCODE_DUPLICATE_FILE,
137137
errmsg("file \"%s\" exists", filename)));
138138

139-
f = fopen(filename, "wb");
139+
f = AllocateFile(filename, "wb");
140140
}
141141
else
142-
f = fopen(filename, "ab");
142+
f = AllocateFile(filename, "ab");
143143

144144
if (!f)
145145
ereport(ERROR,
146146
(errcode_for_file_access(),
147147
errmsg("could not open file \"%s\" for writing: %m",
148148
filename)));
149149

150-
if (VARSIZE(data) != 0)
151-
{
152-
count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);
153-
154-
if (count != VARSIZE(data) - VARHDRSZ)
155-
ereport(ERROR,
156-
(errcode_for_file_access(),
157-
errmsg("could not write file \"%s\": %m", filename)));
158-
}
159-
fclose(f);
150+
count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);
151+
if (count != VARSIZE(data) - VARHDRSZ || FreeFile(f))
152+
ereport(ERROR,
153+
(errcode_for_file_access(),
154+
errmsg("could not write file \"%s\": %m", filename)));
160155

161156
PG_RETURN_INT64(count);
162157
}

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