@@ -280,7 +280,7 @@ static bool connectOptions2(PGconn *conn);
280
280
static int connectDBStart (PGconn * conn );
281
281
static int connectDBComplete (PGconn * conn );
282
282
static PGconn * makeEmptyPGconn (void );
283
- static void fillPGconn (PGconn * conn ,PQconninfoOption * connOptions );
283
+ static bool fillPGconn (PGconn * conn ,PQconninfoOption * connOptions );
284
284
static void freePGconn (PGconn * conn );
285
285
static void closePGconn (PGconn * conn );
286
286
static PQconninfoOption * conninfo_parse (const char * conninfo ,
@@ -452,7 +452,11 @@ PQconnectStartParams(const char **keywords,
452
452
/*
453
453
* Move option values into conn structure
454
454
*/
455
- fillPGconn (conn ,connOptions );
455
+ if (!fillPGconn (conn ,connOptions ))
456
+ {
457
+ PQconninfoFree (connOptions );
458
+ return conn ;
459
+ }
456
460
457
461
/*
458
462
* Free the option info - all is in conn now
@@ -532,59 +536,53 @@ PQconnectStart(const char *conninfo)
532
536
return conn ;
533
537
}
534
538
535
- static void
539
+ /*
540
+ * Move option values into conn structure
541
+ *
542
+ * Don't put anything cute here --- intelligence should be in
543
+ * connectOptions2 ...
544
+ *
545
+ * Returns true on success. On failure, returns false and sets error message.
546
+ */
547
+ #define FILL_CONN_OPTION (dst ,name ) \
548
+ do \
549
+ { \
550
+ tmp = conninfo_getval(connOptions, name); \
551
+ if (tmp) \
552
+ { \
553
+ dst = strdup(tmp); \
554
+ if (dst == NULL) \
555
+ goto oom_error; \
556
+ } \
557
+ else \
558
+ dst = NULL; \
559
+ } while(0)
560
+
561
+ static bool
536
562
fillPGconn (PGconn * conn ,PQconninfoOption * connOptions )
537
563
{
538
564
char * tmp ;
539
565
540
- /*
541
- * Move option values into conn structure
542
- *
543
- * Don't put anything cute here --- intelligence should be in
544
- * connectOptions2 ...
545
- *
546
- * XXX: probably worth checking strdup() return value here...
547
- */
548
- tmp = conninfo_getval (connOptions ,"hostaddr" );
549
- conn -> pghostaddr = tmp ?strdup (tmp ) :NULL ;
550
- tmp = conninfo_getval (connOptions ,"host" );
551
- conn -> pghost = tmp ?strdup (tmp ) :NULL ;
552
- tmp = conninfo_getval (connOptions ,"port" );
553
- conn -> pgport = tmp ?strdup (tmp ) :NULL ;
554
- tmp = conninfo_getval (connOptions ,"tty" );
555
- conn -> pgtty = tmp ?strdup (tmp ) :NULL ;
556
- tmp = conninfo_getval (connOptions ,"options" );
557
- conn -> pgoptions = tmp ?strdup (tmp ) :NULL ;
558
- tmp = conninfo_getval (connOptions ,"application_name" );
559
- conn -> appname = tmp ?strdup (tmp ) :NULL ;
560
- tmp = conninfo_getval (connOptions ,"fallback_application_name" );
561
- conn -> fbappname = tmp ?strdup (tmp ) :NULL ;
562
- tmp = conninfo_getval (connOptions ,"dbname" );
563
- conn -> dbName = tmp ?strdup (tmp ) :NULL ;
564
- tmp = conninfo_getval (connOptions ,"user" );
565
- conn -> pguser = tmp ?strdup (tmp ) :NULL ;
566
- tmp = conninfo_getval (connOptions ,"password" );
567
- conn -> pgpass = tmp ?strdup (tmp ) :NULL ;
568
- tmp = conninfo_getval (connOptions ,"connect_timeout" );
569
- conn -> connect_timeout = tmp ?strdup (tmp ) :NULL ;
570
- tmp = conninfo_getval (connOptions ,"keepalives" );
571
- conn -> keepalives = tmp ?strdup (tmp ) :NULL ;
572
- tmp = conninfo_getval (connOptions ,"keepalives_idle" );
573
- conn -> keepalives_idle = tmp ?strdup (tmp ) :NULL ;
574
- tmp = conninfo_getval (connOptions ,"keepalives_interval" );
575
- conn -> keepalives_interval = tmp ?strdup (tmp ) :NULL ;
576
- tmp = conninfo_getval (connOptions ,"keepalives_count" );
577
- conn -> keepalives_count = tmp ?strdup (tmp ) :NULL ;
578
- tmp = conninfo_getval (connOptions ,"sslmode" );
579
- conn -> sslmode = tmp ?strdup (tmp ) :NULL ;
580
- tmp = conninfo_getval (connOptions ,"sslkey" );
581
- conn -> sslkey = tmp ?strdup (tmp ) :NULL ;
582
- tmp = conninfo_getval (connOptions ,"sslcert" );
583
- conn -> sslcert = tmp ?strdup (tmp ) :NULL ;
584
- tmp = conninfo_getval (connOptions ,"sslrootcert" );
585
- conn -> sslrootcert = tmp ?strdup (tmp ) :NULL ;
586
- tmp = conninfo_getval (connOptions ,"sslcrl" );
587
- conn -> sslcrl = tmp ?strdup (tmp ) :NULL ;
566
+ FILL_CONN_OPTION (conn -> pghostaddr ,"hostaddr" );
567
+ FILL_CONN_OPTION (conn -> pghost ,"host" );
568
+ FILL_CONN_OPTION (conn -> pgport ,"port" );
569
+ FILL_CONN_OPTION (conn -> pgtty ,"tty" );
570
+ FILL_CONN_OPTION (conn -> pgoptions ,"options" );
571
+ FILL_CONN_OPTION (conn -> appname ,"application_name" );
572
+ FILL_CONN_OPTION (conn -> fbappname ,"fallback_application_name" );
573
+ FILL_CONN_OPTION (conn -> dbName ,"dbname" );
574
+ FILL_CONN_OPTION (conn -> pguser ,"user" );
575
+ FILL_CONN_OPTION (conn -> pgpass ,"password" );
576
+ FILL_CONN_OPTION (conn -> connect_timeout ,"connect_timeout" );
577
+ FILL_CONN_OPTION (conn -> keepalives ,"keepalives" );
578
+ FILL_CONN_OPTION (conn -> keepalives_idle ,"keepalives_idle" );
579
+ FILL_CONN_OPTION (conn -> keepalives_interval ,"keepalives_interval" );
580
+ FILL_CONN_OPTION (conn -> keepalives_count ,"keepalives_count" );
581
+ FILL_CONN_OPTION (conn -> sslmode ,"sslmode" );
582
+ FILL_CONN_OPTION (conn -> sslkey ,"sslkey" );
583
+ FILL_CONN_OPTION (conn -> sslcert ,"sslcert" );
584
+ FILL_CONN_OPTION (conn -> sslrootcert ,"sslrootcert" );
585
+ FILL_CONN_OPTION (conn -> sslcrl ,"sslcrl" );
588
586
#ifdef USE_SSL
589
587
tmp = conninfo_getval (connOptions ,"requiressl" );
590
588
if (tmp && tmp [0 ]== '1' )
@@ -593,18 +591,24 @@ fillPGconn(PGconn *conn, PQconninfoOption *connOptions)
593
591
if (conn -> sslmode )
594
592
free (conn -> sslmode );
595
593
conn -> sslmode = strdup ("require" );
594
+ if (!conn -> sslmode )
595
+ return false;
596
596
}
597
597
#endif
598
598
#if defined(KRB5 )|| defined(ENABLE_GSS )|| defined(ENABLE_SSPI )
599
- tmp = conninfo_getval (connOptions ,"krbsrvname" );
600
- conn -> krbsrvname = tmp ?strdup (tmp ) :NULL ;
599
+ FILL_CONN_OPTION (conn -> krbsrvname ,"krbsrvname" );
601
600
#endif
602
601
#if defined(ENABLE_GSS )&& defined(ENABLE_SSPI )
603
- tmp = conninfo_getval (connOptions ,"gsslib" );
604
- conn -> gsslib = tmp ?strdup (tmp ) :NULL ;
602
+ FILL_CONN_OPTION (conn -> gsslib ,"gsslib" );
605
603
#endif
606
- tmp = conninfo_getval (connOptions ,"replication" );
607
- conn -> replication = tmp ?strdup (tmp ) :NULL ;
604
+ FILL_CONN_OPTION (conn -> replication ,"replication" );
605
+
606
+ return true;
607
+
608
+ oom_error :
609
+ printfPQExpBuffer (& conn -> errorMessage ,
610
+ libpq_gettext ("out of memory\n" ));
611
+ return false;
608
612
}
609
613
610
614
/*
@@ -637,7 +641,12 @@ connectOptions1(PGconn *conn, const char *conninfo)
637
641
/*
638
642
* Move option values into conn structure
639
643
*/
640
- fillPGconn (conn ,connOptions );
644
+ if (!fillPGconn (conn ,connOptions ))
645
+ {
646
+ conn -> status = CONNECTION_BAD ;
647
+ PQconninfoFree (connOptions );
648
+ return false;
649
+ }
641
650
642
651
/*
643
652
* Free the option info - all is in conn now
@@ -667,6 +676,8 @@ connectOptions2(PGconn *conn)
667
676
if (conn -> dbName )
668
677
free (conn -> dbName );
669
678
conn -> dbName = strdup (conn -> pguser );
679
+ if (!conn -> dbName )
680
+ gotooom_error ;
670
681
}
671
682
672
683
/*
@@ -679,7 +690,12 @@ connectOptions2(PGconn *conn)
679
690
conn -> pgpass = PasswordFromFile (conn -> pghost ,conn -> pgport ,
680
691
conn -> dbName ,conn -> pguser );
681
692
if (conn -> pgpass == NULL )
693
+ {
682
694
conn -> pgpass = strdup (DefaultPassword );
695
+ if (!conn -> pgpass )
696
+ gotooom_error ;
697
+
698
+ }
683
699
else
684
700
conn -> dot_pgpass_used = true;
685
701
}
@@ -737,7 +753,11 @@ connectOptions2(PGconn *conn)
737
753
#endif
738
754
}
739
755
else
756
+ {
740
757
conn -> sslmode = strdup (DefaultSSLMode );
758
+ if (!conn -> sslmode )
759
+ gotooom_error ;
760
+ }
741
761
742
762
/*
743
763
* Only if we get this far is it appropriate to try to connect. (We need a
@@ -747,6 +767,12 @@ connectOptions2(PGconn *conn)
747
767
conn -> options_valid = true;
748
768
749
769
return true;
770
+
771
+ oom_error :
772
+ conn -> status = CONNECTION_BAD ;
773
+ printfPQExpBuffer (& conn -> errorMessage ,
774
+ libpq_gettext ("out of memory\n" ));
775
+ return false;
750
776
}
751
777
752
778
/*
@@ -829,6 +855,8 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
829
855
if (conn -> dbName )
830
856
free (conn -> dbName );
831
857
conn -> dbName = strdup (dbName );
858
+ if (!conn -> dbName )
859
+ gotooom_error ;
832
860
}
833
861
}
834
862
@@ -841,41 +869,53 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
841
869
if (conn -> pghost )
842
870
free (conn -> pghost );
843
871
conn -> pghost = strdup (pghost );
872
+ if (!conn -> pghost )
873
+ gotooom_error ;
844
874
}
845
875
846
876
if (pgport && pgport [0 ]!= '\0' )
847
877
{
848
878
if (conn -> pgport )
849
879
free (conn -> pgport );
850
880
conn -> pgport = strdup (pgport );
881
+ if (!conn -> pgport )
882
+ gotooom_error ;
851
883
}
852
884
853
885
if (pgoptions && pgoptions [0 ]!= '\0' )
854
886
{
855
887
if (conn -> pgoptions )
856
888
free (conn -> pgoptions );
857
889
conn -> pgoptions = strdup (pgoptions );
890
+ if (!conn -> pgoptions )
891
+ gotooom_error ;
858
892
}
859
893
860
894
if (pgtty && pgtty [0 ]!= '\0' )
861
895
{
862
896
if (conn -> pgtty )
863
897
free (conn -> pgtty );
864
898
conn -> pgtty = strdup (pgtty );
899
+ if (!conn -> pgtty )
900
+ gotooom_error ;
865
901
}
866
902
867
903
if (login && login [0 ]!= '\0' )
868
904
{
869
905
if (conn -> pguser )
870
906
free (conn -> pguser );
871
907
conn -> pguser = strdup (login );
908
+ if (!conn -> pguser )
909
+ gotooom_error ;
872
910
}
873
911
874
912
if (pwd && pwd [0 ]!= '\0' )
875
913
{
876
914
if (conn -> pgpass )
877
915
free (conn -> pgpass );
878
916
conn -> pgpass = strdup (pwd );
917
+ if (!conn -> pgpass )
918
+ gotooom_error ;
879
919
}
880
920
881
921
/*
@@ -891,6 +931,12 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
891
931
(void )connectDBComplete (conn );
892
932
893
933
return conn ;
934
+
935
+ oom_error :
936
+ conn -> status = CONNECTION_BAD ;
937
+ printfPQExpBuffer (& conn -> errorMessage ,
938
+ libpq_gettext ("out of memory\n" ));
939
+ return conn ;
894
940
}
895
941
896
942
@@ -3530,7 +3576,16 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
3530
3576
if (strcmp (options [i ].keyword ,optname )== 0 )
3531
3577
{
3532
3578
if (options [i ].val == NULL )
3579
+ {
3533
3580
options [i ].val = strdup (optval );
3581
+ if (!options [i ].val )
3582
+ {
3583
+ printfPQExpBuffer (errorMessage ,
3584
+ libpq_gettext ("out of memory\n" ));
3585
+ free (result );
3586
+ return 3 ;
3587
+ }
3588
+ }
3534
3589
found_keyword = true;
3535
3590
break ;
3536
3591
}
@@ -3753,6 +3808,13 @@ parseServiceFile(const char *serviceFile,
3753
3808
{
3754
3809
if (options [i ].val == NULL )
3755
3810
options [i ].val = strdup (val );
3811
+ if (!options [i ].val )
3812
+ {
3813
+ printfPQExpBuffer (errorMessage ,
3814
+ libpq_gettext ("out of memory\n" ));
3815
+ fclose (f );
3816
+ return 3 ;
3817
+ }
3756
3818
found_keyword = true;
3757
3819
break ;
3758
3820
}
@@ -4180,6 +4242,14 @@ conninfo_array_parse(const char **keywords, const char **values,
4180
4242
if (options [k ].val )
4181
4243
free (options [k ].val );
4182
4244
options [k ].val = strdup (str_option -> val );
4245
+ if (!options [k ].val )
4246
+ {
4247
+ printfPQExpBuffer (errorMessage ,
4248
+ libpq_gettext ("out of memory\n" ));
4249
+ PQconninfoFree (options );
4250
+ PQconninfoFree (str_options );
4251
+ return NULL ;
4252
+ }
4183
4253
break ;
4184
4254
}
4185
4255
}
@@ -4199,6 +4269,7 @@ conninfo_array_parse(const char **keywords, const char **values,
4199
4269
printfPQExpBuffer (errorMessage ,
4200
4270
libpq_gettext ("out of memory\n" ));
4201
4271
PQconninfoFree (options );
4272
+ PQconninfoFree (str_options );
4202
4273
return NULL ;
4203
4274
}
4204
4275
}