From 52ea92dcf8121df3d529d9e2e30b1db4aa2771a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Fri, 11 May 2018 15:52:38 +0200 Subject: [PATCH] Fix various problems in PC list handling --- src/CodeGenerator.cpp | 4 ++++ src/CodeGenerator.hpp | 2 ++ src/PcListReader.cpp | 5 ++++- src/PcListReader.hpp | 4 ++-- src/main.cpp | 3 ++- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/CodeGenerator.cpp b/src/CodeGenerator.cpp index 7dbbd76..c1e2dc6 100644 --- a/src/CodeGenerator.cpp +++ b/src/CodeGenerator.cpp @@ -19,6 +19,7 @@ CodeGenerator::CodeGenerator( { if(!settings::pc_list.empty()) { pc_list = make_unique(settings::pc_list); + pc_list->read(); } } @@ -133,6 +134,9 @@ void CodeGenerator::gen_case(uintptr_t low_bound, uintptr_t high_bound) { pc_list->get_list().end(), high_bound); + if(first_it == pc_list->get_list().end()) + throw CodeGenerator::InvalidPcList(); + os << std::hex; for(auto it = first_it; it != last_it; ++it) os << "\t\tcase 0x" << *it << ":\n"; diff --git a/src/CodeGenerator.hpp b/src/CodeGenerator.hpp index 696c603..7669459 100644 --- a/src/CodeGenerator.hpp +++ b/src/CodeGenerator.hpp @@ -18,6 +18,8 @@ class CodeGenerator { /// Unimplemented case class NotImplementedCase: public std::exception {}; + class InvalidPcList: public std::exception {}; + /** Create a CodeGenerator to generate code for the given dwarf, on the * given std::ostream object (eg. cout). */ CodeGenerator(const SimpleDwarf& dwarf, std::ostream& os, diff --git a/src/PcListReader.cpp b/src/PcListReader.cpp index 1359d31..8c8edc0 100644 --- a/src/PcListReader.cpp +++ b/src/PcListReader.cpp @@ -18,8 +18,11 @@ void PcListReader::read() { while(!handle.eof()) { handle.read((char*)buffer, 8); - if(handle.gcount() != 8) + if(handle.gcount() != 8) { + if(handle.gcount() == 0) + break; throw PcListReader::BadFormat(); + } last_pc = 0; for(int shift = 0; shift < 8; ++shift) { diff --git a/src/PcListReader.hpp b/src/PcListReader.hpp index e30daf3..55db70d 100644 --- a/src/PcListReader.hpp +++ b/src/PcListReader.hpp @@ -11,10 +11,10 @@ class PcListReader { public: /// Thrown when the file is somehow not readable - class CannotReadFile: std::exception {}; + class CannotReadFile: public std::exception {}; /// Thrown when the file contains bad content (probably not aligned 8B) - class BadFormat: std::exception {}; + class BadFormat: public std::exception {}; PcListReader(const std::string& path); diff --git a/src/main.cpp b/src/main.cpp index 52640d6..81adc8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,7 +73,8 @@ MainOptions options_parse(int argc, char** argv) { if(print_helptext) { cerr << "Usage: " << argv[0] - << " [--switch-per-func | --global-switch] elf_path" + << " [--switch-per-func | --global-switch] " + << "[--pc-list PC_LIST_FILE] elf_path" << endl; } if(exit_status >= 0)