Skip to content

Commit a133bf7

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 a09e3fd commit a133bf7

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/backend/utils/adt/pgstatfuncs.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,6 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
618618
bool nulls[14];
619619
HeapTuple tuple;
620620
PgBackendStatus *beentry;
621-
SockAddr zero_clientaddr;
622621

623622
MemSet(values, 0, sizeof(values));
624623
MemSet(nulls, 0, sizeof(nulls));
@@ -659,6 +658,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
659658
/* Values only available to same user or superuser */
660659
if (superuser() || beentry->st_userid == GetUserId())
661660
{
661+
SockAddr zero_clientaddr;
662+
662663
switch (beentry->st_state)
663664
{
664665
case STATE_IDLE:
@@ -710,7 +711,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
710711
/* A zeroed client addr means we don't know */
711712
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
712713
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
713-
sizeof(zero_clientaddr) == 0))
714+
sizeof(zero_clientaddr)) == 0)
714715
{
715716
nulls[11] = true;
716717
nulls[12] = true;
@@ -974,7 +975,7 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
974975
/* A zeroed client addr means we don't know */
975976
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
976977
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
977-
sizeof(zero_clientaddr) == 0))
978+
sizeof(zero_clientaddr)) == 0)
978979
PG_RETURN_NULL();
979980

980981
switch (beentry->st_clientaddr.addr.ss_family)
@@ -1021,7 +1022,7 @@ pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
10211022
/* A zeroed client addr means we don't know */
10221023
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
10231024
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1024-
sizeof(zero_clientaddr) == 0))
1025+
sizeof(zero_clientaddr)) == 0)
10251026
PG_RETURN_NULL();
10261027

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

0 commit comments

Comments
 (0)