Skip to content

Commit

Permalink
Fix crash when using QProperty<T>::setBinding(Functor ...)
Browse files Browse the repository at this point in the history
We must move the functor properly into the binding object, otherwise we
end up with stale pointers as pointed out by ASAN.

Change-Id: Icd84f4c113dd48e1e3e2d744abac0902cdf9339e
Reviewed-by: Fabian Kosmale <[email protected]>
  • Loading branch information
tronical committed Apr 30, 2020
1 parent 0c4bc39 commit 7096489
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/corelib/kernel/qproperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,11 @@ class QProperty

#ifndef Q_CLANG_QDOC
template <typename Functor>
QPropertyBinding<T> setBinding(Functor f,
const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION)
QPropertyBinding<T> setBinding(Functor &&f,
const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION,
std::enable_if_t<std::is_invocable_v<Functor>> * = nullptr)
{
return setBinding(Qt::makePropertyBinding(f, location));
return setBinding(Qt::makePropertyBinding(std::forward<Functor>(f), location));
}
#else
template <typename Functor>
Expand Down
12 changes: 12 additions & 0 deletions tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private slots:
void genericPropertyBinding();
void genericPropertyBindingBool();
void staticChangeHandler();
void setBindingFunctor();
};

void tst_QProperty::functorBinding()
Expand Down Expand Up @@ -674,6 +675,17 @@ void tst_QProperty::staticChangeHandler()
QCOMPARE(t.observedValues, values);
}

void tst_QProperty::setBindingFunctor()
{
QProperty<int> property;
QProperty<int> injectedValue(100);
// Make sure that this picks the setBinding overload that takes a functor and
// moves it correctly.
property.setBinding([&injectedValue]() { return injectedValue.value(); });
injectedValue = 200;
QCOMPARE(property.value(), 200);
}

QTEST_MAIN(tst_QProperty);

#include "tst_qproperty.moc"

0 comments on commit 7096489

Please sign in to comment.