Commit a133bf70 authored by Kevin Grittner's avatar Kevin Grittner

Fix misplaced right paren bugs in pgstatfuncs.c.

The bug would only show up if the C sockaddr structure contained
zero in the first byte for a valid address; otherwise it would
fail to fail, which is probably why it went unnoticed for so long.

Patch submitted by Joel Jacobson after seeing an article by Andrey
Karpov in which he reports finding this through static code
analysis using PVS-Studio.  While I was at it I moved a definition
of a local variable referenced in the buggy code to a more local
context.

Backpatch to all supported branches.
parent a09e3fd7
...@@ -618,7 +618,6 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) ...@@ -618,7 +618,6 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
bool nulls[14]; bool nulls[14];
HeapTuple tuple; HeapTuple tuple;
PgBackendStatus *beentry; PgBackendStatus *beentry;
SockAddr zero_clientaddr;
MemSet(values, 0, sizeof(values)); MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls)); MemSet(nulls, 0, sizeof(nulls));
...@@ -659,6 +658,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) ...@@ -659,6 +658,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
/* Values only available to same user or superuser */ /* Values only available to same user or superuser */
if (superuser() || beentry->st_userid == GetUserId()) if (superuser() || beentry->st_userid == GetUserId())
{ {
SockAddr zero_clientaddr;
switch (beentry->st_state) switch (beentry->st_state)
{ {
case STATE_IDLE: case STATE_IDLE:
...@@ -710,7 +711,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) ...@@ -710,7 +711,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
/* A zeroed client addr means we don't know */ /* A zeroed client addr means we don't know */
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr)); memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr, if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
sizeof(zero_clientaddr) == 0)) sizeof(zero_clientaddr)) == 0)
{ {
nulls[11] = true; nulls[11] = true;
nulls[12] = true; nulls[12] = true;
...@@ -974,7 +975,7 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS) ...@@ -974,7 +975,7 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
/* A zeroed client addr means we don't know */ /* A zeroed client addr means we don't know */
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr)); memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr, if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
sizeof(zero_clientaddr) == 0)) sizeof(zero_clientaddr)) == 0)
PG_RETURN_NULL(); PG_RETURN_NULL();
switch (beentry->st_clientaddr.addr.ss_family) switch (beentry->st_clientaddr.addr.ss_family)
...@@ -1021,7 +1022,7 @@ pg_stat_get_backend_client_port(PG_FUNCTION_ARGS) ...@@ -1021,7 +1022,7 @@ pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
/* A zeroed client addr means we don't know */ /* A zeroed client addr means we don't know */
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr)); memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr, if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
sizeof(zero_clientaddr) == 0)) sizeof(zero_clientaddr)) == 0)
PG_RETURN_NULL(); PG_RETURN_NULL();
switch (beentry->st_clientaddr.addr.ss_family) switch (beentry->st_clientaddr.addr.ss_family)
......
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