summaryrefslogtreecommitdiff
path: root/src/backend/port/sysv_shmem.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-01-29 20:04:01 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2014-01-29 20:04:01 -0500
commitbf8ee6f157fa587f1c71bc9252e53a34bdc4e8be (patch)
tree839813e16a425d1af197b9136b611d31287747ba /src/backend/port/sysv_shmem.c
parent56c08df55be5a3f12ddc45edcf29b6ecc4111e59 (diff)
Fix unsafe references to errno within error messaging logic.
Various places were supposing that errno could be expected to hold still within an ereport() nest or similar contexts. This isn't true necessarily, though in some cases it accidentally failed to fail depending on how the compiler chanced to order the subexpressions. This class of thinko explains recent reports of odd failures on clang-built versions, typically missing or inappropriate HINT fields in messages. Problem identified by Christian Kruse, who also submitted the patch this commit is based on. (I fixed a few issues in his patch and found a couple of additional places with the same disease.) Back-patch as appropriate to all supported branches.
Diffstat (limited to 'src/backend/port/sysv_shmem.c')
-rw-r--r--src/backend/port/sysv_shmem.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 20e3c321abd..9d170ca69c1 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -96,15 +96,17 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
if (shmid < 0)
{
+ int shmget_errno = errno;
+
/*
* Fail quietly if error indicates a collision with existing segment.
* One would expect EEXIST, given that we said IPC_EXCL, but perhaps
* we could get a permission violation instead? Also, EIDRM might
* occur if an old seg is slated for destruction but not gone yet.
*/
- if (errno == EEXIST || errno == EACCES
+ if (shmget_errno == EEXIST || shmget_errno == EACCES
#ifdef EIDRM
- || errno == EIDRM
+ || shmget_errno == EIDRM
#endif
)
return NULL;
@@ -118,10 +120,8 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
* against SHMMIN in the preexisting-segment case, so we will not get
* EINVAL a second time if there is such a segment.
*/
- if (errno == EINVAL)
+ if (shmget_errno == EINVAL)
{
- int save_errno = errno;
-
shmid = shmget(memKey, 0, IPC_CREAT | IPC_EXCL | IPCProtection);
if (shmid < 0)
@@ -147,8 +147,6 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
elog(LOG, "shmctl(%d, %d, 0) failed: %m",
(int) shmid, IPC_RMID);
}
-
- errno = save_errno;
}
/*
@@ -160,25 +158,26 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
* it should be. SHMMNI violation is ENOSPC, per spec. Just plain
* not-enough-RAM is ENOMEM.
*/
+ errno = shmget_errno;
ereport(FATAL,
(errmsg("could not create shared memory segment: %m"),
errdetail("Failed system call was shmget(key=%lu, size=%lu, 0%o).",
(unsigned long) memKey, (unsigned long) size,
IPC_CREAT | IPC_EXCL | IPCProtection),
- (errno == EINVAL) ?
+ (shmget_errno == EINVAL) ?
errhint("This error usually means that PostgreSQL's request for a shared memory "
"segment exceeded your kernel's SHMMAX parameter, or possibly that "
"it is less than "
"your kernel's SHMMIN parameter.\n"
"The PostgreSQL documentation contains more information about shared "
"memory configuration.") : 0,
- (errno == ENOMEM) ?
+ (shmget_errno == ENOMEM) ?
errhint("This error usually means that PostgreSQL's request for a shared "
"memory segment exceeded your kernel's SHMALL parameter. You might need "
"to reconfigure the kernel with larger SHMALL.\n"
"The PostgreSQL documentation contains more information about shared "
"memory configuration.") : 0,
- (errno == ENOSPC) ?
+ (shmget_errno == ENOSPC) ?
errhint("This error does *not* mean that you have run out of disk space. "
"It occurs either if all available shared memory IDs have been taken, "
"in which case you need to raise the SHMMNI parameter in your kernel, "
@@ -413,9 +412,12 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
AnonymousShmem = mmap(NULL, size, PROT_READ | PROT_WRITE, PG_MMAP_FLAGS,
-1, 0);
if (AnonymousShmem == MAP_FAILED)
+ {
+ int saved_errno = errno;
+
ereport(FATAL,
(errmsg("could not map anonymous shared memory: %m"),
- (errno == ENOMEM) ?
+ (saved_errno == ENOMEM) ?
errhint("This error usually means that PostgreSQL's request "
"for a shared memory segment exceeded available memory "
"or swap space. To reduce the request size (currently "
@@ -423,6 +425,7 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
"perhaps by reducing shared_buffers or "
"max_connections.",
(unsigned long) size) : 0));
+ }
AnonymousShmemSize = size;
/* Now we need only allocate a minimal-sized SysV shmem block. */