Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4231 postgre sql leaks memory for failed connections #4239

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Data/PostgreSQL/src/SessionHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,24 @@ void SessionHandle::connect(const std::string& aConnectionString)
{
throw ConnectionFailedException("Already Connected");
}
else if (_pConnection)
{
// free bad connection
PQfinish(_pConnection);
_pConnection = 0;
}

_pConnection = PQconnectdb(aConnectionString.c_str());

if (!isConnectedNoLock())
{
throw ConnectionFailedException(std::string("Connection Error: ") + lastErrorNoLock());
std::string msg = std::string("Connection Error: ") + lastErrorNoLock();
if (_pConnection)
{
PQfinish(_pConnection);
_pConnection = 0;
}
throw ConnectionFailedException(msg);
}

_connectionString = aConnectionString;
Expand Down Expand Up @@ -137,7 +149,7 @@ void SessionHandle::disconnect()
{
Poco::FastMutex::ScopedLock mutexLocker(_sessionMutex);

if (isConnectedNoLock())
if (_pConnection)
{
PQfinish(_pConnection);

Expand Down
30 changes: 29 additions & 1 deletion Data/PostgreSQL/testsuite/src/PostgreSQLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ void PostgreSQLTest::testConnectNoDB()
}
}


void PostgreSQLTest::testFailedConnect()
{
std::string dbConnString;
dbConnString += "host=" + getHost();
dbConnString += " user=invalid";
dbConnString += " password=invalid";
dbConnString += " port=" + getPort();

try
{
std::cout << "Attempting to Connect to [" << dbConnString << "] with invalid credentials: " << std::endl;
Session session(PostgreSQL::Connector::KEY, dbConnString);
fail ("must fail");
}
catch (ConnectionFailedException& ex)
{
std::cout << ex.displayText() << std::endl;
}
catch (ConnectionException& ex)
{
std::cout << ex.displayText() << std::endl;
}
}


void PostgreSQLTest::testPostgreSQLOIDs()
{
if (!_pSession) fail ("Test not available.");
Expand Down Expand Up @@ -309,6 +335,7 @@ void PostgreSQLTest::testBarebonePostgreSQL()
}



void PostgreSQLTest::testSimpleAccess()
{
if (!_pSession) fail ("Test not available.");
Expand Down Expand Up @@ -775,7 +802,7 @@ void PostgreSQLTest::testSqlState()
}
catch (const Poco::Data::PostgreSQL::PostgreSQLException & exception)
{
assertTrue(exception.sqlState() == std::string("42601"));
assertTrue(exception.sqlState() == std::string("42601"));
}
}

Expand Down Expand Up @@ -1236,6 +1263,7 @@ CppUnit::Test* PostgreSQLTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PostgreSQLTest");

CppUnit_addTest(pSuite, PostgreSQLTest, testConnectNoDB);
CppUnit_addTest(pSuite, PostgreSQLTest, testFailedConnect);
CppUnit_addTest(pSuite, PostgreSQLTest, testPostgreSQLOIDs);
//CppUnit_addTest(pSuite, PostgreSQLTest, testBarebonePostgreSQL);
CppUnit_addTest(pSuite, PostgreSQLTest, testSimpleAccess);
Expand Down
1 change: 1 addition & 0 deletions Data/PostgreSQL/testsuite/src/PostgreSQLTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PostgreSQLTest: public CppUnit::TestCase
~PostgreSQLTest();

void testConnectNoDB();
void testFailedConnect();
void testPostgreSQLOIDs();
void testBarebonePostgreSQL();

Expand Down
Loading