tbeu / matio

MATLAB MAT File I/O Library
https://matio.sourceforge.io
BSD 2-Clause "Simplified" License
338 stars 97 forks source link

Mat_VarReadNextInfo requires a large chunk on a 32KB file #190

Closed frokaikan closed 11 months ago

frokaikan commented 2 years ago

Here is the program

#include <cstring>
#include <cstdlib>
#include <cstdio>

#include "matio.h"

int main (int argc, char **argv) {
    mat_t *ptr = Mat_Open(argv[1], 0);
    auto *var = Mat_VarReadNextInfo(ptr);
    Mat_VarFree(var);
    Mat_Close(ptr);
}

And here is the file (zipped), testfile.zip , which is a 32-KB file Mat_VarReadNextInfo allocates 4278583297 bytes (about 4GB). Maybe there can be a better implementation?

tbeu commented 2 years ago

With logging enabled I get

Unexpected end-of-file: Read 32599 bytes, expected 4278583296 bytes

Could it be that you missed to write the MAT file completely.

frokaikan commented 2 years ago

With logging enabled I get

Unexpected end-of-file: Read 32599 bytes, expected 4278583296 bytes

Could it be that you missed to write the MAT file completely.

Ah, I also got this message. However, the message is printed AFTER the chunk has been allocated. I think the too-large chunk should not be even allocated. Actually, I compile the program with AddressSanitizer and run it with ASAN_OPTIONS=max_allocation_size_mb=3000 , then ASAN reports the error...

tbeu commented 11 months ago

Addressed by https://github.com/tbeu/matio/commit/0adfda6e129c2cedcae89a95918792e62fcd9bbd.