QNetworkInformation: Revise locking during creation
Potential issue caught by the Mårten static analyzer. In case another thread somehow ended up creating and returning an instance while another was waiting to relock it would deallocate the previous instance, which could lead to some bad situations. Change-Id: I6e1843f8a483b2c3e0540e998c383e41f59c8655 Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> (cherry picked from commit c98e92b8ca7fd295482ee99f095c220b6f389169) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
933162c445
commit
b9a721eaaa
@ -168,22 +168,21 @@ QNetworkInformation *QNetworkInformationPrivate::create(QStringView name)
|
|||||||
{
|
{
|
||||||
if (!dataHolder())
|
if (!dataHolder())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
QMutexLocker locker(&dataHolder->instanceMutex);
|
|
||||||
#ifdef DEBUG_LOADING
|
#ifdef DEBUG_LOADING
|
||||||
qDebug().nospace() << "create() called with name=\"" << name
|
qDebug().nospace() << "create() called with name=\"" << name
|
||||||
<< "\". instanceHolder initialized? " << !!dataHolder->instanceHolder;
|
<< "\". instanceHolder initialized? " << !!dataHolder->instanceHolder;
|
||||||
#endif
|
#endif
|
||||||
if (dataHolder->instanceHolder)
|
|
||||||
return dataHolder->instanceHolder.get();
|
|
||||||
|
|
||||||
locker.unlock();
|
|
||||||
if (!initializeList()) {
|
if (!initializeList()) {
|
||||||
#ifdef DEBUG_LOADING
|
#ifdef DEBUG_LOADING
|
||||||
qDebug("Failed to initialize list, returning.");
|
qDebug("Failed to initialize list, returning.");
|
||||||
#endif
|
#endif
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
locker.relock();
|
|
||||||
|
QMutexLocker locker(&dataHolder->instanceMutex);
|
||||||
|
if (dataHolder->instanceHolder)
|
||||||
|
return dataHolder->instanceHolder.get();
|
||||||
|
|
||||||
|
|
||||||
QNetworkInformationBackend *backend = nullptr;
|
QNetworkInformationBackend *backend = nullptr;
|
||||||
if (!name.isEmpty()) {
|
if (!name.isEmpty()) {
|
||||||
@ -231,24 +230,22 @@ QNetworkInformation *QNetworkInformationPrivate::create(QNetworkInformation::Fea
|
|||||||
{
|
{
|
||||||
if (!dataHolder())
|
if (!dataHolder())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
QMutexLocker locker(&dataHolder->instanceMutex);
|
|
||||||
#ifdef DEBUG_LOADING
|
#ifdef DEBUG_LOADING
|
||||||
qDebug().nospace() << "create() called with features=\"" << features
|
qDebug().nospace() << "create() called with features=\"" << features
|
||||||
<< "\". instanceHolder initialized? " << !!dataHolder->instanceHolder;
|
<< "\". instanceHolder initialized? " << !!dataHolder->instanceHolder;
|
||||||
#endif
|
#endif
|
||||||
if (dataHolder->instanceHolder)
|
|
||||||
return dataHolder->instanceHolder.get();
|
|
||||||
if (features == 0)
|
if (features == 0)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
locker.unlock();
|
|
||||||
if (!initializeList()) {
|
if (!initializeList()) {
|
||||||
#ifdef DEBUG_LOADING
|
#ifdef DEBUG_LOADING
|
||||||
qDebug("Failed to initialize list, returning.");
|
qDebug("Failed to initialize list, returning.");
|
||||||
#endif
|
#endif
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
locker.relock();
|
QMutexLocker locker(&dataHolder->instanceMutex);
|
||||||
|
if (dataHolder->instanceHolder)
|
||||||
|
return dataHolder->instanceHolder.get();
|
||||||
|
|
||||||
const auto supportsRequestedFeatures = [features](QNetworkInformationBackendFactory *factory) {
|
const auto supportsRequestedFeatures = [features](QNetworkInformationBackendFactory *factory) {
|
||||||
return factory && (factory->featuresSupported() & features) == features;
|
return factory && (factory->featuresSupported() & features) == features;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user