Skip to content

Commit

Permalink
Merge branch 'fix-crash-on-message-line'
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxDevon committed May 26, 2024
2 parents 0dbe454 + 1bfead3 commit e123a0b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/dbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void DbcParser::parse_dbc_messages(const std::vector<std::string>& lines) {
continue;
}

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

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

Expand Down
36 changes: 36 additions & 0 deletions test/test_parse_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,39 @@ TEST_CASE("Parse Message data length < 8 unsigned") {
REQUIRE(Catch::Approx(result_values.at(0)) == 0x1);
REQUIRE(Catch::Approx(result_values.at(1)) == 0x2);
}

TEST_CASE("Parse message with BO_ on single line should fail.") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_
234 MSG1: 8 Vector__XXX
SG_ State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3
VAL_ 234 State1 123 "Description 1" 0 "Description 2" 90903489 "Big value and special characters &$§())!")";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());

Libdbc::DbcParser p;
p.parse_file(filename);

std::vector<uint8_t> data{0x1, 0x2};
std::vector<double> result_values;
REQUIRE(p.get_messages().size() == 0);
REQUIRE(p.parse_message(234, data, result_values) == Libdbc::Message::ParseSignalsStatus::ErrorUnknownID);
}

TEST_CASE("Parse signal with SG_ on single line should fail.") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 8 Vector__XXX
SG_
State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3
VAL_ 234 State1 123 "Description 1" 0 "Description 2" 90903489 "Big value and special characters &$§())!")";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());

Libdbc::DbcParser p;
p.parse_file(filename);

std::vector<uint8_t> data{0x1, 0x2};
std::vector<double> result_values;
REQUIRE(p.get_messages().size() == 1);
REQUIRE(p.parse_message(234, data, result_values) == Libdbc::Message::ParseSignalsStatus::Success);
REQUIRE(result_values.size() == 1);
REQUIRE(Catch::Approx(result_values.at(0)) == 0x1);
}

0 comments on commit e123a0b

Please sign in to comment.