cxong / tinydir

Lightweight, portable and easy to integrate C directory and file reader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tinydir_file_open crashes with debug msvcr

staticlibs opened this issue · comments

Hi,

The following code crashes when compiled on MSVS2013 with debug version of C runtime lib:

#define UNICODE
#define _UNICODE
#include "tinydir.h"

int main() {
    tinydir_file fi;
    tinydir_file_open(&fi, L".");
    (void) fi;
    return 0;
}

Compilation command:

>cl test.c /MDd
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.40629 for x86

I can see that something is wrong with this call -

_tsplitpath_s(
but it is not clear what is wrong.

_splitpath and _splitpath_s can only handle full path correctly, but you give it ".". So the path is broken.
I will try to fix this.

You are using tinydir_file_open, which is for opening a single file. But "." in Windows should be a folder. It's not recommended. Instead you can use tinydir_open.
And tinydir_open can handle path ".".

commented

It would be best if tinydir_file_open detected this and returned an informative error like ENOENT or EINVAL (if . is never a valid filename).

@cxong Though "." may not be a valid file name, it could be good to return 0 as file.is_dir is set.
The point is _splitpath_s.
Here in VS 2010, _splitpath_s will make drive_buf and dir_name_buf full of messy code when using widechar, but not gcc.
So we need to fix this.

@cxong Sorry to say that although _splitpath_s is not working fine, the tinydir_file_open itself are buggy. Whatever path it is, tinydir_file_open will always exit with Run-Time Failure #2 with _splitpath_s and widechar. Don't know why.

@lautis0503 , about tinydir_open - I want to open a file and check whether it is a regular file or directory, get its name etc. tinydir_file_open is probably a right method for it.

Header updated with #40 worked for me in smoke testing after fixing the typo on line 630 (unneeded sizeof near ext_buf). Though specified sizes are probably incorrect there as they exceed the ones allowed in _wsplitpath_s - https://msdn.microsoft.com/en-us/library/8e46eyt7.aspx#Anchor_3 , from there - "File components larger than the corresponding manifest constants cause heap corruption."

Thanks for the fix!

@staticlibs It is the right method.
@cxong _TINYDIR_PATH_MAX is too large for _splitpath. Maybe we need to write a function manually. What's your opinion?

commented

I guess _TINYDIR_PATH_MAX should be MAX_PATH instead for windows, which is 260 and doesn't break _splitpath. I originally used 4096 just because it was the limit for linux and didn't think it hurt to have it larger than necessary.

OK. I've done that.