Skip to content

Commit

Permalink
Revert "Switch to using exceptions for memory safety"
Browse files Browse the repository at this point in the history
TIL throwing exceptions during signal handling was an undefined
behaviour

This reverts commit f3c8a7e.
  • Loading branch information
DolphyWind committed May 26, 2023
1 parent f3c8a7e commit da37e51
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 124 deletions.
2 changes: 1 addition & 1 deletion include/Argparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SOFTWARE.

#define ELECTRA_VERSION_MAJOR 2
#define ELECTRA_VERSION_MINOR 0
#define ELECTRA_VERSION_PATCH 3
#define ELECTRA_VERSION_PATCH 2

#include <string>
#include <vector>
Expand Down
1 change: 0 additions & 1 deletion include/Bomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ SOFTWARE.
#include <Cable.hpp>
#include <direction.hpp>
#include <Electra.hpp>
#include <SilentException.hpp>

// Ends the program.
class Bomb : public Cable
Expand Down
7 changes: 6 additions & 1 deletion include/Electra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ SOFTWARE.
#pragma once
#include <iostream>
#include <vector>
#include <sstream>
#include <Current.hpp>
#include <Component.hpp>
#include <Generator.hpp>
Expand Down Expand Up @@ -155,6 +154,12 @@ class Electra
/// - Create new currents
void mainLoop();

/// @brief cleans up maps, vectors etc.
void cleanup();

/// @brief Safely exits the program
void safe_exit(int exit_code);

// Methods for mainLoop() method.
void moveCurrents();
void generateGenerators();
Expand Down
35 changes: 0 additions & 35 deletions include/SilentException.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/Bomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ bool Bomb::work(CurrentPtr current, std::vector<CurrentPtr> *currentVector)
if(!Component::work(current, currentVector))
return false;

throw SilentException();
Electra::instance().safe_exit(0);
return Cable::work(current, currentVector);
}
78 changes: 48 additions & 30 deletions src/Electra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ void Electra::initialize(int argc, char* argv[])
{
parser.printHelpMessage();
defaultlogger.log(LogType::INFO, L"Printed help message. Exiting with code 0.");
throw SilentException();
safe_exit(0);
}

if(bool_map["version"])
{
parser.printVersionMessage();
defaultlogger.log(LogType::INFO, L"Printed current version of electra. Exiting with code 0.");
throw SilentException();
safe_exit(0);
}

if(alone_args.size() == 0)
{
parser.printHelpMessage();
defaultlogger.log(LogType::INFO, L"No arguments specified. Printing help message. Exiting with code 1.");
throw SilentException(1);
safe_exit(1);
}

std::string stack_count_str = string_map["stack-count"];
Expand All @@ -93,16 +93,14 @@ void Electra::initialize(int argc, char* argv[])
catch (const std::invalid_argument &e)
{
defaultlogger.log(LogType::ERROR, L"\"{}\" is invalid for stack-count.\n");
std::stringstream ss;
ss << '\"' << stack_count_str << "\" is invalid for stack-count.";
throw std::runtime_error(ss.str());
std::wcerr << L'\"' << std::to_wstring(stack_count_str) << L"\" is invalid for stack-count.\n";
safe_exit(1);
}
catch (const std::out_of_range &e)
{
defaultlogger.log(LogType::ERROR, L"\"{}\" is out of range for stack-count.", stack_count_str);
std::stringstream ss;
ss << '\"' << stack_count_str << "\" is out of range for stack-count.";
throw std::runtime_error(ss.str());
std::wcerr << L'\"' << std::to_wstring(stack_count_str) << L"\" is out of range for stack-count." << std::endl;
safe_exit(1);
}

// Parses --stack argument
Expand All @@ -112,10 +110,9 @@ void Electra::initialize(int argc, char* argv[])
auto splitted_by_comma = Global::split(string_map["stack"], ",");
if(splitted_by_comma.size() > m_stacks.size())
{
std::stringstream ss;
ss << "You've entered inital values for " << splitted_by_comma.size() << " stacks but stack count is " << m_stacks.size() << "!";
std::wcerr << L"You entered inital values for " << splitted_by_comma.size() << L" stacks but stack count is " << m_stacks.size() << L"!" << std::endl;
defaultlogger.log(LogType::ERROR, L"You entered inital values for {} stacks but stack count is {}!", splitted_by_comma.size(), m_stacks.size());
throw std::runtime_error(ss.str());
safe_exit(1);
}
for(auto &splitted : splitted_by_comma)
{
Expand All @@ -130,16 +127,14 @@ void Electra::initialize(int argc, char* argv[])
catch(const std::out_of_range &e)
{
defaultlogger.log(LogType::ERROR, L"The value {} is too big or small for var_t.", i);
std::stringstream ss;
ss << "The value " << i << " is too big or small for var_t.";
throw std::runtime_error(ss.str());
std::wcerr << L"The value " << std::to_wstring(i) << L" is too big or small for var_t." << std::endl;
safe_exit(1);
}
catch(const std::invalid_argument &e)
{
defaultlogger.log(LogType::ERROR, L"Can\'t convert {} to var_t.", i);
std::stringstream ss;
ss << "Can\'t convert " << i << " to var_t.";
throw std::runtime_error(ss.str());
std::wcerr << L"Can\'t convert " << std::to_wstring(i) << L" to var_t." << std::endl;
safe_exit(1);
}
}
index ++;
Expand Down Expand Up @@ -303,6 +298,7 @@ Electra &Electra::instance()

Electra::~Electra()
{
cleanup();
}

void Electra::run()
Expand Down Expand Up @@ -335,13 +331,37 @@ void Electra::mainLoop()
defaultlogger.log(LogType::INFO, L"Program finished. Total ticks: {}", tickCount);
}

void Electra::cleanup()
{
m_components.clear();
m_generatorDataMap.clear();
m_generatorChars.clear();
m_generators.clear();
m_currents.clear();
m_filename.clear();
m_currentPath.clear();
m_sourceCode.clear();
m_includedParts.clear();
m_deadCurrentIndexes.clear();
m_newCurrents.clear();
m_stacks.clear();
m_portalMap.clear();
}

void Electra::safe_exit(int exit_code)
{
cleanup();
std::exit(exit_code);
}

std::vector<std::wstring> Electra::includeFile(fs::path currentPath, const std::wstring& filename, std::size_t start, std::size_t end, bool allow_reinclusion)
{
// Start cannot be greater then the end
if(start >= end)
{
std::wcerr << L"Inclusion failed: Start index must be less than the end index." << std::endl;
defaultlogger.log(LogType::ERROR, L"Inclusion failed: Start index must be less than the end index.");
throw std::runtime_error("Inclusion failed: Start index must be less than the end index.");
safe_exit(1);
}

if(!allow_reinclusion)
Expand Down Expand Up @@ -382,7 +402,8 @@ std::vector<std::wstring> Electra::includeFile(fs::path currentPath, const std::
if(fileData.find(L'\t') != std::string::npos)
{
defaultlogger.log(LogType::ERROR, L"Cannot parse \"{}\". Source code contains tab character. Exiting with code 1.", filename);
throw std::runtime_error("Error while reading file: Source code contains tab!");
std::wcerr << "Error while reading file: Source code contains tab!" << std::endl;
safe_exit(1);
}

// Split by the new line and slice file according to given parameters
Expand Down Expand Up @@ -424,10 +445,9 @@ std::vector<std::wstring> Electra::includeFile(fs::path currentPath, const std::
}
catch (const std::exception &e)
{
std::wcerr << L"Cannot convert \"" << split_from_colon.at(0) << "\" to a number." << std::endl;
defaultlogger.log(LogType::ERROR, L"Cannot convert \"{}\" to a number.", split_from_colon.at(0));
std::stringstream ss;
ss << "Cannot convert \"" << wstring_converter.to_bytes(split_from_colon.at(0)) << "\" to a number.";
throw std::runtime_error(ss.str());
safe_exit(1);
}

try
Expand All @@ -437,10 +457,9 @@ std::vector<std::wstring> Electra::includeFile(fs::path currentPath, const std::
}
catch (const std::exception &e)
{
std::wcerr << L"Cannot convert \"" << split_from_colon.at(1) << "\" to a number." << std::endl;
defaultlogger.log(LogType::ERROR, L"Cannot convert \"{}\" to a number.", split_from_colon.at(1));
std::stringstream ss;
ss << "Cannot convert \"" << wstring_converter.to_bytes(split_from_colon.at(1)) << "\" to a number.";
throw std::runtime_error(ss.str());
safe_exit(1);
}
}

Expand All @@ -455,10 +474,9 @@ std::vector<std::wstring> Electra::includeFile(fs::path currentPath, const std::
}
else
{
std::wcerr << L"Cannot open \"" << filename << L"\"" << std::endl;
defaultlogger.log(LogType::ERROR, L"Cannot open \"{}\". Exiting with code 1.", filename);
std::stringstream ss;
ss << "Cannot open \"" << wstring_converter.to_bytes(filename) << "\"";
throw std::runtime_error(ss.str());
safe_exit(1);
}

return contents;
Expand Down Expand Up @@ -660,5 +678,5 @@ void Electra::createCurrents()

void Electra::sigHandler(int signal)
{
throw SilentException();
Electra::instance().safe_exit(0);
}
34 changes: 0 additions & 34 deletions src/SilentException.cpp

This file was deleted.

24 changes: 3 additions & 21 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,7 @@ int main(int argc, char *argv[])
#endif
std::wcout << std::setprecision(15);

try
{
Electra::instance().initialize(argc, argv);
Electra::instance().run();
}
catch (const SilentException& be)
{
// Just end the program
return be.getExitCode();
}
catch (const std::runtime_error &re)
{
std::wcerr << L"Error: " << re.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception &e)
{
std::wcerr << L"An unexpected error has occured: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
Electra::instance().initialize(argc, argv);
Electra::instance().run();
return 0;
}

0 comments on commit da37e51

Please sign in to comment.