Skip to content

Poco::Data::PostgreSQL::SessionHandle::disconnect() leaks memory for failed connections #4231

@tadas-smetanin

Description

@tadas-smetanin

Poco::Data::PostgreSQL::SessionHandle::disconnect() implementation does not free (PQfinish) PGconn* _pConnection, if the connection has failed:

SessionHandle::~SessionHandle()
{
	try
	{
		disconnect();
	}
	catch (...)
	{
	}
}

void SessionHandle::disconnect()
{
	Poco::FastMutex::ScopedLock mutexLocker(_sessionMutex);

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

		_pConnection = 0;

		_connectionString = std::string();
		_inTransaction= false;
		_isAutoCommit = true;
		_isAsynchronousCommit = false;
		_tranactionIsolationLevel = Session::TRANSACTION_READ_COMMITTED;
	}
}

bool SessionHandle::isConnectedNoLock() const
{
	// DO NOT ACQUIRE THE MUTEX IN PRIVATE METHODS

	if (_pConnection && PQstatus(_pConnection) == CONNECTION_OK)
	{
		return true;
	}
	return false;
}

As stated by libpq documentation:

Note that even if the server connection attempt fails (as indicated by PQstatus), the application should call PQfinish to free the memory used by the PGconn object. The PGconn pointer must not be used again after PQfinish has been called.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions