Skip to content

Commit 1bfead3

Browse files
committed
fix: Crashed when BO_ and the message content were on different lines
1 parent 6d62885 commit 1bfead3

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
@@ -176,7 +176,7 @@ void DbcParser::parse_dbc_messages(const std::vector<std::string>& lines) {
176176
continue;
177177
}
178178

179-
if (std::regex_search(line, match, signal_re)) {
179+
if (std::regex_search(line, match, signal_re) && messages.size() > 0) {
180180
std::string name = match.str(SIGNAL_NAME_GROUP);
181181
bool is_multiplexed = false; // No support yet
182182
uint32_t start_bit = static_cast<uint32_t>(std::stoul(match.str(SIGNAL_START_BIT_GROUP)));
@@ -199,7 +199,7 @@ void DbcParser::parse_dbc_messages(const std::vector<std::string>& lines) {
199199
continue;
200200
}
201201

202-
if (std::regex_search(line, match, value_re)) {
202+
if (std::regex_search(line, match, value_re) && messages.size() > 0) {
203203
uint32_t message_id = static_cast<uint32_t>(std::stoul(match.str(2)));
204204
std::string signal_name = match.str(3);
205205

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)