abduld/libwb

The system fails when reading CSV files created on Windows

jacool opened this issue · 3 comments

The issue is with the wbFile_read() function in wbFile.cpp. When Windows-formatted CSV files are used the file has \r\n line endings, the files are being opened in the default text mode, which means they are replaced by \n in the buffer. As a result the size of the res=fread() is less than the count (file length), which kills the function. The following comment and amendment can be applied for a quick fix, though they remove the assertion, so probably it is better to perform binary reads taking into account \r\n EOLs.

/* if (res != count) {
wbLog(ERROR, "Failed to read data from ", wbFile_getFileName(file));
wbDelete(buffer);
return NULL;
}
buffer[bufferlen - 1] = '\0'; // make valid C string
*/

buffer[size * res] = '\0'; // make valid C string

your fix introduces a buffer overflow

I don't see how, since res is always equal or less than count (by definition of fread). What it indeed does sometimes is to put the terminating 0 before the end of the buffer, which is a valid trick, as long as you treat the buffer as string only.

ok fixed