|
| 1 | +From 25c2d05340eee01cf55457b8327f8f69d408879a Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?UTF-8?q?Morten=20S=C3=B8rvig?= <morten.sorvig@qt.io> |
| 3 | +Date: Fri, 24 Jun 2022 13:10:33 +0200 |
| 4 | +Subject: wasm: don't access deleted settings objects |
| 5 | +MIME-Version: 1.0 |
| 6 | +Content-Type: text/plain; charset=UTF-8 |
| 7 | +Content-Transfer-Encoding: 8bit |
| 8 | + |
| 9 | +The emscripten_idb_async_ functions are async, so there |
| 10 | +is no guarantee that the QSettings object which made |
| 11 | +the request will be live by the time the completion |
| 12 | +callback is made. |
| 13 | + |
| 14 | +Keep track of live QWasmSettingsPrivate objects, return |
| 15 | +early from the callbacks if userData does not point |
| 16 | +to a valid QWasmSettingsPrivate. |
| 17 | + |
| 18 | +Pick-to: 6.4 6.3 6.2 5.15 |
| 19 | +Change-Id: Ia319b1875dcf2c329ba27954e3f026e004fe0aac |
| 20 | +Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io> |
| 21 | +Reviewed-by: Lorn Potter <lorn.potter@gmail.com> |
| 22 | +--- |
| 23 | + src/corelib/io/qsettings_wasm.cpp | 43 ++++++++++++++++++++++++++------------- |
| 24 | + 1 file changed, 29 insertions(+), 14 deletions(-) |
| 25 | + |
| 26 | +(limited to 'src/corelib/io/qsettings_wasm.cpp') |
| 27 | + |
| 28 | +diff --git a/src/corelib/io/qsettings_wasm.cpp b/src/corelib/io/qsettings_wasm.cpp |
| 29 | +index ba081997fd..5c15e6d89b 100644 |
| 30 | +--- a/src/corelib/io/qsettings_wasm.cpp |
| 31 | ++++ b/src/corelib/io/qsettings_wasm.cpp |
| 32 | +@@ -13,6 +13,8 @@ |
| 33 | + |
| 34 | + #include <QFileInfo> |
| 35 | + #include <QDir> |
| 36 | ++#include <QList> |
| 37 | ++ |
| 38 | + #include <emscripten.h> |
| 39 | + |
| 40 | + QT_BEGIN_NAMESPACE |
| 41 | +@@ -27,6 +29,7 @@ public: |
| 42 | + QWasmSettingsPrivate(QSettings::Scope scope, const QString &organization, |
| 43 | + const QString &application); |
| 44 | + ~QWasmSettingsPrivate(); |
| 45 | ++ static QWasmSettingsPrivate *get(void *userData); |
| 46 | + |
| 47 | + std::optional<QVariant> get(const QString &key) const override; |
| 48 | + QStringList children(const QString &prefix, ChildSpec spec) const override; |
| 49 | +@@ -43,14 +46,19 @@ public: |
| 50 | + private: |
| 51 | + QString databaseName; |
| 52 | + QString id; |
| 53 | ++ static QList<QWasmSettingsPrivate *> liveSettings; |
| 54 | + }; |
| 55 | + |
| 56 | ++QList<QWasmSettingsPrivate *> QWasmSettingsPrivate::liveSettings; |
| 57 | ++ |
| 58 | + static void QWasmSettingsPrivate_onLoad(void *userData, void *dataPtr, int size) |
| 59 | + { |
| 60 | +- QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData); |
| 61 | ++ QWasmSettingsPrivate *settings = QWasmSettingsPrivate::get(userData); |
| 62 | ++ if (!settings) |
| 63 | ++ return; |
| 64 | + |
| 65 | +- QFile file(wasm->fileName()); |
| 66 | +- QFileInfo fileInfo(wasm->fileName()); |
| 67 | ++ QFile file(settings->fileName()); |
| 68 | ++ QFileInfo fileInfo(settings->fileName()); |
| 69 | + QDir dir(fileInfo.path()); |
| 70 | + if (!dir.exists()) |
| 71 | + dir.mkpath(fileInfo.path()); |
| 72 | +@@ -58,32 +66,29 @@ static void QWasmSettingsPrivate_onLoad(void *userData, void *dataPtr, int size) |
| 73 | + if (file.open(QFile::WriteOnly)) { |
| 74 | + file.write(reinterpret_cast<char *>(dataPtr), size); |
| 75 | + file.close(); |
| 76 | +- wasm->setReady(); |
| 77 | ++ settings->setReady(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static void QWasmSettingsPrivate_onError(void *userData) |
| 82 | + { |
| 83 | +- QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData); |
| 84 | +- if (wasm) |
| 85 | +- wasm->setStatus(QSettings::AccessError); |
| 86 | ++ if (QWasmSettingsPrivate *settings = QWasmSettingsPrivate::get(userData)) |
| 87 | ++ settings->setStatus(QSettings::AccessError); |
| 88 | + } |
| 89 | + |
| 90 | + static void QWasmSettingsPrivate_onStore(void *userData) |
| 91 | + { |
| 92 | +- QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData); |
| 93 | +- if (wasm) |
| 94 | +- wasm->setStatus(QSettings::NoError); |
| 95 | ++ if (QWasmSettingsPrivate *settings = QWasmSettingsPrivate::get(userData)) |
| 96 | ++ settings->setStatus(QSettings::NoError); |
| 97 | + } |
| 98 | + |
| 99 | + static void QWasmSettingsPrivate_onCheck(void *userData, int exists) |
| 100 | + { |
| 101 | +- QWasmSettingsPrivate *wasm = reinterpret_cast<QWasmSettingsPrivate *>(userData); |
| 102 | +- if (wasm) { |
| 103 | ++ if (QWasmSettingsPrivate *settings = QWasmSettingsPrivate::get(userData)) { |
| 104 | + if (exists) |
| 105 | +- wasm->loadLocal(wasm->fileName().toLocal8Bit()); |
| 106 | ++ settings->loadLocal(settings->fileName().toLocal8Bit()); |
| 107 | + else |
| 108 | +- wasm->setReady(); |
| 109 | ++ settings->setReady(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +@@ -114,6 +119,8 @@ QWasmSettingsPrivate::QWasmSettingsPrivate(QSettings::Scope scope, const QString |
| 114 | + const QString &application) |
| 115 | + : QConfFileSettingsPrivate(QSettings::NativeFormat, scope, organization, application) |
| 116 | + { |
| 117 | ++ liveSettings.push_back(this); |
| 118 | ++ |
| 119 | + setStatus(QSettings::AccessError); // access error until sandbox gets loaded |
| 120 | + databaseName = organization; |
| 121 | + id = application; |
| 122 | +@@ -127,6 +134,14 @@ QWasmSettingsPrivate::QWasmSettingsPrivate(QSettings::Scope scope, const QString |
| 123 | + |
| 124 | + QWasmSettingsPrivate::~QWasmSettingsPrivate() |
| 125 | + { |
| 126 | ++ liveSettings.removeAll(this); |
| 127 | ++} |
| 128 | ++ |
| 129 | ++QWasmSettingsPrivate *QWasmSettingsPrivate::get(void *userData) |
| 130 | ++{ |
| 131 | ++ if (QWasmSettingsPrivate::liveSettings.contains(reinterpret_cast<QWasmSettingsPrivate *>(userData))) |
| 132 | ++ return reinterpret_cast<QWasmSettingsPrivate *>(userData); |
| 133 | ++ return nullptr; |
| 134 | + } |
| 135 | + |
| 136 | + void QWasmSettingsPrivate::initAccess() |
| 137 | +-- |
| 138 | +cgit v1.2.3 |
| 139 | + |
0 commit comments