Allow "dbname" from connection string to be overridden in PQconnectDBParams
authorHeikki Linnakangas
Tue, 25 Nov 2014 15:12:07 +0000 (17:12 +0200)
committerHeikki Linnakangas
Tue, 25 Nov 2014 15:39:00 +0000 (17:39 +0200)
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.

doc/src/sgml/libpq.sgml
src/interfaces/libpq/fe-connect.c

index 490bb14f46a25ee2abf0d64621f4cea592f6fa53..30a236d3b741b0d9459b66dbbf94c0606e6e562c 100644 (file)
@@ -124,7 +124,10 @@ PGconn *PQconnectdbParams(const char * const *keywords,
       
        When expand_dbname is non-zero, the
        dbname key word value is allowed to be recognized
-       as a connection string. More details on the possible formats appear in
+       as a connection string. Only the first occurrence of
+       dbname is expanded this way, any subsequent
+       dbname value is processed as plain database name. More
+       details on the possible connection string formats appear in
        .
       
 
index adeec6abbf79be603581e331fb4d6f991d7b8e77..daa95f32b8017096798b07999e9ddb5ca1327141 100644 (file)
@@ -4393,10 +4393,11 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
  * Defaults are supplied (from a service file, environment variables, etc)
  * for unspecified options, but only if use_defaults is TRUE.
  *
- * If expand_dbname is non-zero, and the value passed for keyword "dbname" is a
- * connection string (as indicated by recognized_connection_string) then parse
- * and process it, overriding any previously processed conflicting
- * keywords. Subsequent keywords will take precedence, however.
+ * If expand_dbname is non-zero, and the value passed for the first occurrence
+ * of "dbname" keyword is a connection string (as indicated by
+ * recognized_connection_string) then parse and process it, overriding any
+ * previously processed conflicting keywords. Subsequent keywords will take
+ * precedence, however.
  */
 static PQconninfoOption *
 conninfo_array_parse(const char *const * keywords, const char *const * values,
@@ -4472,7 +4473,7 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
            }
 
            /*
-            * If we are on the dbname parameter, and we have a parsed
+            * If we are on the first dbname parameter, and we have a parsed
             * connection string, copy those parameters across, overriding any
             * existing previous settings.
             */
@@ -4506,6 +4507,12 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
                        }
                    }
                }
+               /*
+                * Forget the parsed connection string, so that any subsequent
+                * dbname parameters will not be expanded.
+                */
+               PQconninfoFree(dbname_options);
+               dbname_options = NULL;
            }
            else
            {