diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-02-06 17:57:24 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-02-14 12:29:36 +0100 |
commit | 65349283e32a5a0f0bf99469d9e596212a88e623 (patch) | |
tree | 12a387a63f307ba08b146ad0b8b11e88fd726fb3 | |
parent | 20d4cba63221cb144537518e492187ff82a590fa (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
Pick-to: 6.5
Change-Id: I98a9275262f5a05fee77905ce7c7d861f2dce6fe
Reviewed-by: André Hartmann <aha_1980@gmx.de>
-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(), |