Skip to content

Commit 5e7710e

Browse files
author
Michael Meskes
committed
Make sure all connection paramters are used in call to PQconnectdbParams.
1 parent 08fd6ff commit 5e7710e

File tree

5 files changed

+98
-21
lines changed

5 files changed

+98
-21
lines changed

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,15 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
267267
struct sqlca_t *sqlca = ECPGget_sqlca();
268268
enum COMPAT_MODE compat = c;
269269
struct connection *this;
270-
int i;
270+
int i, connect_params = 0;
271271
char *dbname = name ? ecpg_strdup(name, lineno) : NULL,
272272
*host = NULL,
273273
*tmp,
274274
*port = NULL,
275275
*realname = NULL,
276276
*options = NULL;
277-
const char *conn_keywords[7];
278-
const char *conn_values[6];
277+
const char **conn_keywords;
278+
const char **conn_values;
279279

280280
ecpg_init_sqlca(sqlca);
281281

@@ -359,7 +359,10 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
359359
if (tmp != NULL) /* database name given */
360360
{
361361
if (tmp[1] != '\0') /* non-empty database name */
362+
{
362363
realname = ecpg_strdup(tmp + 1, lineno);
364+
connect_params++;
365+
}
363366
*tmp = '\0';
364367
}
365368

@@ -373,6 +376,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
373376
{
374377
*tmp2 = '\0';
375378
host = ecpg_strdup(tmp + 1, lineno);
379+
connect_params++;
376380
if (strncmp(dbname, "unix:", 5) != 0)
377381
{
378382
ecpg_log("ECPGconnect: socketname %s given for TCP connection on line %d\n", host, lineno);
@@ -394,7 +398,10 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
394398
}
395399
}
396400
else
401+
{
397402
port = ecpg_strdup(tmp + 1, lineno);
403+
connect_params++;
404+
}
398405
}
399406

400407
if (strncmp(dbname, "unix:", 5) == 0)
@@ -418,7 +425,10 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
418425
}
419426
}
420427
else
428+
{
421429
host = ecpg_strdup(dbname + offset, lineno);
430+
connect_params++;
431+
}
422432

423433
}
424434
}
@@ -429,17 +439,25 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
429439
if (tmp != NULL) /* port number given */
430440
{
431441
port = ecpg_strdup(tmp + 1, lineno);
442+
connect_params++;
432443
*tmp = '\0';
433444
}
434445

435446
tmp = strrchr(dbname, '@');
436447
if (tmp != NULL) /* host name given */
437448
{
438449
host = ecpg_strdup(tmp + 1, lineno);
450+
connect_params++;
439451
*tmp = '\0';
440452
}
441453

442-
realname = (strlen(dbname) > 0) ? ecpg_strdup(dbname, lineno) : NULL;
454+
if (strlen(dbname) > 0)
455+
{
456+
realname = ecpg_strdup(dbname, lineno);
457+
connect_params++;
458+
}
459+
else
460+
realname = NULL;
443461
}
444462
}
445463
else
@@ -475,10 +493,35 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
475493
options ? "with options " : "", options ? options : "",
476494
(user && strlen(user) > 0) ? "for user " : "", user ? user : "");
477495

478-
if (options) /* replace '&' if there are any */
496+
if (options)
479497
for (i = 0; options[i]; i++)
480-
if (options[i] == '&')
481-
options[i] = ' ';
498+
/* count options */
499+
if (options[i] == '=')
500+
connect_params++;
501+
502+
if (user && strlen(user) > 0)
503+
connect_params++;
504+
if (passwd && strlen(passwd) > 0)
505+
connect_params++;
506+
507+
/* allocate enough space for all connection parameters */
508+
conn_keywords = (const char **) ecpg_alloc((connect_params + 1) * sizeof (char *), lineno);
509+
conn_values = (const char **) ecpg_alloc(connect_params * sizeof (char *), lineno);
510+
if (conn_keywords == NULL || conn_values == NULL)
511+
{
512+
if (host)
513+
ecpg_free(host);
514+
if (port)
515+
ecpg_free(port);
516+
if (options)
517+
ecpg_free(options);
518+
if (realname)
519+
ecpg_free(realname);
520+
if (dbname)
521+
ecpg_free(dbname);
522+
free(this);
523+
return false;
524+
}
482525

483526
i = 0;
484527
if (realname)
@@ -513,9 +556,27 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
513556
}
514557
if (options)
515558
{
516-
conn_keywords[i] = "options";
517-
conn_values[i] = options;
518-
i++;
559+
char *saveptr, *token1, *token2, *str;
560+
561+
/* options look like this "option1 = value1 option2 = value2 ... */
562+
/* we have to break up the string into single options */
563+
for (str = options; ; str = NULL)
564+
{
565+
token1 = strtok_r(str, "=", &saveptr);
566+
if (token1 == NULL)
567+
break;
568+
/* strip leading blanks */
569+
for (; *token1 && *token1 == ' '; token1++);
570+
571+
token2 = strtok_r(NULL, "&", &saveptr);
572+
if (token2 == NULL)
573+
break;
574+
575+
conn_keywords[i] = token1;
576+
conn_values[i] = token2;
577+
i++;
578+
}
579+
519580
}
520581
conn_keywords[i] = NULL; /* terminator */
521582

@@ -529,6 +590,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
529590
ecpg_free(options);
530591
if (dbname)
531592
ecpg_free(dbname);
593+
ecpg_free(conn_values);
594+
ecpg_free(conn_keywords);
532595

533596
if (PQstatus(this->connection) == CONNECTION_BAD)
534597
{

src/interfaces/ecpg/test/connect/test5.pgc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ exec sql end declare section;
5353
exec sql connect to 'unix:postgresql://localhost/connectdb' as main user :user;
5454
exec sql disconnect main;
5555

56+
exec sql connect to unix:postgresql://localhost/connectdb?connect_timeout=14&client_encoding=latin1 as main user connectuser;
57+
exec sql disconnect main;
58+
5659
exec sql connect to "unix:postgresql://200.46.204.71/connectdb" as main user connectuser;
5760
exec sql disconnect main;
5861

src/interfaces/ecpg/test/expected/connect-test1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
[NO_PID]: ECPGconnect: opening database connectdb on localhost port <REGRESSION_PORT> for user connectuser
5353
[NO_PID]: sqlca: code: 0, state: 00000
5454
[NO_PID]: ECPGconnect: could not open database: could not connect to server: Connection refused
55-
Is the server running on host "localhost" and accepting
55+
Is the server running on host "localhost" (127.0.0.1) and accepting
5656
TCP/IP connections on port 20?
5757

5858
[NO_PID]: sqlca: code: 0, state: 00000

src/interfaces/ecpg/test/expected/connect-test5.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,34 +115,41 @@ main(void)
115115
#line 54 "test5.pgc"
116116

117117

118-
{ ECPGconnect(__LINE__, 0, "unix:postgresql://200.46.204.71/connectdb" , "connectuser" , NULL , "main", 0); }
118+
{ ECPGconnect(__LINE__, 0, "unix:postgresql://localhost/connectdb?connect_timeout=14 & client_encoding=latin1" , "connectuser" , NULL , "main", 0); }
119119
#line 56 "test5.pgc"
120120

121121
{ ECPGdisconnect(__LINE__, "main");}
122122
#line 57 "test5.pgc"
123123

124124

125-
{ ECPGconnect(__LINE__, 0, "unix:postgresql://localhost/" , "connectdb" , NULL , "main", 0); }
125+
{ ECPGconnect(__LINE__, 0, "unix:postgresql://200.46.204.71/connectdb" , "connectuser" , NULL , "main", 0); }
126126
#line 59 "test5.pgc"
127127

128128
{ ECPGdisconnect(__LINE__, "main");}
129129
#line 60 "test5.pgc"
130130

131131

132+
{ ECPGconnect(__LINE__, 0, "unix:postgresql://localhost/" , "connectdb" , NULL , "main", 0); }
133+
#line 62 "test5.pgc"
134+
135+
{ ECPGdisconnect(__LINE__, "main");}
136+
#line 63 "test5.pgc"
137+
138+
132139
/* connect twice */
133140
{ ECPGconnect(__LINE__, 0, "connectdb" , NULL, NULL , "main", 0); }
134-
#line 63 "test5.pgc"
141+
#line 66 "test5.pgc"
135142

136143
{ ECPGconnect(__LINE__, 0, "connectdb" , NULL, NULL , "main", 0); }
137-
#line 64 "test5.pgc"
144+
#line 67 "test5.pgc"
138145

139146
{ ECPGdisconnect(__LINE__, "main");}
140-
#line 65 "test5.pgc"
147+
#line 68 "test5.pgc"
141148

142149

143150
/* not connected */
144151
{ ECPGdisconnect(__LINE__, "nonexistant");}
145-
#line 68 "test5.pgc"
152+
#line 71 "test5.pgc"
146153

147154

148155
return (0);

src/interfaces/ecpg/test/expected/connect-test5.stderr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@
4646
[NO_PID]: sqlca: code: 0, state: 00000
4747
[NO_PID]: ecpg_finish: connection main closed
4848
[NO_PID]: sqlca: code: 0, state: 00000
49-
[NO_PID]: ECPGconnect: non-localhost access via sockets on line 56
49+
[NO_PID]: ECPGconnect: opening database connectdb on <DEFAULT> port <DEFAULT> with options connect_timeout=14 & client_encoding=latin1 for user connectuser
5050
[NO_PID]: sqlca: code: 0, state: 00000
51-
[NO_PID]: raising sqlcode -402 on line 56: could not connect to database "connectdb" on line 56
51+
[NO_PID]: ecpg_finish: connection main closed
52+
[NO_PID]: sqlca: code: 0, state: 00000
53+
[NO_PID]: ECPGconnect: non-localhost access via sockets on line 59
54+
[NO_PID]: sqlca: code: 0, state: 00000
55+
[NO_PID]: raising sqlcode -402 on line 59: could not connect to database "connectdb" on line 59
5256
[NO_PID]: sqlca: code: -402, state: 08001
53-
[NO_PID]: raising sqlcode -220 on line 57: connection "main" does not exist on line 57
57+
[NO_PID]: raising sqlcode -220 on line 60: connection "main" does not exist on line 60
5458
[NO_PID]: sqlca: code: -220, state: 08003
5559
[NO_PID]: ECPGconnect: opening database <DEFAULT> on <DEFAULT> port <DEFAULT> for user connectdb
5660
[NO_PID]: sqlca: code: 0, state: 00000
@@ -62,5 +66,5 @@
6266
[NO_PID]: sqlca: code: 0, state: 00000
6367
[NO_PID]: ecpg_finish: connection main closed
6468
[NO_PID]: sqlca: code: 0, state: 00000
65-
[NO_PID]: raising sqlcode -220 on line 68: connection "nonexistant" does not exist on line 68
69+
[NO_PID]: raising sqlcode -220 on line 71: connection "nonexistant" does not exist on line 71
6670
[NO_PID]: sqlca: code: -220, state: 08003

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy