Fix race condition in getting the system locale data

QSharedDataPointer obeys the regular Qt container thread-safety rules:
it's thread-safe in const methods but not in mutating ones. QSDP::data()
is mutating, which causes a data race. For example, if the contained
QLocalePrivate has a refcount of 2 and two threads see that, both
threads will try to detach and then replace the pointer, but that
pointer replacement is not atomic.

Using QExplicitSharedDataPointer makes the race go away, since data() is
now non-mutating. QESDP is used only to destroy the QLocalePrivate on
program shutdown.

Note that there are still race conditions relating to *updating* the
locale private.

Fixes: QTBUG-73403
Change-Id: Id98140e1c2f0426cabbefffd157ed6ec30a3e08f
Reviewed-by: Thomas Sondergaard <thomas@sondergaard.cc>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2019-01-30 21:13:51 -08:00
parent d84912838c
commit 188eea0eb4

View File

@ -1,7 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -781,7 +781,7 @@ static const int locale_data_size = sizeof(locale_data)/sizeof(QLocaleData) - 1;
Q_GLOBAL_STATIC_WITH_ARGS(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate,
(QLocalePrivate::create(defaultData(), default_number_options)))
Q_GLOBAL_STATIC_WITH_ARGS(QSharedDataPointer<QLocalePrivate>, systemLocalePrivate,
Q_GLOBAL_STATIC_WITH_ARGS(QExplicitlySharedDataPointer<QLocalePrivate>, systemLocalePrivate,
(QLocalePrivate::create(systemData())))
static QLocalePrivate *localePrivateByName(const QString &name)