Commit 944a026b authored by Noah Misch's avatar Noah Misch

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).
parent 2fd26b23
...@@ -136,10 +136,10 @@ pg_file_write(PG_FUNCTION_ARGS) ...@@ -136,10 +136,10 @@ pg_file_write(PG_FUNCTION_ARGS)
(ERRCODE_DUPLICATE_FILE, (ERRCODE_DUPLICATE_FILE,
errmsg("file \"%s\" exists", filename))); errmsg("file \"%s\" exists", filename)));
f = fopen(filename, "wb"); f = AllocateFile(filename, "wb");
} }
else else
f = fopen(filename, "ab"); f = AllocateFile(filename, "ab");
if (!f) if (!f)
ereport(ERROR, ereport(ERROR,
...@@ -147,16 +147,11 @@ pg_file_write(PG_FUNCTION_ARGS) ...@@ -147,16 +147,11 @@ pg_file_write(PG_FUNCTION_ARGS)
errmsg("could not open file \"%s\" for writing: %m", errmsg("could not open file \"%s\" for writing: %m",
filename))); filename)));
if (VARSIZE(data) != 0) count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);
{ if (count != VARSIZE(data) - VARHDRSZ || FreeFile(f))
count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f); ereport(ERROR,
(errcode_for_file_access(),
if (count != VARSIZE(data) - VARHDRSZ) errmsg("could not write file \"%s\": %m", filename)));
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write file \"%s\": %m", filename)));
}
fclose(f);
PG_RETURN_INT64(count); PG_RETURN_INT64(count);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment