Skip to content

Commit 304c914

Browse files
committed
Postfix for #8429: Segfault when already destroyed callback interface is used; more performance optimization
1 parent f08b23b commit 304c914

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

src/jrd/extds/ExtDS.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ Connection* Manager::getConnection(thread_db* tdbb, const string& dataSource,
207207

208208
Provider* prv = getProvider(prvName);
209209

210-
const bool isCurrent = (prvName == INTERNAL_PROVIDER_NAME) &&
210+
const bool isCurrentAtt = (prvName == INTERNAL_PROVIDER_NAME) &&
211211
isCurrentAccount(att->att_user, user, pwd, role);
212212

213213
ClumpletWriter dpb(ClumpletReader::dpbList, MAX_DPB_SIZE);
214-
if (!isCurrent)
214+
if (!isCurrentAtt)
215215
prv->generateDPB(tdbb, dpb, user, pwd, role);
216216

217217
// look up at connections already bound to current attachment
218-
Connection* conn = prv->getBoundConnection(tdbb, dbName, dpb, tra_scope);
218+
Connection* conn = prv->getBoundConnection(tdbb, dbName, dpb, tra_scope, isCurrentAtt);
219219
if (conn)
220220
return conn;
221221

@@ -226,7 +226,7 @@ Connection* Manager::getConnection(thread_db* tdbb, const string& dataSource,
226226

227227
ULONG hash = 0;
228228

229-
if (!isCurrent)
229+
if (!isCurrentAtt)
230230
{
231231
CryptHash ch(att->att_crypt_callback);
232232
hash = DefaultHash<UCHAR>::hash(dbName.c_str(), dbName.length(), MAX_ULONG) +
@@ -255,7 +255,7 @@ Connection* Manager::getConnection(thread_db* tdbb, const string& dataSource,
255255
{
256256
// finally, create new connection
257257
conn = prv->createConnection(tdbb, dbName, dpb, tra_scope);
258-
if (!isCurrent)
258+
if (!isCurrentAtt)
259259
m_connPool->addConnection(tdbb, conn, hash);
260260
}
261261

@@ -358,7 +358,10 @@ Connection* Provider::createConnection(thread_db* tdbb,
358358
const PathName& dbName, ClumpletReader& dpb, TraScope tra_scope)
359359
{
360360
Connection* conn = doCreateConnection();
361-
conn->setup(dbName, dpb, tdbb->getAttachment()->att_crypt_callback);
361+
conn->setup(dbName, dpb);
362+
if (!conn->isCurrent())
363+
conn->setCallbackRedirect(tdbb->getAttachment()->att_crypt_callback);
364+
362365
try
363366
{
364367
conn->attach(tdbb);
@@ -390,11 +393,13 @@ void Provider::bindConnection(thread_db* tdbb, Connection* conn)
390393

391394
Connection* Provider::getBoundConnection(Jrd::thread_db* tdbb,
392395
const Firebird::PathName& dbName, Firebird::ClumpletReader& dpb,
393-
TraScope tra_scope)
396+
TraScope tra_scope, bool isCurrentAtt)
394397
{
395398
Database* dbb = tdbb->getDatabase();
396399
Attachment* att = tdbb->getAttachment();
397-
CryptHash ch(att->att_crypt_callback);
400+
CryptHash ch;
401+
if (!isCurrentAtt)
402+
ch.assign(att->att_crypt_callback);
398403

399404
MutexLockGuard guard(m_mutex, FB_FUNCTION);
400405

@@ -590,14 +595,12 @@ Connection::Connection(Provider& prov) :
590595
{
591596
}
592597

593-
void Connection::setup(const PathName& dbName, const ClumpletReader& dpb, ICryptKeyCallback* attCallback)
598+
void Connection::setup(const PathName& dbName, const ClumpletReader& dpb)
594599
{
595600
m_dbName = dbName;
596601

597602
m_dpb.clear();
598603
m_dpb.add(dpb.getBuffer(), dpb.getBufferLength());
599-
600-
m_cryptCallbackRedir.setRedirect(attCallback);
601604
}
602605

603606
void Connection::deleteConnection(thread_db* tdbb, Connection* conn)
@@ -2668,7 +2671,7 @@ void CryptHash::assign(ICryptKeyCallback* callback)
26682671

26692672
bool CryptHash::operator==(const CryptHash& h) const
26702673
{
2671-
return isValid() && h.isValid() && m_value == h.m_value;
2674+
return (isValid() == h.isValid()) && (m_value == h.m_value);
26722675
}
26732676

26742677

src/jrd/extds/ExtDS.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class Provider : public Firebird::GlobalStorage
192192
// get available connection already bound to the current attachment
193193
Connection* getBoundConnection(Jrd::thread_db* tdbb,
194194
const Firebird::PathName& dbName, Firebird::ClumpletReader& dpb,
195-
TraScope tra_scope);
195+
TraScope tra_scope, bool isCurrent);
196196

197197
// Connection gets unused, release it into pool or delete it immediately
198198
virtual void releaseConnection(Jrd::thread_db* tdbb, Connection& conn, bool inPool = true);
@@ -491,7 +491,12 @@ class Connection : public Firebird::PermanentStorage
491491
virtual ~Connection();
492492

493493
static void deleteConnection(Jrd::thread_db* tdbb, Connection* conn);
494-
void setup(const Firebird::PathName& dbName, const Firebird::ClumpletReader& dpb, Firebird::ICryptKeyCallback* attCallback);
494+
void setup(const Firebird::PathName& dbName, const Firebird::ClumpletReader& dpb);
495+
496+
void setCallbackRedirect(Firebird::ICryptKeyCallback* attCallback)
497+
{
498+
m_cryptCallbackRedir.setRedirect(attCallback);
499+
}
495500

496501
void setBoundAtt(Jrd::Attachment* att) { m_boundAtt = att; }
497502

src/jrd/extds/InternalDS.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,10 @@ void InternalConnection::attach(thread_db* tdbb)
136136
// Don't wrap raised errors. This is needed for backward compatibility.
137137
setWrapErrors(false);
138138

139-
if (m_dpb.isEmpty())
140-
{
141-
m_isCurrent = true;
139+
if (isCurrent())
142140
m_attachment = attachment->getInterface();
143-
}
144141
else
145142
{
146-
m_isCurrent = false;
147143
m_dbName = dbb->dbb_database_name.c_str();
148144

149145
// Avoid change of m_dpb by validatePassword() below
@@ -182,7 +178,7 @@ void InternalConnection::doDetach(thread_db* tdbb)
182178
if (!m_attachment->getHandle())
183179
return;
184180

185-
if (m_isCurrent)
181+
if (isCurrent())
186182
{
187183
m_attachment = NULL;
188184
}
@@ -221,7 +217,7 @@ bool InternalConnection::cancelExecution(bool /*forced*/)
221217
if (!m_attachment->getHandle())
222218
return false;
223219

224-
if (m_isCurrent)
220+
if (isCurrent())
225221
return true;
226222

227223
FbLocalStatus status;
@@ -232,9 +228,9 @@ bool InternalConnection::cancelExecution(bool /*forced*/)
232228

233229
bool InternalConnection::resetSession(thread_db* tdbb)
234230
{
235-
fb_assert(!m_isCurrent);
231+
fb_assert(isCurrent());
236232

237-
if (m_isCurrent)
233+
if (isCurrent())
238234
return true;
239235

240236
FbLocalStatus status;
@@ -252,13 +248,13 @@ bool InternalConnection::resetSession(thread_db* tdbb)
252248
// b) is not current connection
253249
bool InternalConnection::isAvailable(thread_db* tdbb, TraScope /*traScope*/) const
254250
{
255-
return !m_isCurrent ||
256-
(m_isCurrent && (tdbb->getAttachment() == m_attachment->getHandle()));
251+
return (!isCurrent()) ||
252+
(isCurrent() && (tdbb->getAttachment() == m_attachment->getHandle()));
257253
}
258254

259255
bool InternalConnection::validate(thread_db* tdbb)
260256
{
261-
if (m_isCurrent)
257+
if (isCurrent())
262258
return true;
263259

264260
if (!m_attachment)
@@ -272,7 +268,7 @@ bool InternalConnection::validate(thread_db* tdbb)
272268

273269
bool InternalConnection::isSameDatabase(const PathName& dbName, ClumpletReader& dpb, const CryptHash& ch) const
274270
{
275-
if (m_isCurrent)
271+
if (isCurrent())
276272
{
277273
const Attachment* att = m_attachment->getHandle();
278274
const MetaString& attUser = att->getUserName();

src/jrd/extds/InternalDS.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class InternalConnection : public Connection
5656

5757
explicit InternalConnection(InternalProvider& prov) :
5858
Connection(prov),
59-
m_attachment(0),
60-
m_isCurrent(false)
59+
m_attachment(0)
6160
{}
6261

6362
virtual ~InternalConnection();
@@ -76,7 +75,7 @@ class InternalConnection : public Connection
7675
virtual bool isSameDatabase(const Firebird::PathName& dbName,
7776
Firebird::ClumpletReader& dpb, const CryptHash& ch) const override;
7877

79-
virtual bool isCurrent() const { return m_isCurrent; }
78+
virtual bool isCurrent() const { return m_dpb.isEmpty(); }
8079

8180
Jrd::JAttachment* getJrdAtt() { return m_attachment; }
8281

@@ -89,7 +88,6 @@ class InternalConnection : public Connection
8988

9089
Firebird::AutoPlugin<Jrd::JProvider> m_provider;
9190
Firebird::RefPtr<Jrd::JAttachment> m_attachment;
92-
bool m_isCurrent;
9391
};
9492

9593

0 commit comments

Comments
 (0)