diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-02-06 17:57:24 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-02-14 11:46:45 +0000 |
commit | 7eb6b74200cd1c4d2cc764af551cea17eb3398cf (patch) | |
tree | 2535df0bdb6a110fe08ca04e51a01c91925423b7 | |
parent | 77d851496b651122151ed0b85f9449b9251cdaa6 (diff) |
Modbus server: fix data conversion error handling
In line
ok = modbusDevice->setData(..., value.toUShort(&ok, 16));
the value of 'ok' variable after the toUShort() conversion is
immediately overwritten by the return value of setData() method.
As a result, the code could never detect that toUShort() conversion
failed.
This commit splits the line into several independent statements, adding
an if (ok) check in between.
Task-number: QTBUG-110890
Change-Id: I98a9275262f5a05fee77905ce7c7d861f2dce6fe
Reviewed-by: André Hartmann <aha_1980@gmx.de>
(cherry picked from commit 65349283e32a5a0f0bf99469d9e596212a88e623)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | examples/serialbus/modbus/server/mainwindow.cpp | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/examples/serialbus/modbus/server/mainwindow.cpp b/examples/serialbus/modbus/server/mainwindow.cpp index 3bdcefa..1dc9f35 100644 --- a/examples/serialbus/modbus/server/mainwindow.cpp +++ b/examples/serialbus/modbus/server/mainwindow.cpp @@ -212,10 +212,15 @@ void MainWindow::setRegister(const QString &value) if (registers.contains(objectName)) { bool ok = true; const quint16 id = quint16(QObject::sender()->property("ID").toUInt()); - if (objectName.startsWith(QStringLiteral("inReg"))) - ok = modbusDevice->setData(QModbusDataUnit::InputRegisters, id, value.toUShort(&ok, 16)); - else if (objectName.startsWith(QStringLiteral("holdReg"))) - ok = modbusDevice->setData(QModbusDataUnit::HoldingRegisters, id, value.toUShort(&ok, 16)); + if (objectName.startsWith(QStringLiteral("inReg"))) { + const auto uval = value.toUShort(&ok, 16); + if (ok) + ok = modbusDevice->setData(QModbusDataUnit::InputRegisters, id, uval); + } else if (objectName.startsWith(QStringLiteral("holdReg"))) { + const auto uval = value.toUShort(&ok, 16); + if (ok) + ok = modbusDevice->setData(QModbusDataUnit::HoldingRegisters, id, uval); + } if (!ok) statusBar()->showMessage(tr("Could not set register: ") + modbusDevice->errorString(), |