Skip to content

Commit 9b468bc

Browse files
committed
Allow "dbname" from connection string to be overridden in PQconnectDBParams
If the "dbname" attribute in PQconnectDBParams contained a connection string or URI (and expand_dbname = TRUE), the database name from the connection string could not be overridden by a subsequent "dbname" keyword in the array. That was not intentional; all other options can be overridden. Furthermore, any subsequent "dbname" caused the connection string from the first dbname value to be processed again, overriding any values for the same options that were given between the connection string and the second dbname option. In the passing, clarify in the docs that only the first dbname option in the array is parsed as a connection string. Alex Shulgin. Backpatch to all supported versions.
1 parent abcab2a commit 9b468bc

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ PGconn *PQconnectdbParams(const char * const *keywords,
124124
<para>
125125
When <literal>expand_dbname</literal> is non-zero, the
126126
<parameter>dbname</parameter> key word value is allowed to be recognized
127-
as a connection string. More details on the possible formats appear in
127+
as a connection string. Only the first occurrence of
128+
<parameter>dbname</parameter> is expanded this way, any subsequent
129+
<parameter>dbname</parameter> value is processed as plain database name. More
130+
details on the possible connection string formats appear in
128131
<xref linkend="libpq-connstring">.
129132
</para>
130133

src/interfaces/libpq/fe-connect.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4400,10 +4400,11 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
44004400
* Defaults are supplied (from a service file, environment variables, etc)
44014401
* for unspecified options, but only if use_defaults is TRUE.
44024402
*
4403-
* If expand_dbname is non-zero, and the value passed for keyword "dbname" is a
4404-
* connection string (as indicated by recognized_connection_string) then parse
4405-
* and process it, overriding any previously processed conflicting
4406-
* keywords. Subsequent keywords will take precedence, however.
4403+
* If expand_dbname is non-zero, and the value passed for the first occurrence
4404+
* of "dbname" keyword is a connection string (as indicated by
4405+
* recognized_connection_string) then parse and process it, overriding any
4406+
* previously processed conflicting keywords. Subsequent keywords will take
4407+
* precedence, however.
44074408
*/
44084409
static PQconninfoOption *
44094410
conninfo_array_parse(const char *const * keywords, const char *const * values,
@@ -4479,7 +4480,7 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
44794480
}
44804481

44814482
/*
4482-
* If we are on the dbname parameter, and we have a parsed
4483+
* If we are on the first dbname parameter, and we have a parsed
44834484
* connection string, copy those parameters across, overriding any
44844485
* existing previous settings.
44854486
*/
@@ -4513,6 +4514,12 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
45134514
}
45144515
}
45154516
}
4517+
/*
4518+
* Forget the parsed connection string, so that any subsequent
4519+
* dbname parameters will not be expanded.
4520+
*/
4521+
PQconninfoFree(dbname_options);
4522+
dbname_options = NULL;
45164523
}
45174524
else
45184525
{

0 commit comments

Comments
 (0)