c-ridgway / cpp-feather-ini-parser

Header only, simple, fast, templated - portable INI parser for ANSI C++.
MIT License
47 stars 14 forks source link

Crash on DOS-style CR+LF line endings #1

Closed oncer closed 10 years ago

oncer commented 10 years ago

The parser crashes on empty lines with CR+LF ending. The reason is this code below: if (!(fini_strlen(line) >= 2 && line[0] == '/' && line[1] == '/')) //Ignore comment An empty line with CR+LF will have strlen == 2 and therefore it tries to parse the line. Later in the code you have skey = fini_strtok(line, _T("=")); svalue = fini_strtok(NULL, _T("\n")); which will result in skey: "" and svalue: NULL When the parser tries to access svalue, for example calling strlen, the program will throw an exception, trying to dereference the NULL pointer.

Turbine1991 commented 10 years ago

I've pushed a fix for this issue, could you please re-test this issue and post your success here.

oncer commented 10 years ago

Thanks for the quick reply.

I'm not sure why, but file.getline(line, FINI_BUFFER_SIZE); actually strips the \r\n away for me. The last line of my INI file is empty, and for that line it returns a string with a single \n, causing the crash. So the bug report wasn't entirely accurate in the first place, sorry about that. I fixed the issue on my end by checking for NULL here:

skey = fini_strtok(line, _T("="));
svalue = fini_strtok(NULL, _T("\n"));

if (skey && svalue)
{
   ....
Turbine1991 commented 10 years ago

Thought it was worth a wild quick try, this is actually a regression from a previously working version, hence it removes the carriage return correctly.

Yeah I mixed my order of operator precedence there, I should have used more parenthesis for clarity anyway.

oncer commented 10 years ago

I should probably mention that I'm on MSVC2010, so the STL methods might behave a little different here.

Turbine1991 commented 10 years ago

It has caused me grief in the past with some of the differences here, however as I never use Windows/DOS line endings, I wouldn't notice. The last time was when I created a text file in notepad, notepad++ was much nicer to edit .ini files with, not to mention converting between them.