From 9af53e272ef2bf35600f7614e0a2224f305471d6 Mon Sep 17 00:00:00 2001 From: zvon Date: Sun, 6 Jan 2019 20:40:16 +0100 Subject: [PATCH] Fix possible memory leak, split FSLib::Directory into cpp and hpp parts --- filesystem.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++- filesystem.hpp | 56 +++++++++++------------------------------------- 2 files changed, 70 insertions(+), 44 deletions(-) diff --git a/filesystem.cpp b/filesystem.cpp index 6832be0..a7a1301 100644 --- a/filesystem.cpp +++ b/filesystem.cpp @@ -16,8 +16,10 @@ bool FSLib::isDirectory(const std::string &path) { std::string FSLib::canonical(const std::string &path) { char *canonical_path = static_cast(malloc(PATH_MAX)); - if( realpath(path.c_str(), canonical_path) == nullptr ) + if( realpath(path.c_str(), canonical_path) == nullptr ) { + free(canonical_path); return ""; + } std::string canonical_string{canonical_path}; free(canonical_path); return canonical_string; @@ -27,3 +29,57 @@ bool FSLib::rename(const std::string &file_a, const std::string &file_b) { return ::rename( file_a.c_str(), file_b.c_str() ) == 0; } +FSLib::Directory::iterator FSLib::Directory::begin() { + return Iterator(dir_path); +} + +FSLib::Directory::const_iterator FSLib::Directory::begin() const { + return Iterator(dir_path); +} + +FSLib::Directory::const_iterator FSLib::Directory::cbegin() const { + return begin(); +} + +FSLib::Directory::iterator FSLib::Directory::end() { + return Iterator(dir_path, nullptr); +} + +FSLib::Directory::const_iterator FSLib::Directory::end() const { + return Iterator(dir_path, nullptr); +} + +FSLib::Directory::const_iterator FSLib::Directory::cend() const { + return end(); +} + +const char *FSLib::Directory::path() const { + return dir_path.c_str(); +} + +std::string FSLib::Directory::Iterator::operator*() { + return current_entry->d_name; +} + +FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() { + if( current_entry == nullptr ) + return *this; + current_entry = readdir(d); + if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) ) + operator++(); + return *this; +} + +FSLib::Directory::Iterator FSLib::Directory::Iterator::operator++(int) { + Iterator ret(*this); + operator++(); + return ret; +} + +bool FSLib::Directory::Iterator::operator==(const Iterator &i_other) { + return i_other.current_entry == current_entry; +} + +bool FSLib::Directory::Iterator::operator!=(const Iterator &i_other) { + return i_other.current_entry != current_entry; +} diff --git a/filesystem.hpp b/filesystem.hpp index 6f5d62c..bbc8151 100644 --- a/filesystem.hpp +++ b/filesystem.hpp @@ -36,32 +36,15 @@ namespace FSLib{ closedir(d); } - std::string operator*() { - return current_entry->d_name; - } + std::string operator*(); - Iterator &operator++() { - if( current_entry == nullptr ) - return *this; - current_entry = readdir(d); - if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) ) - operator++(); - return *this; - } + Iterator &operator++(); - Iterator operator++(int) { - Iterator ret(*this); - operator++(); - return ret; - } + Iterator operator++(int); - bool operator==(const Iterator &i_other) { - return i_other.current_entry == current_entry; - } + bool operator==(const Iterator &i_other); - bool operator!=(const Iterator &i_other) { - return i_other.current_entry != current_entry; - } + bool operator!=(const Iterator &i_other); private: DIR *d; @@ -71,33 +54,20 @@ namespace FSLib{ using iterator = Iterator; using const_iterator = Iterator; - iterator begin() { - return Iterator(dir_path); - } + iterator begin(); - const_iterator begin() const { - return Iterator(dir_path); - } + const_iterator begin() const; - const_iterator cbegin() const { - return begin(); - } + const_iterator cbegin() const; - iterator end() { - return Iterator(dir_path, nullptr); - } + iterator end(); - const_iterator end() const { - return Iterator(dir_path, nullptr); - } + const_iterator end() const; - const_iterator cend() const { - return end(); - } + const_iterator cend() const; + + const char *path() const; - const char *path() const { - return dir_path.c_str(); - } private: std::string dir_path; };