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

Commit b61a5e6

Browse files
committed
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
1 parent 137b03b commit b61a5e6

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
516516
options ? "with options " : "", options ? options : "",
517517
(user && strlen(user) > 0) ? "for user " : "", user ? user : "");
518518

519+
/* count options (this may produce an overestimate, it's ok) */
519520
if (options)
520521
for (i = 0; options[i]; i++)
521-
/* count options */
522522
if (options[i] == '=')
523523
connect_params++;
524524

@@ -585,22 +585,34 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
585585
{
586586
char *str;
587587

588-
/* options look like this "option1 = value1 option2 = value2 ... */
589-
/* we have to break up the string into single options */
588+
/*
589+
* The options string contains "keyword=value" pairs separated by
590+
* '&'s. We must break this up into keywords and values to pass to
591+
* libpq (it's okay to scribble on the options string). We ignore
592+
* spaces just before each keyword or value.
593+
*/
590594
for (str = options; *str;)
591595
{
592596
int e,
593597
a;
594598
char *token1,
595599
*token2;
596600

597-
for (token1 = str; *token1 && *token1 == ' '; token1++);
598-
for (e = 0; token1[e] && token1[e] != '='; e++);
601+
/* Skip spaces before keyword */
602+
for (token1 = str; *token1 == ' '; token1++)
603+
/* skip */ ;
604+
/* Find end of keyword */
605+
for (e = 0; token1[e] && token1[e] != '='; e++)
606+
/* skip */ ;
599607
if (token1[e]) /* found "=" */
600608
{
601609
token1[e] = '\0';
602-
for (token2 = token1 + e + 1; *token2 && *token2 == ' '; token2++);
603-
for (a = 0; token2[a] && token2[a] != '&'; a++);
610+
/* Skip spaces before value */
611+
for (token2 = token1 + e + 1; *token2 == ' '; token2++)
612+
/* skip */ ;
613+
/* Find end of value */
614+
for (a = 0; token2[a] && token2[a] != '&'; a++)
615+
/* skip */ ;
604616
if (token2[a]) /* found "&" => another option follows */
605617
{
606618
token2[a] = '\0';
@@ -614,11 +626,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
614626
i++;
615627
}
616628
else
617-
/* the parser should not be able to create this invalid option */
629+
{
630+
/* Bogus options syntax ... ignore trailing garbage */
618631
str = token1 + e;
632+
}
619633
}
620-
621634
}
635+
636+
Assert(i <= connect_params);
622637
conn_keywords[i] = NULL; /* terminator */
623638

624639
this->connection = PQconnectdbParams(conn_keywords, conn_values, 0);

0 commit comments

Comments
 (0)