Skip to content

Commit 8427ce4

Browse files
committed
Fix handling of escape sequences in postgres_fdw.application_name
postgres_fdw.application_name relies on MyProcPort to define the data that should be added to escape sequences %u (user name) or %d (database name). However this code could be run in processes that lack a MyProcPort, like an autovacuum process, causing crashes. The code generating the application name is made more flexible with this commit, so as it now generates no data for %u and %d if MyProcPort is missing, and a simple "unknown" if MyProcPort exists, but the expected fields are not set. Reported-by: Alexander Lakhin Author: Kyotaro Horiguchi, Michael Paquier Reviewed-by: Hayato Kuroda, Masahiko Sawada Discussion: https://postgr.es/m/17789-8b31c5a4672b74d9@postgresql.org Backpatch-through: 15
1 parent 038f586 commit 8427ce4

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

contrib/postgres_fdw/option.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ process_pgfdw_appname(const char *appname)
485485
const char *p;
486486
StringInfoData buf;
487487

488-
Assert(MyProcPort != NULL);
489-
490488
initStringInfo(&buf);
491489

492490
for (p = appname; *p != '\0'; p++)
@@ -522,13 +520,29 @@ process_pgfdw_appname(const char *appname)
522520
appendStringInfoString(&buf, cluster_name);
523521
break;
524522
case 'd':
525-
appendStringInfoString(&buf, MyProcPort->database_name);
523+
if (MyProcPort)
524+
{
525+
const char *dbname = MyProcPort->database_name;
526+
527+
if (dbname)
528+
appendStringInfoString(&buf, dbname);
529+
else
530+
appendStringInfoString(&buf, "[unknown]");
531+
}
526532
break;
527533
case 'p':
528534
appendStringInfo(&buf, "%d", MyProcPid);
529535
break;
530536
case 'u':
531-
appendStringInfoString(&buf, MyProcPort->user_name);
537+
if (MyProcPort)
538+
{
539+
const char *username = MyProcPort->user_name;
540+
541+
if (username)
542+
appendStringInfoString(&buf, username);
543+
else
544+
appendStringInfoString(&buf, "[unknown]");
545+
}
532546
break;
533547
default:
534548
/* format error - ignore it */

0 commit comments

Comments
 (0)