diff options
author | Michael Paquier | 2023-02-21 11:01:43 +0000 |
---|---|---|
committer | Michael Paquier | 2023-02-21 11:01:43 +0000 |
commit | 8427ce4c379ee3774b8a9aca13ca7f4bfb76ba85 (patch) | |
tree | b4200b49621e13beebe2b4638fe06bdc31048e12 | |
parent | 038f586d5f1d90c6ed58544a068696bb81a4e12e (diff) |
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/[email protected]
Backpatch-through: 15
-rw-r--r-- | contrib/postgres_fdw/option.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/contrib/postgres_fdw/option.c b/contrib/postgres_fdw/option.c index 984e4d168a0..d530f7d0860 100644 --- a/contrib/postgres_fdw/option.c +++ b/contrib/postgres_fdw/option.c @@ -485,8 +485,6 @@ process_pgfdw_appname(const char *appname) const char *p; StringInfoData buf; - Assert(MyProcPort != NULL); - initStringInfo(&buf); for (p = appname; *p != '\0'; p++) @@ -522,13 +520,29 @@ process_pgfdw_appname(const char *appname) appendStringInfoString(&buf, cluster_name); break; case 'd': - appendStringInfoString(&buf, MyProcPort->database_name); + if (MyProcPort) + { + const char *dbname = MyProcPort->database_name; + + if (dbname) + appendStringInfoString(&buf, dbname); + else + appendStringInfoString(&buf, "[unknown]"); + } break; case 'p': appendStringInfo(&buf, "%d", MyProcPid); break; case 'u': - appendStringInfoString(&buf, MyProcPort->user_name); + if (MyProcPort) + { + const char *username = MyProcPort->user_name; + + if (username) + appendStringInfoString(&buf, username); + else + appendStringInfoString(&buf, "[unknown]"); + } break; default: /* format error - ignore it */ |