Skip to content

Commit 918d74a

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 7016d97 commit 918d74a

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
@@ -486,7 +486,6 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
486486
bool nulls[11];
487487
HeapTuple tuple;
488488
PgBackendStatus *beentry;
489-
SockAddr zero_clientaddr;
490489

491490
MemSet(values, 0, sizeof(values));
492491
MemSet(nulls, 0, sizeof(nulls));
@@ -527,6 +526,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
527526
/* Values only available to same user or superuser */
528527
if (superuser() || beentry->st_userid == GetUserId())
529528
{
529+
SockAddr zero_clientaddr;
530+
530531
if (*(beentry->st_activity) == '\0')
531532
{
532533
values[4] = CStringGetTextDatum("<command string not enabled>");
@@ -556,7 +557,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
556557
/* A zeroed client addr means we don't know */
557558
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
558559
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
559-
sizeof(zero_clientaddr) == 0))
560+
sizeof(zero_clientaddr)) == 0)
560561
{
561562
nulls[9] = true;
562563
nulls[10] = true;
@@ -809,7 +810,7 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
809810
/* A zeroed client addr means we don't know */
810811
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
811812
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
812-
sizeof(zero_clientaddr) == 0))
813+
sizeof(zero_clientaddr)) == 0)
813814
PG_RETURN_NULL();
814815

815816
switch (beentry->st_clientaddr.addr.ss_family)
@@ -856,7 +857,7 @@ pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
856857
/* A zeroed client addr means we don't know */
857858
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
858859
if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
859-
sizeof(zero_clientaddr) == 0))
860+
sizeof(zero_clientaddr)) == 0)
860861
PG_RETURN_NULL();
861862

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

0 commit comments

Comments
 (0)