Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit add1b05

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 81d815d commit add1b05

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
@@ -4379,10 +4379,11 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
43794379
* Defaults are supplied (from a service file, environment variables, etc)
43804380
* for unspecified options, but only if use_defaults is TRUE.
43814381
*
4382-
* If expand_dbname is non-zero, and the value passed for keyword "dbname" is a
4383-
* connection string (as indicated by recognized_connection_string) then parse
4384-
* and process it, overriding any previously processed conflicting
4385-
* keywords. Subsequent keywords will take precedence, however.
4382+
* If expand_dbname is non-zero, and the value passed for the first occurrence
4383+
* of "dbname" keyword is a connection string (as indicated by
4384+
* recognized_connection_string) then parse and process it, overriding any
4385+
* previously processed conflicting keywords. Subsequent keywords will take
4386+
* precedence, however.
43864387
*/
43874388
static PQconninfoOption *
43884389
conninfo_array_parse(const char *const * keywords, const char *const * values,
@@ -4458,7 +4459,7 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
44584459
}
44594460

44604461
/*
4461-
* If we are on the dbname parameter, and we have a parsed
4462+
* If we are on the first dbname parameter, and we have a parsed
44624463
* connection string, copy those parameters across, overriding any
44634464
* existing previous settings.
44644465
*/
@@ -4492,6 +4493,12 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
44924493
}
44934494
}
44944495
}
4496+
/*
4497+
* Forget the parsed connection string, so that any subsequent
4498+
* dbname parameters will not be expanded.
4499+
*/
4500+
PQconninfoFree(dbname_options);
4501+
dbname_options = NULL;
44954502
}
44964503
else
44974504
{

0 commit comments

Comments
 (0)