Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 Fix regexes and memory leaks #47

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ComponentFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ namespace nts {
_components = std::unordered_map<std::string, IComponent *>();
}

ComponentFactory::~ComponentFactory() = default;
ComponentFactory::~ComponentFactory()
{
for (auto &component : _components) {
delete component.second;
}
}

void ComponentFactory::registerComponent(const std::string &name, IComponent *component)
{
Expand Down
14 changes: 11 additions & 3 deletions src/FileContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ nts::FileContainer::FileContainer(const std::string &filename)
this->_pins = std::map<std::string, IComponent *>();
}

nts::FileContainer::~FileContainer()
{
for (auto &pin : this->_pins) {
delete pin.second;
}
}

void nts::FileContainer::extractFileContent()
{
ssize_t count;
Expand All @@ -44,6 +51,7 @@ void nts::FileContainer::extractFileContent()
content = removeComments(content);
this->extractChipsetsAndLinks(content);
close(fd);
delete[] buffer;
}

std::vector<std::string> nts::FileContainer::getChipsets(void) const
Expand Down Expand Up @@ -129,7 +137,7 @@ void nts::FileContainer::extractChipsetsAndLinks(const std::string &content)

void nts::FileContainer::fillChipsets(std::string &str)
{
std::regex reg("^([a-zA-Z0-9]+)\\s+(\\w+)(\\s+)?");
std::regex reg("^\\s*([a-zA-Z0-9]+)\\s+(\\w+)\\s*(#.*)?$");
std::smatch match2;
char *token;
std::string str2;
Expand All @@ -152,7 +160,7 @@ void nts::FileContainer::fillChipsets(std::string &str)

void nts::FileContainer::fillLinks(std::string &str)
{
std::regex reg("^([a-zA-Z0-9_]+)((\\s+)?:(\\s+)?)([0-9]+)\\s+([a-zA-Z0-9_]+)((\\s+)?:(\\s+)?)([0-9]+)( +)?");
std::regex reg("^\\s*([a-zA-Z0-9_]+)\\s*:\\s*([0-9]+)\\s+([a-zA-Z0-9_]+)\\s*:\\s*([0-9]+)\\s*(#.*)?$");
std::smatch match2;
char *token;
std::string str2;
Expand All @@ -164,7 +172,7 @@ void nts::FileContainer::fillLinks(std::string &str)
while (token != NULL) {
str2 = std::string(token);
if (std::regex_search(str2, match2, reg)) {
str2 = match2[1].str() + " " + match2[5].str() + " " + match2[6].str() + " " + match2[10].str();
str2 = match2[1].str() + " " + match2[2].str() + " " + match2[3].str() + " " + match2[4].str();
this->_links.push_back(str2);
} else {
throw nts::Error("Invalid file format in the links section.");
Expand Down
2 changes: 1 addition & 1 deletion src/FileContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace nts
class FileContainer {
public:
FileContainer(const std::string &filename);
~FileContainer() = default;
~FileContainer();
void extractFileContent();

std::vector<std::string> getChipsets(void) const;
Expand Down
3 changes: 2 additions & 1 deletion src/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ void nts::Simulation::loop()
this->_loop = true;
handleLoop(this);
while (this->_loop) {
sigaction(SIGINT, &sigIntHandler, NULL);
simulate();
display();
sigaction(SIGINT, &sigIntHandler, NULL);
display();
}
sigIntHandler.sa_handler = SIG_DFL;
sigaction(SIGINT, &sigIntHandler, NULL);
Expand Down
6 changes: 6 additions & 0 deletions src/components/ComposedComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ using namespace nts::Components;
ComposedComponent::ComposedComponent(std::size_t nbPins, std::size_t nbInternals) : AbstractComponent(nbPins) {
this->_internal.reserve(nbInternals);
}

ComposedComponent::~ComposedComponent() {
for (auto &internal : this->_internal) {
delete internal.second;
}
}
2 changes: 1 addition & 1 deletion src/components/ComposedComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ namespace nts::Components {

ComposedComponent(std::size_t nbPins, std::size_t nbInternals);
public:
~ComposedComponent() override = default;
~ComposedComponent() override;
};
}
Loading