Fix locale dependent number parsing#1229
Merged
bcoconni merged 8 commits intoJSBSim-Team:masterfrom Feb 22, 2025
Merged
Conversation
3 tasks
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1229 +/- ##
==========================================
+ Coverage 24.75% 24.76% +0.01%
==========================================
Files 170 170
Lines 19457 19464 +7
==========================================
+ Hits 4816 4820 +4
- Misses 14641 14644 +3 ☔ View full report in Codecov by Sentry. |
…this should be decided at an upper level.
bcoconni
added a commit
to bcoconni/jsbsim
that referenced
this pull request
Feb 22, 2025
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.
Following the issue #1227, this PR is making the function
atof_locale_cmuch more pedantic and rejects any number that do not match a strict syntax such as[+-]0.12E[+-]34or12.34. A regular expression is used for that purpose (they have been introduced in C++11).1. New exception
InvalidNumerA new exception class
JSBSim::InvalidNumberhas been created which inherits fromJSBSim::BaseException. Since the former is defined ininput_output/string_utilities.hand the latter inFGJSBBase.h, it was no longer possible thatFGJSBBase.hincludesstring_utilities.hbecause that was creating a mutual dependence that compilers complain about. ActuallyFGJSBBase.hdoes not need to includestring_utilities.hso the corresponding#includehas simply been removed fromFGJSBBase.h. As a consequence, a number of source files have been modified to explicitly includestring_utilities.h.2.
atof_locale_cpedantryThe function
is_numberwill now be much more pedantic and will basically be a function that checks thatatof_locale_csucceeds.All the occurrences of the following code:
have been replaced by
The problem was that both
is_numberandstrtod_lhave a relaxed interpretation of what a valid number syntax is. For examplestrtod_l("1.3.2", nullptr, numeric_c.Locale);returns1.3and the stringsxand.return0.0. In addition the strings1.3.2and.are considered valid numbers byis_numberalthough it rejectsx. The regular expression will allow rejecting these strings as invalid.Once this PR will be merged, all the validation and conversion work will be made by
atof_locale_cwhich is now extremely pedantic. Also since the PR #799,atof_locale_cuses the locale "C" for numerical value whatever is the setting ofLC_NUMERIC.3. Removal of calls to
atofMost of the calls to
atofhave been replaced byatof_locale_c. The remaining calls toatofare all located insimger/props/props.cxx:jsbsim/src/simgear/props/props.cxx
Line 1434 in 0ac6194
jsbsim/src/simgear/props/props.cxx
Line 1770 in 0ac6194
jsbsim/src/simgear/props/props.cxx
Line 1822 in 0ac6194
These calls are respectively made in the methods
SGPropertyNode::getFloatValue,SGPropertyNode::setStringValueandSGPropertyNode::setUnspecifiedValuewhich are not used in the code.4. Remaining calls to
strtofunctionsSome calls to the standard library functions starting with
strto(such asstrtol,strtod, ...) remain.jsbsim/src/simgear/props/props.cxx
Line 1401 in 0ac6194
jsbsim/src/simgear/props/props.cxx
Line 1767 in 0ac6194
jsbsim/src/simgear/props/props.cxx
Line 1773 in 0ac6194
jsbsim/src/simgear/props/props.cxx
Line 1819 in 0ac6194
jsbsim/src/simgear/props/props.cxx
Line 1825 in 0ac6194
These calls are respectively made in the methods
SGPropertyNode::getLongValue,SGPropertyNode::setStringValueandSGPropertyNode::setUnspecifiedValuewhich are not used in the code.The following call to
strtodis made inSGPropertyNode::getLongValue:jsbsim/src/simgear/props/props.cxx
Lines 1466 to 1468 in 0ac6194
However for that case to be selected, the property should have been previously set by
SGPropertyNode::setStringValueorSGPropertyNode::setUnspecifiedValuewhich we are not using.The following call to
strtoulis made insimgear/xml/xmlparse.cjsbsim/src/simgear/xml/xmlparse.c
Line 8533 in 0ac6194
The function
getDebugLevelis internally used byxmlparse.cto get the value of the environment variablesEXPAT_ENTROPY_DEBUG,EXPAT_ACCOUNTING_DEBUGandEXPAT_ENTITY_DEBUG. These are used internally by Expat for debug purposes. It is unlikely that they are related to the issue #1227.Conclusion
atofhave been removed and replaced byatof_locale_c. For the record, the behavior ofatofdepends on the environment variableLC_NUMERIC.atofandstrtofunctions remain but they are located in functions which we do not use.simgear/props/props.cxxand I am reluctant to useatof_locale_cin this piece of code. If we want to be super safe, I'd rather remove the aforementioned functions fromSGPropertyNodesince we are not using them anyway.