@@ -121,9 +121,9 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
121
121
* fallback is available. If after all no value can be determined
122
122
* for an option, an error is returned.
123
123
*
124
- * The value for the username is treated specially in conninfo_parse .
125
- * If the Compiled-in resource is specified as a NULL value , the
126
- * user is determined by pg_fe_getauthname().
124
+ * The value for the username is treated specially in conninfo_add_defaults .
125
+ * If the value is not obtained any other way , the username is determined
126
+ * by pg_fe_getauthname().
127
127
*
128
128
* The Label and Disp-Char entries are provided for applications that
129
129
* want to use PQconndefaults() to create a generic database connection
@@ -292,11 +292,14 @@ static PGconn *makeEmptyPGconn(void);
292
292
static void fillPGconn (PGconn * conn , PQconninfoOption * connOptions );
293
293
static void freePGconn (PGconn * conn );
294
294
static void closePGconn (PGconn * conn );
295
+ static PQconninfoOption * conninfo_init (PQExpBuffer errorMessage );
295
296
static PQconninfoOption * conninfo_parse (const char * conninfo ,
296
297
PQExpBuffer errorMessage , bool use_defaults );
297
298
static PQconninfoOption * conninfo_array_parse (const char * const * keywords ,
298
299
const char * const * values , PQExpBuffer errorMessage ,
299
300
bool use_defaults , int expand_dbname );
301
+ static bool conninfo_add_defaults (PQconninfoOption * options ,
302
+ PQExpBuffer errorMessage );
300
303
static char * conninfo_getval (PQconninfoOption * connOptions ,
301
304
const char * keyword );
302
305
static void defaultNoticeReceiver (void * arg , const PGresult * res );
@@ -813,10 +816,9 @@ connectOptions2(PGconn *conn)
813
816
/*
814
817
* PQconndefaults
815
818
*
816
- * Parse an empty string like PQconnectdb() would do and return the
817
- * resulting connection options array, ie, all the default values that are
818
- * available from the environment etc. On error (eg out of memory),
819
- * NULL is returned.
819
+ * Construct a default connection options array, which identifies all the
820
+ * available options and shows any default values that are available from the
821
+ * environment etc. On error (eg out of memory), NULL is returned.
820
822
*
821
823
* Using this function, an application may determine all possible options
822
824
* and their current default values.
@@ -833,10 +835,21 @@ PQconndefaults(void)
833
835
PQExpBufferData errorBuf ;
834
836
PQconninfoOption * connOptions ;
835
837
838
+ /* We don't actually report any errors here, but callees want a buffer */
836
839
initPQExpBuffer (& errorBuf );
837
840
if (PQExpBufferDataBroken (errorBuf ))
838
841
return NULL ; /* out of memory already :-( */
839
- connOptions = conninfo_parse ("" , & errorBuf , true);
842
+
843
+ connOptions = conninfo_init (& errorBuf );
844
+ if (connOptions != NULL )
845
+ {
846
+ if (!conninfo_add_defaults (connOptions , & errorBuf ))
847
+ {
848
+ PQconninfoFree (connOptions );
849
+ connOptions = NULL ;
850
+ }
851
+ }
852
+
840
853
termPQExpBuffer (& errorBuf );
841
854
return connOptions ;
842
855
}
@@ -3986,6 +3999,25 @@ PQconninfoParse(const char *conninfo, char **errmsg)
3986
3999
return connOptions ;
3987
4000
}
3988
4001
4002
+ /*
4003
+ * Build a working copy of the constant PQconninfoOptions array.
4004
+ */
4005
+ static PQconninfoOption *
4006
+ conninfo_init (PQExpBuffer errorMessage )
4007
+ {
4008
+ PQconninfoOption * options ;
4009
+
4010
+ options = (PQconninfoOption * ) malloc (sizeof (PQconninfoOptions ));
4011
+ if (options == NULL )
4012
+ {
4013
+ printfPQExpBuffer (errorMessage ,
4014
+ libpq_gettext ("out of memory\n" ));
4015
+ return NULL ;
4016
+ }
4017
+ memcpy (options , PQconninfoOptions , sizeof (PQconninfoOptions ));
4018
+ return options ;
4019
+ }
4020
+
3989
4021
/*
3990
4022
* Conninfo parser routine
3991
4023
*
@@ -4002,21 +4034,15 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
4002
4034
char * pname ;
4003
4035
char * pval ;
4004
4036
char * buf ;
4005
- char * tmp ;
4006
4037
char * cp ;
4007
4038
char * cp2 ;
4008
4039
PQconninfoOption * options ;
4009
4040
PQconninfoOption * option ;
4010
4041
4011
4042
/* Make a working copy of PQconninfoOptions */
4012
- options = malloc ( sizeof ( PQconninfoOptions ) );
4043
+ options = conninfo_init ( errorMessage );
4013
4044
if (options == NULL )
4014
- {
4015
- printfPQExpBuffer (errorMessage ,
4016
- libpq_gettext ("out of memory\n" ));
4017
4045
return NULL ;
4018
- }
4019
- memcpy (options , PQconninfoOptions , sizeof (PQconninfoOptions ));
4020
4046
4021
4047
/* Need a modifiable copy of the input string */
4022
4048
if ((buf = strdup (conninfo )) == NULL )
@@ -4170,73 +4196,14 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
4170
4196
free (buf );
4171
4197
4172
4198
/*
4173
- * Stop here if caller doesn't want defaults filled in.
4174
- */
4175
- if (!use_defaults )
4176
- return options ;
4177
-
4178
- /*
4179
- * If there's a service spec, use it to obtain any not-explicitly-given
4180
- * parameters.
4181
- */
4182
- if (parseServiceInfo (options , errorMessage ))
4183
- {
4184
- PQconninfoFree (options );
4185
- return NULL ;
4186
- }
4187
-
4188
- /*
4189
- * Get the fallback resources for parameters not specified in the conninfo
4190
- * string nor the service.
4199
+ * Add in defaults if the caller wants that.
4191
4200
*/
4192
- for ( option = options ; option -> keyword != NULL ; option ++ )
4201
+ if ( use_defaults )
4193
4202
{
4194
- if (option -> val != NULL )
4195
- continue ; /* Value was in conninfo or service */
4196
-
4197
- /*
4198
- * Try to get the environment variable fallback
4199
- */
4200
- if (option -> envvar != NULL )
4203
+ if (!conninfo_add_defaults (options , errorMessage ))
4201
4204
{
4202
- if ((tmp = getenv (option -> envvar )) != NULL )
4203
- {
4204
- option -> val = strdup (tmp );
4205
- if (!option -> val )
4206
- {
4207
- printfPQExpBuffer (errorMessage ,
4208
- libpq_gettext ("out of memory\n" ));
4209
- PQconninfoFree (options );
4210
- return NULL ;
4211
- }
4212
- continue ;
4213
- }
4214
- }
4215
-
4216
- /*
4217
- * No environment variable specified or this one isn't set - try
4218
- * compiled in
4219
- */
4220
- if (option -> compiled != NULL )
4221
- {
4222
- option -> val = strdup (option -> compiled );
4223
- if (!option -> val )
4224
- {
4225
- printfPQExpBuffer (errorMessage ,
4226
- libpq_gettext ("out of memory\n" ));
4227
- PQconninfoFree (options );
4228
- return NULL ;
4229
- }
4230
- continue ;
4231
- }
4232
-
4233
- /*
4234
- * Special handling for user
4235
- */
4236
- if (strcmp (option -> keyword , "user" ) == 0 )
4237
- {
4238
- option -> val = pg_fe_getauthname (errorMessage );
4239
- continue ;
4205
+ PQconninfoFree (options );
4206
+ return NULL ;
4240
4207
}
4241
4208
}
4242
4209
@@ -4262,7 +4229,6 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
4262
4229
PQExpBuffer errorMessage , bool use_defaults ,
4263
4230
int expand_dbname )
4264
4231
{
4265
- char * tmp ;
4266
4232
PQconninfoOption * options ;
4267
4233
PQconninfoOption * str_options = NULL ;
4268
4234
PQconninfoOption * option ;
@@ -4298,18 +4264,15 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
4298
4264
}
4299
4265
4300
4266
/* Make a working copy of PQconninfoOptions */
4301
- options = malloc ( sizeof ( PQconninfoOptions ) );
4267
+ options = conninfo_init ( errorMessage );
4302
4268
if (options == NULL )
4303
4269
{
4304
- printfPQExpBuffer (errorMessage ,
4305
- libpq_gettext ("out of memory\n" ));
4306
4270
PQconninfoFree (str_options );
4307
4271
return NULL ;
4308
4272
}
4309
- memcpy (options , PQconninfoOptions , sizeof (PQconninfoOptions ));
4310
4273
4311
- i = 0 ;
4312
4274
/* Parse the keywords/values arrays */
4275
+ i = 0 ;
4313
4276
while (keywords [i ])
4314
4277
{
4315
4278
const char * pname = keywords [i ];
@@ -4386,20 +4349,42 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
4386
4349
PQconninfoFree (str_options );
4387
4350
4388
4351
/*
4389
- * Stop here if caller doesn't want defaults filled in .
4352
+ * Add in defaults if the caller wants that .
4390
4353
*/
4391
- if (!use_defaults )
4392
- return options ;
4354
+ if (use_defaults )
4355
+ {
4356
+ if (!conninfo_add_defaults (options , errorMessage ))
4357
+ {
4358
+ PQconninfoFree (options );
4359
+ return NULL ;
4360
+ }
4361
+ }
4362
+
4363
+ return options ;
4364
+ }
4365
+
4366
+ /*
4367
+ * Add the default values for any unspecified options to the connection
4368
+ * options array.
4369
+ *
4370
+ * Defaults are obtained from a service file, environment variables, etc.
4371
+ *
4372
+ * Returns TRUE if successful, otherwise FALSE; errorMessage is filled in
4373
+ * upon failure. Note that failure to locate a default value is not an
4374
+ * error condition here --- we just leave the option's value as NULL.
4375
+ */
4376
+ static bool
4377
+ conninfo_add_defaults (PQconninfoOption * options , PQExpBuffer errorMessage )
4378
+ {
4379
+ PQconninfoOption * option ;
4380
+ char * tmp ;
4393
4381
4394
4382
/*
4395
4383
* If there's a service spec, use it to obtain any not-explicitly-given
4396
4384
* parameters.
4397
4385
*/
4398
- if (parseServiceInfo (options , errorMessage ))
4399
- {
4400
- PQconninfoFree (options );
4401
- return NULL ;
4402
- }
4386
+ if (parseServiceInfo (options , errorMessage ) != 0 )
4387
+ return false;
4403
4388
4404
4389
/*
4405
4390
* Get the fallback resources for parameters not specified in the conninfo
@@ -4422,16 +4407,15 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
4422
4407
{
4423
4408
printfPQExpBuffer (errorMessage ,
4424
4409
libpq_gettext ("out of memory\n" ));
4425
- PQconninfoFree (options );
4426
- return NULL ;
4410
+ return false;
4427
4411
}
4428
4412
continue ;
4429
4413
}
4430
4414
}
4431
4415
4432
4416
/*
4433
- * No environment variable specified or this one isn't set - try
4434
- * compiled in
4417
+ * No environment variable specified or the variable isn't set - try
4418
+ * compiled-in default
4435
4419
*/
4436
4420
if (option -> compiled != NULL )
4437
4421
{
@@ -4440,14 +4424,13 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
4440
4424
{
4441
4425
printfPQExpBuffer (errorMessage ,
4442
4426
libpq_gettext ("out of memory\n" ));
4443
- PQconninfoFree (options );
4444
- return NULL ;
4427
+ return false;
4445
4428
}
4446
4429
continue ;
4447
4430
}
4448
4431
4449
4432
/*
4450
- * Special handling for user
4433
+ * Special handling for " user" option
4451
4434
*/
4452
4435
if (strcmp (option -> keyword , "user" ) == 0 )
4453
4436
{
@@ -4456,7 +4439,7 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
4456
4439
}
4457
4440
}
4458
4441
4459
- return options ;
4442
+ return true ;
4460
4443
}
4461
4444
4462
4445
static char *
0 commit comments