I recently found a bug in error handling of iniparser_load
. Look at Line 732 of iniparser.c
! That break
jumps out of the switch
, not the while
loop processing the lines. errs
is set, but processing will go on, errs
will be overwritten if a following line is syntactically correct, so the returned dictionary won't be NULL
.
Currently there is no way that the programmer could detect the syntax error, only the user can (stderr
). Look at Commit 8ffcea6f516270f84ef7716c983fd2982ecd9b91 pointing that out.
I didn't create a pull request for this as I don't know what error handling strategies would I follow.
- Stop at first syntax error, free everything and return
NULL
.
- Detect and print all syntax errors, free everything and return
NULL
.
- Print syntax error, but return a valid dictionary.
The current implementation varies between 2 and 3. If there was a valid Section or Value line after syntax error, it's 3, otherwise 2.
I provided a fix resulting in 1 (e78613270c28891215b2fa2a0b21914e9ee78175), but that's just ugly. Bad thing is, the break
s are chaotic in iniparser_load
. I can rewrite it using goto
instead of random breaks, either implementing strategy 1 or 2, like this, if necessary.
It is your job to decide the strategy, feel free to cherry-pick these commits if you want strategy 1.
There is another huge problem in iniparser_load
about error handling: it writes the message to stderr
. This is just fine for CLI programs written in C, but consider the following:
- GUI applications that have
stderr
omitted, and would like show the message to the user.
- C++ wrapper for iniparser. In C++ it is usually better to throw something derived from
std::runtime_error
containing the message instead of arbitarily printing to stdout
.
So most C libraries have const char * getlasterror()
to handle the problem, and this gives a workaround for both problems. You can see my approach: 83c45bf29452b1fdbe4181d2ae9dbb7605b2b7df
Note that this isn't perfect either. For example it contains possibility of buffer overflows, that could be solved by snprintf
. Bad news: snprintf
is not part of the C89 standard, and only the latest MSVC compilers have the C99 compatible version...
This change has only a small impact on the API, so it can be changed without breaking code based on iniparser. More bad news: CLI programs might expect that iniparser writes the errors to stderr
.
A possible solution: logging both into stderr
and ERRORBUFFER
by default, and adding void iniparser_stderr(int enabled)
function that would toggle printing to stderr
.
This is another question that You should decide.
dobragab