From 6e582907c0977b1719cc370917ea22af805149a1 Mon Sep 17 00:00:00 2001 From: Lukas Steiner Date: Thu, 15 Jun 2023 11:32:51 +0200 Subject: [PATCH] Add checks for empty trace and catch conversion exceptions. --- src/simulator/simulator/player/StlPlayer.cpp | 118 ++++++++++--------- 1 file changed, 65 insertions(+), 53 deletions(-) diff --git a/src/simulator/simulator/player/StlPlayer.cpp b/src/simulator/simulator/player/StlPlayer.cpp index d80a6157..3cfca1e7 100644 --- a/src/simulator/simulator/player/StlPlayer.cpp +++ b/src/simulator/simulator/player/StlPlayer.cpp @@ -71,6 +71,9 @@ StlPlayer::StlPlayer(std::string_view tracePath, if (line.size() > 1 && line[0] != '#') numberOfLines++; } + if (numberOfLines == 0) + SC_REPORT_FATAL("StlPlayer", + (std::string("Empty trace ") + tracePath.data()).c_str()); traceFile.clear(); traceFile.seekg(0); } @@ -143,69 +146,78 @@ void StlPlayer::parseTraceFile() iss.str(line); - // Get the timestamp for the transaction. - iss >> element; - if (element.empty()) - SC_REPORT_FATAL( - "StlPlayer", - ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); - - content.delay = playerPeriod * static_cast(std::stoull(element)); - - // Get the optional burst length and command - iss >> element; - if (element.empty()) - SC_REPORT_FATAL( - "StlPlayer", - ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); - - if (element.at(0) == '(') + try { - element.erase(0, 1); - content.length = std::stoul(element); + // Get the timestamp for the transaction. iss >> element; if (element.empty()) SC_REPORT_FATAL( - "StlPlayer", - ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); - } - else - content.length = defaultDataLength; + "StlPlayer", + ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); - if (element == "read") - content.command = Request::Command::Read; - else if (element == "write") - content.command = Request::Command::Write; - else - SC_REPORT_FATAL( - "StlPlayer", - ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); + content.delay = playerPeriod * static_cast(std::stoull(element)); - // Get the address. - iss >> element; - if (element.empty()) - SC_REPORT_FATAL( - "StlPlayer", - ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); - content.address = std::stoull(element, nullptr, 16); - - // Get the data if necessary. - if (storageEnabled && content.command == Request::Command::Write) - { - // The input trace file must provide the data to be stored into the memory. + // Get the optional burst length and command iss >> element; - - // Check if data length in the trace file is correct. - // We need two characters to represent 1 byte in hexadecimal. Offset for 0x prefix. - if (element.length() != (content.length * 2 + 2)) + if (element.empty()) SC_REPORT_FATAL( + "StlPlayer", + ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); + + if (element.at(0) == '(') + { + element.erase(0, 1); + content.length = std::stoul(element); + iss >> element; + if (element.empty()) + SC_REPORT_FATAL( + "StlPlayer", + ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); + } + else + content.length = defaultDataLength; + + if (element == "read") + content.command = Request::Command::Read; + else if (element == "write") + content.command = Request::Command::Write; + else + SC_REPORT_FATAL( + "StlPlayer", + ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); + + // Get the address. + iss >> element; + if (element.empty()) + SC_REPORT_FATAL( + "StlPlayer", + ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); + content.address = std::stoull(element, nullptr, 16); + + // Get the data if necessary. + if (storageEnabled && content.command == Request::Command::Write) + { + // The input trace file must provide the data to be stored into the memory. + iss >> element; + + // Check if data length in the trace file is correct. + // We need two characters to represent 1 byte in hexadecimal. Offset for 0x prefix. + if (element.length() != (content.length * 2 + 2)) + SC_REPORT_FATAL( + "StlPlayer", + ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); + + // Set data + for (unsigned i = 0; i < content.length; i++) + content.data.emplace_back(static_cast( + std::stoi(element.substr(i * 2 + 2, 2), nullptr, 16))); + } + } + catch (...) + { + SC_REPORT_FATAL( "StlPlayer", ("Malformed trace file line " + std::to_string(currentLine) + ".").c_str()); - - // Set data - for (unsigned i = 0; i < content.length; i++) - content.data.emplace_back(static_cast( - std::stoi(element.substr(i * 2 + 2, 2), nullptr, 16))); } } }