diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-06-06 17:58:09 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-06-09 08:59:53 +0000 |
commit | 733cb6267eadf3b11dcb847f2a802f4b0bd5cd3b (patch) | |
tree | d3091c04b4e6ae900f5a563d3b3b020661c6b788 | |
parent | 457c8adacd28242c3812c6b6140ff3a1a0d53338 (diff) |
QCanDbcFileParser: fix the size of Unique Id
The unique id can contain 11 or 29 bits, so apply a mask when
extracting it.
Update the unit-tests to use a bus value with the 31-st bit set,
to make sure that we handle such values correctly.
Fixes: QTBUG-114043
Change-Id: I1164bc13980af998007e35a3dcb4c22db2bda755
Reviewed-by: André Hartmann <aha_1980@gmx.de>
(cherry picked from commit 42cbbe9b681e6e8c2a6e1b9a88679e5158ec3977)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
7 files changed, 53 insertions, 42 deletions
diff --git a/src/serialbus/qcandbcfileparser.cpp b/src/serialbus/qcandbcfileparser.cpp index 7733c37..63d4f98 100644 --- a/src/serialbus/qcandbcfileparser.cpp +++ b/src/serialbus/qcandbcfileparser.cpp @@ -12,6 +12,8 @@ #include <QtCore/QFile> #include <QtCore/QRegularExpression> +#include <optional> + QT_BEGIN_NAMESPACE /*! @@ -408,6 +410,15 @@ bool QCanDbcFileParserPrivate::processLine(const QStringView line) return true; } +static std::optional<QtCanBus::UniqueId> extractUniqueId(QStringView view) +{ + bool ok = false; + const uint value = view.toUInt(&ok); + if (ok) + return QtCanBus::UniqueId{value & 0x1FFFFFFF}; + return std::nullopt; +} + /*! \internal Returns \c false only in case of hard error. Returns \c true even if some @@ -457,16 +468,15 @@ QCanDbcFileParserPrivate::extractMessage(const QRegularExpressionMatch &match) QCanMessageDescription desc; desc.setName(match.captured(u"name"_s)); - bool ok = false; - - const QtCanBus::UniqueId id{match.capturedView(u"messageId"_s).toUInt(&ok)}; - if (ok) { - desc.setUniqueId(id); + const auto id = extractUniqueId(match.capturedView(u"messageId"_s)); + if (id.has_value()) { + desc.setUniqueId(id.value()); } else { addWarning(QObject::tr("Failed to parse frame id for message %1").arg(desc.name())); return {}; } + bool ok = false; const auto size = match.capturedView(u"size"_s).toUInt(&ok); if (ok) { desc.setSize(size); @@ -659,18 +669,19 @@ void QCanDbcFileParserPrivate::parseSignalType(const QStringView data) m_lineOffset = match.capturedEnd(0); - bool ok = false; - const QtCanBus::UniqueId uid{match.capturedView(u"messageId"_s).toUInt(&ok)}; - if (!ok) { + const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s)); + if (!uidOptional) { addWarning(QObject::tr("Failed to parse frame id from string %1").arg(data)); return; } + const QtCanBus::UniqueId uid = uidOptional.value(); auto msgDesc = m_messageDescriptions.value(uid); if (msgDesc.isValid()) { const QString sigName = match.captured(u"sigName"_s); auto sigDesc = msgDesc.signalDescriptionForName(sigName); if (sigDesc.isValid()) { + bool ok = false; const auto type = match.capturedView(u"type").toUInt(&ok); if (ok) { bool sigDescChanged = false; @@ -742,13 +753,13 @@ void QCanDbcFileParserPrivate::parseComment(const QStringView data) const auto type = match.capturedView(u"type"_s); - bool ok = false; - const QtCanBus::UniqueId uid{match.capturedView(u"messageId"_s).toUInt(&ok)}; - if (!ok) { + const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s)); + if (!uidOptional) { addWarning(QObject::tr("Failed to parse frame id from string %1").arg(data)); return; } + const QtCanBus::UniqueId uid = uidOptional.value(); auto messageDesc = m_messageDescriptions.value(uid); if (!messageDesc.isValid()) { addWarning(QObject::tr("Failed to find message description for unique id %1. " @@ -806,13 +817,13 @@ void QCanDbcFileParserPrivate::parseExtendedMux(const QStringView data) m_lineOffset = match.capturedEnd(0); - bool ok = false; - const QtCanBus::UniqueId uid{match.capturedView(u"messageId"_s).toUInt(&ok)}; - if (!ok) { + const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s)); + if (!uidOptional) { addWarning(QObject::tr("Failed to parse frame id from string %1").arg(data)); return; } + const QtCanBus::UniqueId uid = uidOptional.value(); auto messageDesc = m_messageDescriptions.value(uid); if (!messageDesc.isValid()) { addWarning(QObject::tr("Failed to find message description for unique id %1. " @@ -906,13 +917,13 @@ void QCanDbcFileParserPrivate::parseValueDescriptions(const QStringView data) m_lineOffset = match.capturedEnd(0); - bool ok = false; - const QtCanBus::UniqueId uid{match.capturedView(u"messageId"_s).toUInt(&ok)}; - if (!ok) { + const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s)); + if (!uidOptional) { addWarning(QObject::tr("Failed to parse value description from string %1").arg(data)); return; } + const QtCanBus::UniqueId uid = uidOptional.value(); // Check if the message exists const auto messageDesc = m_messageDescriptions.value(uid); if (!messageDesc.isValid()) { diff --git a/tests/auto/qcandbcfileparser/data/different_data_types.dbc b/tests/auto/qcandbcfileparser/data/different_data_types.dbc index 596f8d7..63e813c 100644 --- a/tests/auto/qcandbcfileparser/data/different_data_types.dbc +++ b/tests/auto/qcandbcfileparser/data/different_data_types.dbc @@ -3,9 +3,9 @@ BO_ 1234 Test : 7 Vector__XXX SG_ s1 : 12|12@1- (1,0) [0|0] "unit" Vector__XXX SG_ s2 : 24|32@1- (1,0) [0|0] "unit" Vector__XXX -BO_ 1235 Test1: 8 Vector__XXX +BO_ 2566844926 Test1: 8 Vector__XXX SG_ s3: 0|64@1+ (1,0) [0|0] "unit" Vector__XXX SIG_VALTYPE_ 1234 s1 : 0; SIG_VALTYPE_ 1234 s2: 1; -SIG_VALTYPE_ 1235 s3:2 ; +SIG_VALTYPE_ 2566844926 s3:2 ; diff --git a/tests/auto/qcandbcfileparser/data/extended_multiplexing.dbc b/tests/auto/qcandbcfileparser/data/extended_multiplexing.dbc index 47dbad6..0d170e4 100644 --- a/tests/auto/qcandbcfileparser/data/extended_multiplexing.dbc +++ b/tests/auto/qcandbcfileparser/data/extended_multiplexing.dbc @@ -44,7 +44,7 @@ BO_ 1234 Test: 3 Vector__XXX SG_ s0 M : 0|4@1+ (1,0) [0|0] "" Vector__XXX SG_ s5 : 16|8@1- (1,0) [0|0] "" Vector__XXX -BO_ 1235 Test: 3 Vector__XXX +BO_ 2566844926 Test: 3 Vector__XXX SG_ s4 m2 : 8|8@1- (1,0) [0|0] "" Vector__XXX SG_ s3 m1 : 8|8@1- (1,0) [0|0] "" Vector__XXX SG_ s2 m2 : 4|12@1- (1,0) [0|0] "" Vector__XXX @@ -82,10 +82,10 @@ SG_MUL_VAL_ 1234 s2 s0 2 -4 , 6- 8 , 10 -10 ; SG_MUL_VAL_ 1234 s1 s0 1-1, 5-5, 9-9; CM_ "Typo in mux switch name"; -SG_MUL_VAL_ 1235 s4 s11 2-3, 5-5; -SG_MUL_VAL_ 1235 s3 s1 1-1; -SG_MUL_VAL_ 1235 s2 s0 2-4, 6-8, 10-10; -SG_MUL_VAL_ 1235 s1 s0 1-1, 5-5, 9-9; +SG_MUL_VAL_ 2566844926 s4 s11 2-3, 5-5; +SG_MUL_VAL_ 2566844926 s3 s1 1-1; +SG_MUL_VAL_ 2566844926 s2 s0 2-4, 6-8, 10-10; +SG_MUL_VAL_ 2566844926 s1 s0 1-1, 5-5, 9-9; CM_ "Typo in multiplexed signal name"; SG_MUL_VAL_ 1236 s6 s1 2-3, 5-5; diff --git a/tests/auto/qcandbcfileparser/data/message_signals_in_one_line.dbc b/tests/auto/qcandbcfileparser/data/message_signals_in_one_line.dbc index 8f5c48b..8a81510 100644 --- a/tests/auto/qcandbcfileparser/data/message_signals_in_one_line.dbc +++ b/tests/auto/qcandbcfileparser/data/message_signals_in_one_line.dbc @@ -1,7 +1,7 @@ BO_ 1234 Test : 7 Vector__XXX SG_ s0 : 0|12@1+ (1,0) [0|0] "unit" Vector__XXX SG_ s1 : 12|12@1- (1,0) [0|0] "unit" Vector__XXX SG_ s2 : 24|32@1- (1,0) [0|0] "unit" Vector__XXX -BO_ 1235 Test1: 8 Vector__XXX SG_ s3: 0|64@1+ (1,0) [0|0] "unit" Vector__XXX +BO_ 2566844926 Test1: 8 Vector__XXX SG_ s3: 0|64@1+ (1,0) [0|0] "unit" Vector__XXX SIG_VALTYPE_ 1234 s1 : 0; SIG_VALTYPE_ 1234 s2 : 1 ; -SIG_VALTYPE_ 1235 s3 : 2; +SIG_VALTYPE_ 2566844926 s3 : 2; diff --git a/tests/auto/qcandbcfileparser/data/messages_with_comments.dbc b/tests/auto/qcandbcfileparser/data/messages_with_comments.dbc index 89e6283..850b138 100644 --- a/tests/auto/qcandbcfileparser/data/messages_with_comments.dbc +++ b/tests/auto/qcandbcfileparser/data/messages_with_comments.dbc @@ -36,7 +36,7 @@ BS_: BU_: -BO_ 1235 Test1: 1 Vector__XXX +BO_ 2566844926 Test1: 1 Vector__XXX SG_ s2 : 7|8@0+ (1,0) [0|0] "unit" Vector__XXX BO_ 1234 Test: 2 Vector__XXX @@ -45,8 +45,8 @@ BO_ 1234 Test: 2 Vector__XXX -CM_ BO_ 1235 "comment for Test1." ; -CM_ SG_ 1235 s2 "comment for s2" ; +CM_ BO_ 2566844926 "comment for Test1." ; +CM_ SG_ 2566844926 s2 "comment for s2" ; CM_ BO_ 1234 "Comment for message Test"; CM_ SG_ 1234 s1 "comment for s1" ; CM_ SG_ 1234 s0 "Comment for s0"; diff --git a/tests/auto/qcandbcfileparser/data/value_descriptions.dbc b/tests/auto/qcandbcfileparser/data/value_descriptions.dbc index 47cbfe4..f77acc1 100644 --- a/tests/auto/qcandbcfileparser/data/value_descriptions.dbc +++ b/tests/auto/qcandbcfileparser/data/value_descriptions.dbc @@ -39,7 +39,7 @@ VAL_TABLE_ ValueTable1 5 "blue" 4 "green" 3 "red" ; VAL_TABLE_ ValueTable0 2 "Description for the value '0x2'" 1 "Description for the value '0x1'" 0 "Description for the value '0x0'" ; -BO_ 1235 Test1: 2 Vector__XXX +BO_ 2566844926 Test1: 2 Vector__XXX SG_ s2 : 8|8@1+ (1,0) [0|0] "" Vector__XXX SG_ s1 : 0|8@1+ (1,0) [0|0] "" Vector__XXX @@ -48,13 +48,13 @@ BO_ 1234 Test: 2 Vector__XXX SG_ s0 : 0|8@1+ (1,0) [0|0] "" Vector__XXX -VAL_ 1235 s2 4 "Value4" 3 "Value3" 2 "Value2" 1 "Value1" 0 "Value0" ; -VAL_ 1235 s1 5 "b" 4 "g" 3 "r" ; +VAL_ 2566844926 s2 4 "Value4" 3 "Value3" 2 "Value2" 1 "Value1" 0 "Value0" ; +VAL_ 2566844926 s1 5 "b" 4 "g" 3 "r" ; VAL_ 1234 s1 5 "blue" 4 "green" 3 "red" ; VAL_ 1234 s0 2 "Description for the value '0x2'" 1 "Description for the value '0x1'" 0 "Description for the value '0x0'" ; CM_ "Invalid descriptions go here"; VAL_ 1236 s2 4 "Value4" 3 "Value3" 2 "Value2" 1 "Value1" 0 "Value0" ; -VAL_ 1235 s3 4 "Value4" 3 "Value3" 2 "Value2" 1 "Value1" 0 "Value0" ; -VAL_ 1235 s2 4 "Value4" 3 "Value3" 2 "Value2" 1 "Value1" 0 ; +VAL_ 2566844926 s3 4 "Value4" 3 "Value3" 2 "Value2" 1 "Value1" 0 "Value0" ; +VAL_ 2566844926 s2 4 "Value4" 3 "Value3" 2 "Value2" 1 "Value1" 0 ; diff --git a/tests/auto/qcandbcfileparser/tst_qcandbcfileparser.cpp b/tests/auto/qcandbcfileparser/tst_qcandbcfileparser.cpp index feeaa08..260da11 100644 --- a/tests/auto/qcandbcfileparser/tst_qcandbcfileparser.cpp +++ b/tests/auto/qcandbcfileparser/tst_qcandbcfileparser.cpp @@ -273,7 +273,7 @@ void tst_QCanDbcFileParser::parseFile_data() QCanMessageDescription doubleMessage = messageDesc; doubleMessage.clearSignalDescriptions(); doubleMessage.setName("Test1"); - doubleMessage.setUniqueId(QtCanBus::UniqueId{1235}); + doubleMessage.setUniqueId(QtCanBus::UniqueId{0x18fef1fe}); doubleMessage.setSize(8); signalDesc.setName("s3"); @@ -475,7 +475,7 @@ void tst_QCanDbcFileParser::parseFile_data() QCanMessageDescription doubleMessage = messageDesc; doubleMessage.clearSignalDescriptions(); doubleMessage.setName("Test1"); - doubleMessage.setUniqueId(QtCanBus::UniqueId{1235}); + doubleMessage.setUniqueId(QtCanBus::UniqueId{0x18fef1fe}); doubleMessage.setSize(8); signalDesc.setName("s3"); @@ -562,7 +562,7 @@ void tst_QCanDbcFileParser::parseFile_data() QCanMessageDescription otherDesc = messageDesc; otherDesc.clearSignalDescriptions(); otherDesc.setName("Test1"); - otherDesc.setUniqueId(QtCanBus::UniqueId{1235}); + otherDesc.setUniqueId(QtCanBus::UniqueId{0x18fef1fe}); otherDesc.setSize(1); otherDesc.setComment("comment for Test1."); @@ -711,10 +711,10 @@ void tst_QCanDbcFileParser::parseFile_data() u"Failed to find message description for unique id 12371. Skipping string " "SG_MUL_VAL_ 12371 s4 s1 2-3, 5-5;"_s, u"Failed to find signal description for signal s11. Skipping string " - "SG_MUL_VAL_ 1235 s4 s11 2-3, 5-5;"_s, + "SG_MUL_VAL_ 2566844926 s4 s11 2-3, 5-5;"_s, u"Failed to find signal description for signal s6. Skipping string " "SG_MUL_VAL_ 1236 s6 s1 2-3, 5-5;"_s, - u"Message description with unique id 1235 is skipped because it has invalid " + u"Message description with unique id 419361278 is skipped because it has invalid " "multiplexing description."_s, u"Message description with unique id 1236 is skipped because it has invalid " "multiplexing description."_s, @@ -971,15 +971,15 @@ void tst_QCanDbcFileParser::valueDescriptions() QCanDbcFileParser::MessageValueDescriptions expectedDescriptions; expectedDescriptions.insert(QtCanBus::UniqueId{1234}, test_value_descriptions); - expectedDescriptions.insert(QtCanBus::UniqueId{1235}, test1_value_descriptions); + expectedDescriptions.insert(QtCanBus::UniqueId{0x18fef1fe}, test1_value_descriptions); const QStringList expectedWarnings { u"Failed to find message description for unique id 1236. Skipping string " "VAL_ 1236 s2 4 \"Value4\" 3 \"Value3\" 2 \"Value2\" 1 \"Value1\" 0 \"Value0\" ;"_s, u"Failed to find signal description for signal s3. Skipping string " - "VAL_ 1235 s3 4 \"Value4\" 3 \"Value3\" 2 \"Value2\" 1 \"Value1\" 0 \"Value0\" ;"_s, + "VAL_ 2566844926 s3 4 \"Value4\" 3 \"Value3\" 2 \"Value2\" 1 \"Value1\" 0 \"Value0\" ;"_s, u"Failed to parse value description from string " - "VAL_ 1235 s2 4 \"Value4\" 3 \"Value3\" 2 \"Value2\" 1 \"Value1\" 0 ;"_s + "VAL_ 2566844926 s2 4 \"Value4\" 3 \"Value3\" 2 \"Value2\" 1 \"Value1\" 0 ;"_s }; const QString fileName = u"value_descriptions.dbc"_s; |