Commit 5550a9c3 authored by Michael Paquier's avatar Michael Paquier

Fix compatibility thinko for fstat() on standard streams in win32stat.c

GetFinalPathNameByHandleA() cannot be used in compilation environments
where _WIN32_WINNT < 0x0600, meaning at least Windows XP used by some
buildfarm members under MinGW that Postgres still needs to support.
This was reported as a compilation warning by the buildfarm, but this is
actually worse than the report as the code would have not worked.

Instead, this switches to GetFileInformationByHandle() that is able to
fail for standard streams and succeed for redirected ones, which is what
we are looking for herein the code emulating fstat().  We also know that
it is able to work in all the environments still supported, thanks to
the existing logic of win32stat.c.

Issue introduced by 10260c7, so backpatch down to 14.

Reported-by: Justin Pryzby, via buildfarm member jacana
Author: Michael Paquier
Reviewed-by: Juan José Santamaría Flecha
Discussion: https://postgr.es/m/20211129050122.GK17618@telsasoft.com
Backpatch-through: 14
parent ce43b3dc
...@@ -289,7 +289,7 @@ int ...@@ -289,7 +289,7 @@ int
_pgfstat64(int fileno, struct stat *buf) _pgfstat64(int fileno, struct stat *buf)
{ {
HANDLE hFile = (HANDLE) _get_osfhandle(fileno); HANDLE hFile = (HANDLE) _get_osfhandle(fileno);
char path[MAX_PATH]; BY_HANDLE_FILE_INFORMATION fiData;
if (hFile == INVALID_HANDLE_VALUE || buf == NULL) if (hFile == INVALID_HANDLE_VALUE || buf == NULL)
{ {
...@@ -306,7 +306,7 @@ _pgfstat64(int fileno, struct stat *buf) ...@@ -306,7 +306,7 @@ _pgfstat64(int fileno, struct stat *buf)
if ((fileno == _fileno(stdin) || if ((fileno == _fileno(stdin) ||
fileno == _fileno(stdout) || fileno == _fileno(stdout) ||
fileno == _fileno(stderr)) && fileno == _fileno(stderr)) &&
GetFinalPathNameByHandleA(hFile, path, MAX_PATH, VOLUME_NAME_NT) == 0) !GetFileInformationByHandle(hFile, &fiData))
{ {
memset(buf, 0, sizeof(*buf)); memset(buf, 0, sizeof(*buf));
buf->st_mode = _S_IFCHR; buf->st_mode = _S_IFCHR;
......
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