dimanche 22 mai 2022

Cannot access member variable in method call

I'm building a parser for PE files and I've encountered a somewhat weird error. Here is the minimal example

#include <iostream>
#include <vector>
#include <string.h>
#include <istream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <cstdint>


class BinaryDataSegment {
protected:
    std::vector<uint8_t>* binary_data;   
    int data_size;

public:   
    BinaryDataSegment(){
        data_size = 0;
    }

    BinaryDataSegment(std::istream& field_data, int reserve_size) {
        binary_data = new std::vector<uint8_t>();
        binary_data->reserve(reserve_size);
        std::copy(std::istream_iterator<uint8_t>(field_data), std::istream_iterator<uint8_t>(), std::back_inserter(*binary_data));
        data_size = binary_data->size();
    }

    template<typename D>
    D Read(int position){
        if (position + sizeof(D) > data_size){
            throw std::overflow_error("attempting to read outsite binary data");
        }
        D temp;
        std::cout << binary_data->data()+position << std::endl; // Segmentation fault here: binary_data is not accessible
        memcpy(&temp, binary_data->data()+position, sizeof(D));
        return temp;
    }

    ~BinaryDataSegment(){
        delete binary_data;
    }
};

class PEFile {
protected:
    BinaryDataSegment data;

public:
    PEFile(std::istream& file_stream, int reserve=0){
        data = BinaryDataSegment(file_stream, reserve);
    }
    int dos_header;

    void parse(){
        dos_header = data.Read<int>(0);
    }
};

int main(){
    std::ifstream in_file = std::ifstream("anyFile.txt", std::ios::binary);
    PEFile foo = PEFile(in_file, 0);
    foo.parse(); // Segmentation fault

    BinaryDataSegment bar = BinaryDataSegment(in_file, 0);
    bar.Read<int>(0); // This works correctly
    
}

When I call the Read<int> function from main, it works correctly, but when I do it from the PEFile class, the same function call fails with a Segmentation fault. GDB says that the memory of binary_data is not accessible, even though it looks like it should be. My question is: why does this unexpected behavior happen?

I compiled without warnings using:

g++ -Wall -std=c++11 example.cpp

When running the example, make sure to replace "anyFile.txt" with a real local file. Thank you.

Aucun commentaire:

Enregistrer un commentaire