Skip to content

Commit 150a30e

Browse files
committed
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.
1 parent 0c07ef1 commit 150a30e

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
600600
bool nulls[14];
601601
HeapTuple tuple;
602602
PgBackendStatus *beentry;
603-
SockAddr zero_clientaddr;
604603

605604
MemSet(values, 0, sizeof(values));
606605
MemSet(nulls, 0, sizeof(nulls));
@@ -641,6 +640,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
641640
/* Values only available to same user or superuser */
642641
if (superuser() || beentry->st_userid == GetUserId())
643642
{
643+
SockAddr zero_clientaddr;
644+
644645
switch (beentry->st_state)
645646
{
646647
case STATE_IDLE:
@@ -692,7 +693,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
692693
/* A zeroed client addr means we don't know */
693694
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
694695
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
695-
sizeof(zero_clientaddr) == 0))
696+
sizeof(zero_clientaddr)) == 0)
696697
{
697698
nulls[11] = true;
698699
nulls[12] = true;
@@ -956,7 +957,7 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
956957
/* A zeroed client addr means we don't know */
957958
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
958959
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
959-
sizeof(zero_clientaddr) == 0))
960+
sizeof(zero_clientaddr)) == 0)
960961
PG_RETURN_NULL();
961962

962963
switch (beentry->st_clientaddr.addr.ss_family)
@@ -1003,7 +1004,7 @@ pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
10031004
/* A zeroed client addr means we don't know */
10041005
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
10051006
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1006-
sizeof(zero_clientaddr) == 0))
1007+
sizeof(zero_clientaddr)) == 0)
10071008
PG_RETURN_NULL();
10081009

10091010
switch (beentry->st_clientaddr.addr.ss_family)

0 commit comments

Comments
 (0)