inifile: Fix non-reentrant Find() returning pointer to static buffer #3763
+304
−324
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
IniFile::Find()returned a pointer to a static buffer, causing reentrancy bugs when multipleFind()calls were made before using the first result:The code had a comment acknowledging this issue:
Solution
Changed
IniFile::Find()to returnstd::optional<std::string>instead ofstd::optional<const char*>. Each caller now owns their own copy of the result.Before
After
Changes Made
Core API Changes
inifile.hh: ChangedFind()signature to returnstd::optional<std::string>inifile.cc: Removed static buffer, returnstd::stringby valueemcIniFile.hh: Updated wrapper class to match new return typeCaller Updates (20 files)
All callers updated to use the new API:
s->c_str()when passing to C APIs requiringconst char**s == "value"instead ofstrcmp()for string comparisonss->find_first_of()instead ofstrchr()for character searchess->length()instead ofstrlen()s->empty()instead of checkings[0]Migration Guide
String Output
String Comparison
Character Search
C API Compatibility
The legacy C function
iniFind()maintains backward compatibility but still uses static storage:Important: C code calling
iniFind()must still copy the result immediately if it needs to be preserved. This preserves the original C API behavior for backward compatibility.Benefits
Build Status
✅ Compiles successfully with no warnings or errors