Skip to content

Commit e123a0b

Browse files
committed
Merge branch 'fix-crash-on-message-line'
2 parents 0dbe454 + 1bfead3 commit e123a0b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/dbc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void DbcParser::parse_dbc_messages(const std::vector<std::string>& lines) {
181181
continue;
182182
}
183183

184-
if (std::regex_search(line, match, signal_re)) {
184+
if (std::regex_search(line, match, signal_re) && messages.size() > 0) {
185185
std::string name = match.str(SIGNAL_NAME_GROUP);
186186
bool is_multiplexed = false; // No support yet
187187
uint32_t start_bit = static_cast<uint32_t>(std::stoul(match.str(SIGNAL_START_BIT_GROUP)));
@@ -204,7 +204,7 @@ void DbcParser::parse_dbc_messages(const std::vector<std::string>& lines) {
204204
continue;
205205
}
206206

207-
if (std::regex_search(line, match, value_re)) {
207+
if (std::regex_search(line, match, value_re) && messages.size() > 0) {
208208
uint32_t message_id = static_cast<uint32_t>(std::stoul(match.str(2)));
209209
std::string signal_name = match.str(3);
210210

test/test_parse_message.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,39 @@ TEST_CASE("Parse Message data length < 8 unsigned") {
173173
REQUIRE(Catch::Approx(result_values.at(0)) == 0x1);
174174
REQUIRE(Catch::Approx(result_values.at(1)) == 0x2);
175175
}
176+
177+
TEST_CASE("Parse message with BO_ on single line should fail.") {
178+
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_
179+
234 MSG1: 8 Vector__XXX
180+
SG_ State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
181+
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3
182+
VAL_ 234 State1 123 "Description 1" 0 "Description 2" 90903489 "Big value and special characters &$§())!")";
183+
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
184+
185+
Libdbc::DbcParser p;
186+
p.parse_file(filename);
187+
188+
std::vector<uint8_t> data{0x1, 0x2};
189+
std::vector<double> result_values;
190+
REQUIRE(p.get_messages().size() == 0);
191+
REQUIRE(p.parse_message(234, data, result_values) == Libdbc::Message::ParseSignalsStatus::ErrorUnknownID);
192+
}
193+
194+
TEST_CASE("Parse signal with SG_ on single line should fail.") {
195+
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 8 Vector__XXX
196+
SG_
197+
State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
198+
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3
199+
VAL_ 234 State1 123 "Description 1" 0 "Description 2" 90903489 "Big value and special characters &$§())!")";
200+
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
201+
202+
Libdbc::DbcParser p;
203+
p.parse_file(filename);
204+
205+
std::vector<uint8_t> data{0x1, 0x2};
206+
std::vector<double> result_values;
207+
REQUIRE(p.get_messages().size() == 1);
208+
REQUIRE(p.parse_message(234, data, result_values) == Libdbc::Message::ParseSignalsStatus::Success);
209+
REQUIRE(result_values.size() == 1);
210+
REQUIRE(Catch::Approx(result_values.at(0)) == 0x1);
211+
}

0 commit comments

Comments
 (0)