Fix UB and locale-dependent behavior in StringHelper::toLowerCase#687
Open
metsw24-max wants to merge 1 commit into
Open
Fix UB and locale-dependent behavior in StringHelper::toLowerCase#687metsw24-max wants to merge 1 commit into
metsw24-max wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix undefined behavior and locale-dependent output in
StringHelper::toLowerCase.The previous implementation passed
LogStringcharacters directly to::tolower(int)viastd::transform. This violates the C standard requirement that the argument must either beEOFor representable asunsigned char.When
logchar = char(commonly signed), any byte greater than0x7Fsign-extends to a negativeint, triggering undefined behavior. The behavior also depended on the activeLC_CTYPElocale, causing the same configuration file to produce different lowercased values on different systems.This patch replaces the locale-sensitive transformation with deterministic ASCII-only folding (
A-Z -> a-z) while preserving all non-ASCII bytes unchanged.Changes
src/main/cpp/stringhelper.cppReplaced:
std::transform(..., tolower)With:
Eliminates UB from invalid
tolowerinputsRemoves locale-dependent behavior
Preserves non-ASCII bytes unchanged
src/test/cpp/helpers/stringhelpertestcase.cppAdded regression coverage:
testToLowerCaseAsciitestToLowerCaseNonAsciiPassesThroughReproducer
With the original implementation:
testToLowerCaseNonAsciiPassesThroughfails on systems using locales such asEnglish_India.1252Example:
0xC9(É) may be transformed into0xE9(é) through locale-sensitivetolowerWith this patch:
stringhelpertestcasetests passpatternparsertestcasealso passes unchanged