Skip to content

Commit

Permalink
Fix pg_file_write() error handling.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
nmisch committed Mar 12, 2017
1 parent c4613c3 commit 0276da5
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions contrib/adminpack/adminpack.c
Expand Up @@ -141,27 +141,22 @@ pg_file_write(PG_FUNCTION_ARGS)
(ERRCODE_DUPLICATE_FILE,
errmsg("file \"%s\" exists", filename)));

f = fopen(filename, "wb");
f = AllocateFile(filename, "wb");
}
else
f = fopen(filename, "ab");
f = AllocateFile(filename, "ab");

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

if (VARSIZE(data) != 0)
{
count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);

if (count != VARSIZE(data) - VARHDRSZ)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write file \"%s\": %m", filename)));
}
fclose(f);
count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);
if (count != VARSIZE(data) - VARHDRSZ || FreeFile(f))
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write file \"%s\": %m", filename)));

PG_RETURN_INT64(count);
}
Expand Down

0 comments on commit 0276da5

Please sign in to comment.