tbeu / matio

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

Mat_GetDir incorrect behavior after first evaluation #110

Closed rafal-c closed 5 years ago

rafal-c commented 5 years ago

In the code below:

size_t n;
char** names = Mat_GetDir(matFile, &n); 

names and n will have correct values when this code is evaluated for the first time for a given matFile but all subsequent evaluations will leave n uninitialized.

This is because apart from the first evaluation of Mat_GetDir the check in mat.c:579 will be false and we jump directly to mat.c:649 which only initializes dir but n is untouched through the whole function execution path. Adding *n = mat->num_datasets; in L650 is the simplest fix that comes to mind.

tbeu commented 5 years ago

Unfortunately the proposed fix is not sufficient, since there can be variables that are excluded by the directory, see e.g., https://github.com/tbeu/matio/blob/8523c5ed5e03c0523f6ecd1f407fc8a128e8ab29/src/mat73.c#L2711-L2713

tbeu commented 5 years ago

Resolved by e697a0a8a238b773f20b432c3a58dd4f53f85e62. Thanks for reporting.