From 8e6b4ee21f486e6800aaa6955ff3d98e1a521b49 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 23 Sep 2016 10:09:52 -0400
Subject: [PATCH] Don't trust CreateFileMapping() to clear the error code on
 success.

We must test GetLastError() even when CreateFileMapping() returns a
non-null handle.  If that value were left over from some previous system
call, we might be fooled into thinking the segment already existed.
Experimentation on Windows 7 suggests that CreateFileMapping() clears
the error code on success, but it is not documented to do so, so let's
not rely on that happening in all Windows releases.

Amit Kapila

Discussion: <20811.1474390987@sss.pgh.pa.us>
---
 src/backend/storage/ipc/dsm_impl.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/backend/storage/ipc/dsm_impl.c b/src/backend/storage/ipc/dsm_impl.c
index 7122c4abe00..dbac7bdac08 100644
--- a/src/backend/storage/ipc/dsm_impl.c
+++ b/src/backend/storage/ipc/dsm_impl.c
@@ -681,6 +681,9 @@ dsm_impl_windows(dsm_op op, dsm_handle handle, Size request_size,
 #endif
 		size_low = (DWORD) request_size;
 
+		/* CreateFileMapping might not clear the error code on success */
+		SetLastError(0);
+
 		hmap = CreateFileMapping(INVALID_HANDLE_VALUE,	/* Use the pagefile */
 								 NULL,	/* Default security attrs */
 								 PAGE_READWRITE,		/* Memory is read/write */
-- 
GitLab