-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let iniparser_getstring()
not return NULL
if there is a section (not key) with specified name
#139
base: master
Are you sure you want to change the base?
Conversation
- iniparser_getstring() returns `NULL` on some condition and it can cause crash
Returning "default" instead on NULL in the case of "section" breaks completely functions iniparser_dumpsection_ini() and iniparser_dump_ini() because iniparser_find_entry() will no longer work for sections (line 303 of iniparser.c). Possibly the best we can do is to return "another_one_default" from iniparser_getstring() function in the case of section. |
- Fix iniparser_find_entry() - Return (null) in NULL case(section / key with NULL value)
@dofuuz Please have a look at #125 (comment) |
@lmoellendorf What this PR is different from #146 is, In my opinion, returning NULL can be potential problem, so returning "(null)" string is more safe option. |
Maybe I am missing something, but I do not understand why parsing a returned pointer for As far as I know all standard functions which return pointers, return |
@dofuuz Thank you for the clarification. |
@lmoellendorf Even authors of iniparser missed the null check and made CVE entry (CVE-2023-33461). #125 #144 Making an API Hard to Misuse is good principle of making a library. |
The CVE reads [emphasizes by me]:
This was in internal bug, which is fixed. I get your argumentation. Programmers tend to forget parameter and return value validation. But I would rather not resort to returning an arbitrary string instead of
If so, I would rather suggest an API like this: /*-------------------------------------------------------------------------*/
/**
@brief Get the string associated to a key
@param[out] val Pointer to statically allocated character string
@param d Dictionary to search
@param key Key string to look for
@param def Default value to return if key not found.
@return 0 on success, -1 else
This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
the pointer passed as 'def' is returned.
The returned char pointer is pointing to a string allocated in
the dictionary, do not free or modify it.
*/
/*--------------------------------------------------------------------------*/
int iniparser_getstring(const char **val, const dictionary * d, const char * key, const char * def); ...or I would return a pointer to a statically allocated character string, denoting an empty string like it is done in protobuf-c. Making this string part of the API allows for a simple pointer check as used to with This will have to be discussed for v5.x. |
iniparser_getstring()
not return NULL
if there is a section (not key) with specified name
Returning "(null)" is not so wrong. With common compilers (including gcc, msvc),
prints So here is my sugesstion (it's modified from the latter option of your previous comment)
This will allow explicit pointer check without changing API. |
- to allow explicit pointer check - Add more explicit NULL check to be sure
iniparser_getboolean()
can segfault on malformed config files. #125There are similar problem in
iniparser_getlongint()
andiniparser_getdouble()
.iniparser_getstring()
returnsNULL
if there are section(not key) with that name.The key does not exists, so it's better to return default fallback.
And returning
NULL
for "string" is not likely to be considered by programmers, which can be security problem.Fixes #125