Commit b61a5e6a authored by Tom Lane's avatar Tom Lane

Cosmetic improvements for options-handling code in ECPGconnect().

The comment describing the string format was a lie.  Make it agree with
reality, add/improve some other comments, fix coding style for loops with
empty bodies.  Also add an Assert that we counted parameters correctly,
because the spread-out logic for that looks pretty fragile.

No actual bugs fixed here, so no need to back-patch.

Discussion: https://postgr.es/m/848B1649C8A6274AA527C4472CA11EDD5FC70CBE@G01JPEXMBYT02
parent 137b03b8
...@@ -516,9 +516,9 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p ...@@ -516,9 +516,9 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
options ? "with options " : "", options ? options : "", options ? "with options " : "", options ? options : "",
(user && strlen(user) > 0) ? "for user " : "", user ? user : ""); (user && strlen(user) > 0) ? "for user " : "", user ? user : "");
/* count options (this may produce an overestimate, it's ok) */
if (options) if (options)
for (i = 0; options[i]; i++) for (i = 0; options[i]; i++)
/* count options */
if (options[i] == '=') if (options[i] == '=')
connect_params++; connect_params++;
...@@ -585,8 +585,12 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p ...@@ -585,8 +585,12 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
{ {
char *str; char *str;
/* options look like this "option1 = value1 option2 = value2 ... */ /*
/* we have to break up the string into single options */ * The options string contains "keyword=value" pairs separated by
* '&'s. We must break this up into keywords and values to pass to
* libpq (it's okay to scribble on the options string). We ignore
* spaces just before each keyword or value.
*/
for (str = options; *str;) for (str = options; *str;)
{ {
int e, int e,
...@@ -594,13 +598,21 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p ...@@ -594,13 +598,21 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
char *token1, char *token1,
*token2; *token2;
for (token1 = str; *token1 && *token1 == ' '; token1++); /* Skip spaces before keyword */
for (e = 0; token1[e] && token1[e] != '='; e++); for (token1 = str; *token1 == ' '; token1++)
/* skip */ ;
/* Find end of keyword */
for (e = 0; token1[e] && token1[e] != '='; e++)
/* skip */ ;
if (token1[e]) /* found "=" */ if (token1[e]) /* found "=" */
{ {
token1[e] = '\0'; token1[e] = '\0';
for (token2 = token1 + e + 1; *token2 && *token2 == ' '; token2++); /* Skip spaces before value */
for (a = 0; token2[a] && token2[a] != '&'; a++); for (token2 = token1 + e + 1; *token2 == ' '; token2++)
/* skip */ ;
/* Find end of value */
for (a = 0; token2[a] && token2[a] != '&'; a++)
/* skip */ ;
if (token2[a]) /* found "&" => another option follows */ if (token2[a]) /* found "&" => another option follows */
{ {
token2[a] = '\0'; token2[a] = '\0';
...@@ -614,11 +626,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p ...@@ -614,11 +626,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
i++; i++;
} }
else else
/* the parser should not be able to create this invalid option */ {
/* Bogus options syntax ... ignore trailing garbage */
str = token1 + e; str = token1 + e;
}
} }
} }
Assert(i <= connect_params);
conn_keywords[i] = NULL; /* terminator */ conn_keywords[i] = NULL; /* terminator */
this->connection = PQconnectdbParams(conn_keywords, conn_values, 0); this->connection = PQconnectdbParams(conn_keywords, conn_values, 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment