Commit fad3cfd2 authored by Igor Kushnir's avatar Igor Kushnir Committed by Milian Wolff
Browse files

Prevent using commentRepository() without locking

commentRepository() returned a reference to StringRepository, which
could be accidentally used directly rather than via the helper function
commentRepositoryOp() that locks the mutex. Prevent such usage mistakes
by encapsulating the mutex and the repository in class CommentRepository
similar to class StaticCacheData in modificationrevision.cpp.
parent 090dbf9f
......@@ -51,21 +51,29 @@ DeclarationData::DeclarationData()
namespace {
///@todo Use reference counting
static Repositories::StringRepository& commentRepository()
class CommentRepository
static QMutex mutex;
static Repositories::StringRepository commentRepositoryObject(QStringLiteral("Comment Repository"), &mutex);
return commentRepositoryObject;
template <typename Op>
auto op(Op&& op)
QMutexLocker lock(&m_mutex);
return op(m_comments);
QMutex m_mutex;
Repositories::StringRepository m_comments{QStringLiteral("Comment Repository"), &m_mutex};
template <typename Op>
static auto commentRepositoryOp(Op&& op)
CommentRepository& commentRepository()
auto& repo = commentRepository();
QMutexLocker lock(repo.mutex());
return op(repo);
static CommentRepository comments;
return comments;
} // unnamed namespace
void initDeclarationRepositories()
......@@ -177,7 +185,7 @@ QByteArray Declaration::comment() const
if (!d->m_comment)
return QByteArray();
return commentRepositoryOp([d](const Repositories::StringRepository& repo) {
return commentRepository().op([d](const Repositories::StringRepository& repo) {
return Repositories::arrayFromItem(repo.itemFromIndex(d->m_comment));
......@@ -193,7 +201,7 @@ void Declaration::setComment(const QByteArray& str)
const auto request = Repositories::StringRepositoryItemRequest(
str.constData(), IndexedString::hashString(str.constData(), str.length()), str.length());
d->m_comment = commentRepositoryOp([&](Repositories::StringRepository& repo) { return repo.index(request); });
d->m_comment = commentRepository().op([&](Repositories::StringRepository& repo) { return repo.index(request); });
void Declaration::setComment(const QString& str)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment