Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix memory leak
  • Loading branch information
romainthomas committed Sep 28, 2017
1 parent cd0f1e0 commit 88dafa8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 132 deletions.
2 changes: 2 additions & 0 deletions include/LIEF/ELF/DataHandler/Handler.hpp
Expand Up @@ -18,13 +18,15 @@

#include "LIEF/ELF/DataHandler/Node.hpp"
#include "LIEF/visibility.h"
#include "LIEF/utils.hpp"
#include <vector>

namespace LIEF {
namespace ELF {
namespace DataHandler {
class DLL_PUBLIC Handler {
public:
static constexpr size_t MAX_SIZE = 10_GB;
Handler(const std::vector<uint8_t>& content);
Handler(std::vector<uint8_t>&& content);
~Handler(void);
Expand Down
16 changes: 16 additions & 0 deletions include/LIEF/utils.hpp
Expand Up @@ -41,6 +41,22 @@ inline uint64_t round<uint64_t>(uint64_t x) {
}


constexpr size_t operator ""_KB(unsigned long long kbs)
{
return 1024 * kbs;
}

constexpr size_t operator ""_MB(unsigned long long mbs)
{
return 1024 * 1024 * mbs;
}

constexpr size_t operator ""_GB(unsigned long long gbs)
{
return 1024 * 1024 * 1024 * gbs;
}


}

#endif
3 changes: 3 additions & 0 deletions src/ELF/DataHandler/Handler.cpp
Expand Up @@ -117,6 +117,9 @@ void Handler::make_hole(uint64_t offset, uint64_t size) {


void Handler::reserve(uint64_t offset, uint64_t size) {
if ((offset + size) > Handler::MAX_SIZE) {
throw std::bad_alloc();
}
if (this->data_.size() < (offset + size)) {
this->data_.resize((offset + size), 0);
}
Expand Down
54 changes: 29 additions & 25 deletions src/ELF/Parser.cpp
Expand Up @@ -61,34 +61,38 @@ Parser::Parser(const std::string& file, DYNSYM_COUNT_METHODS count_mtd) :

void Parser::init(const std::string& name) {
VLOG(VDEBUG) << "Parsing binary: " << name << std::endl;
this->binary_ = new Binary{};
this->binary_->original_size_ = this->binary_size_;
this->binary_->name(name);
this->binary_->datahandler_ = new DataHandler::Handler{this->stream_->content()};

this->type_ = reinterpret_cast<const Elf32_Ehdr*>(
this->stream_->read(0, sizeof(Elf32_Ehdr)))->e_ident[IDENTITY::EI_CLASS];

this->binary_->type_ = static_cast<ELF_CLASS>(this->type_);

switch (this->binary_->type_) {
case ELFCLASS32:
{
this->parse_binary<ELF32>();
break;
}
try {
this->binary_ = new Binary{};
this->binary_->original_size_ = this->binary_size_;
this->binary_->name(name);
this->binary_->datahandler_ = new DataHandler::Handler{this->stream_->content()};

this->type_ = reinterpret_cast<const Elf32_Ehdr*>(
this->stream_->read(0, sizeof(Elf32_Ehdr)))->e_ident[IDENTITY::EI_CLASS];

this->binary_->type_ = static_cast<ELF_CLASS>(this->type_);
switch (this->binary_->type_) {
case ELFCLASS32:
{
this->parse_binary<ELF32>();
break;
}

case ELFCLASS64:
{
this->parse_binary<ELF64>();
break;
}
case ELFCLASS64:
{
this->parse_binary<ELF64>();
break;
}

case ELFCLASSNONE:
default:
LOG(WARNING) << "e_ident[EI_CLASS] seems corrupted." << std::endl;
//TODO try to guess with e_machine
throw LIEF::corrupted("e_ident[EI_CLASS] corrupted");
case ELFCLASSNONE:
default:
//TODO try to guess with e_machine
throw LIEF::corrupted("e_ident[EI_CLASS] corrupted");
}
} catch (const std::exception& e) {
LOG(WARNING) << e.what();
//delete this->binary_;
}
}

Expand Down

0 comments on commit 88dafa8

Please sign in to comment.