Skip to content

Commit 5bace41

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 108a22b commit 5bace41

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
@@ -461,8 +461,6 @@ process_pgfdw_appname(const char *appname)
461461
const char *p;
462462
StringInfoData buf;
463463

464-
Assert(MyProcPort != NULL);
465-
466464
initStringInfo(&buf);
467465

468466
for (p = appname; *p != '\0'; p++)
@@ -498,13 +496,29 @@ process_pgfdw_appname(const char *appname)
498496
appendStringInfoString(&buf, cluster_name);
499497
break;
500498
case 'd':
501-
appendStringInfoString(&buf, MyProcPort->database_name);
499+
if (MyProcPort)
500+
{
501+
const char *dbname = MyProcPort->database_name;
502+
503+
if (dbname)
504+
appendStringInfoString(&buf, dbname);
505+
else
506+
appendStringInfoString(&buf, "[unknown]");
507+
}
502508
break;
503509
case 'p':
504510
appendStringInfo(&buf, "%d", MyProcPid);
505511
break;
506512
case 'u':
507-
appendStringInfoString(&buf, MyProcPort->user_name);
513+
if (MyProcPort)
514+
{
515+
const char *username = MyProcPort->user_name;
516+
517+
if (username)
518+
appendStringInfoString(&buf, username);
519+
else
520+
appendStringInfoString(&buf, "[unknown]");
521+
}
508522
break;
509523
default:
510524
/* format error - ignore it */

0 commit comments

Comments
 (0)