Skip to content

Commit 262d9dd

Browse files
committed
Fix memory leaks on Postgres connection failure
1 parent 7ed64a7 commit 262d9dd

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

geodiff/src/drivers/postgresdriver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ void PostgresDriver::openPrivate( const DriverParametersMap &conn )
9898

9999
if ( PQstatus( c ) != CONNECTION_OK )
100100
{
101-
throw GeoDiffException( "Cannot connect to PostgreSQL database: " + std::string( PQerrorMessage( c ) ) );
101+
std::string msg( PQerrorMessage( c ) );
102+
PQfinish( c );
103+
throw GeoDiffException( "Cannot connect to PostgreSQL database: " + msg );
102104
}
103105

104106
mConn = c;

geodiff/tests/test_driver_postgres.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ extern "C"
2424
void execSqlCommandsFromString( const std::string &conninfo, const std::string &sql )
2525
{
2626
PGconn *c = PQconnectdb( conninfo.c_str() );
27-
28-
ASSERT_EQ( PQstatus( c ), CONNECTION_OK );
27+
if ( PQstatus( c ) != CONNECTION_OK )
28+
{
29+
PQfinish( c );
30+
FAIL() << "Failed to connect to Postgres!";
31+
}
2932

3033
PGresult *res = PQexec( c, sql.c_str() );
3134
ASSERT_TRUE( res );

0 commit comments

Comments
 (0)