graphitemaster / incbin

Include binary files in C/C++
The Unlicense
966 stars 90 forks source link

Binary data is empty (contains no characters) #51

Closed ewancg closed 2 years ago

ewancg commented 2 years ago

In a situation where the file is included properly and no compile-time errors are encountered, trying to refer to the Data variable will yield nothing.

...

#define INCBIN_PREFIX r_
#include "lib/incbin/incbin.h"
INCBIN(grc, "./data.tar");

...

std::vector<Resource::fileContents> Resource::getFileList() {

    char *tmp = reinterpret_cast<char *>(tarData);
    tmp[r_grcSize + 1] = '\0';

    mtar_mem_stream_t e;
    e.data = tmp;
    e.size = r_grcSize;
    e.pos = 0;

    mtar_open_mem(&tarball, &e);
//    mtar_open(&tarball, "./data.tar", "r"); // this works
    std::vector<Resource::fileContents> list;
    mtar_header_t h;
    while ((mtar_read_header(&tarball, &h)) != MTAR_ENULLRECORD) {
        list.push_back(Resource::fileContents{h.name, h.size});
        mtar_next(&tarball);
    }
    return list;
}

Resource::Resource() {
    tarData = calloc(1, r_grcSize);
    memcpy(tarData, r_grcData, r_grcSize);
    fileList = getFileList();
}

This code crashes because r_grcData is empty. I am using GCC 11 and glibc. The tar processing code works perfectly and does everything I want it to do when using it's normal filename-based open function. The memory stream counterpart is given a direct copy of the r_grcData variable, which should contain the tar file all the same, but doesn't.

ewancg commented 2 years ago

A minimal reproduction worked with the same compiler and settings.

#define INCBIN_PREFIX r_
#include "incbin.h"
#include <iostream>
INCBIN(grc, "./test.txt");

int main(int argc, char *argv[]) {
    std::cout << r_grcData;
}

I'm not sure how else this problem could be caused, but it certainly isn't your fault. I'm closing this issue.