From 0041a9f8507bd8945e69a724a0598b2869f6c280 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sun, 16 Jun 2013 11:21:34 +0200 Subject: [PATCH] Make access to Connection->nodeid thread-safe --- src/libtomahawk/network/Connection.cpp | 22 ++++++++++++++++------ src/libtomahawk/network/Connection_p.h | 2 ++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/libtomahawk/network/Connection.cpp b/src/libtomahawk/network/Connection.cpp index a447440e6..791922aa0 100644 --- a/src/libtomahawk/network/Connection.cpp +++ b/src/libtomahawk/network/Connection.cpp @@ -311,7 +311,10 @@ Connection::start( QTcpSocket* sock ) void Connection::checkACL() { - if ( d_func()->nodeid.isEmpty() ) + Q_D( Connection ); + QReadLocker nodeidLocker( &d->nodeidLock ); + + if ( d->nodeid.isEmpty() ) { tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Not checking ACL, nodeid is empty"; QTimer::singleShot( 0, this, SLOT( doSetup() ) ); @@ -328,7 +331,7 @@ Connection::checkACL() connect( ACLRegistry::instance(), SIGNAL( aclResult( QString, QString, Tomahawk::ACLStatus::Type ) ), this, SLOT( checkACLResult( QString, QString, Tomahawk::ACLStatus::Type ) ), Qt::QueuedConnection ); - QMetaObject::invokeMethod( ACLRegistry::instance(), "isAuthorizedUser", Qt::QueuedConnection, Q_ARG( QString, d_func()->nodeid ), Q_ARG( QString, bareName() ), Q_ARG( Tomahawk::ACLStatus::Type, Tomahawk::ACLStatus::NotFound ) ); + QMetaObject::invokeMethod( ACLRegistry::instance(), "isAuthorizedUser", Qt::QueuedConnection, Q_ARG( QString, d->nodeid ), Q_ARG( QString, bareName() ), Q_ARG( Tomahawk::ACLStatus::Type, Tomahawk::ACLStatus::NotFound ) ); } @@ -341,9 +344,12 @@ Connection::bareName() const void Connection::checkACLResult( const QString &nodeid, const QString &username, Tomahawk::ACLStatus::Type peerStatus ) { - if ( nodeid != d_func()->nodeid ) + Q_D( Connection ); + QReadLocker nodeidLocker( &d->nodeidLock ); + + if ( nodeid != d->nodeid ) { - tDebug( LOGVERBOSE ) << Q_FUNC_INFO << QString( "nodeid (%1) not ours (%2) for user %3" ).arg( nodeid ).arg( d_func()->nodeid ).arg( username ); + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << QString( "nodeid (%1) not ours (%2) for user %3" ).arg( nodeid ).arg( d->nodeid ).arg( username ); return; } if ( username != bareName() ) @@ -494,13 +500,17 @@ Connection::setId( const QString& id ) QString Connection::nodeId() const { - return d_func()->nodeid; + Q_D( const Connection ); + QReadLocker locker( &d->nodeidLock ); + return d->nodeid; } void Connection::setNodeId( const QString& nodeid ) { - d_func()->nodeid = nodeid; + Q_D( Connection ); + QWriteLocker locker( &d->nodeidLock ); + d->nodeid = nodeid; } diff --git a/src/libtomahawk/network/Connection_p.h b/src/libtomahawk/network/Connection_p.h index 6f64d944f..83435ac87 100644 --- a/src/libtomahawk/network/Connection_p.h +++ b/src/libtomahawk/network/Connection_p.h @@ -25,6 +25,7 @@ #include "MsgProcessor.h" +#include #include #include @@ -67,6 +68,7 @@ private: QString id; QString name; QString nodeid; + mutable QReadWriteLock nodeidLock; msg_ptr msg; msg_ptr firstmsg; int peerport;