-
Notifications
You must be signed in to change notification settings - Fork 473
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
Fix locale dependent number parsing #1229
Merged
Merged
Conversation
This file contains 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
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.
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_c
much more pedantic and rejects any number that do not match a strict syntax such as[+-]0.12E[+-]34
or12.34
. A regular expression is used for that purpose (they have been introduced in C++11).1. New exception
InvalidNumer
A new exception class
JSBSim::InvalidNumber
has been created which inherits fromJSBSim::BaseException
. Since the former is defined ininput_output/string_utilities.h
and the latter inFGJSBBase.h
, it was no longer possible thatFGJSBBase.h
includesstring_utilities.h
because that was creating a mutual dependence that compilers complain about. ActuallyFGJSBBase.h
does not need to includestring_utilities.h
so the corresponding#include
has simply been removed fromFGJSBBase.h
. As a consequence, a number of source files have been modified to explicitly includestring_utilities.h
.2.
atof_locale_c
pedantryThe function
is_number
will now be much more pedantic and will basically be a function that checks thatatof_locale_c
succeeds.All the occurrences of the following code:
have been replaced by
The problem was that both
is_number
andstrtod_l
have a relaxed interpretation of what a valid number syntax is. For examplestrtod_l("1.3.2", nullptr, numeric_c.Locale);
returns1.3
and the stringsx
and.
return0.0
. In addition the strings1.3.2
and.
are considered valid numbers byis_number
although 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_c
which is now extremely pedantic. Also since the PR #799,atof_locale_c
uses the locale "C" for numerical value whatever is the setting ofLC_NUMERIC
.3. Removal of calls to
atof
Most of the calls to
atof
have been replaced byatof_locale_c
. The remaining calls toatof
are 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::setStringValue
andSGPropertyNode::setUnspecifiedValue
which are not used in the code.4. Remaining calls to
strto
functionsSome 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::setStringValue
andSGPropertyNode::setUnspecifiedValue
which are not used in the code.The following call to
strtod
is 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::setStringValue
orSGPropertyNode::setUnspecifiedValue
which we are not using.The following call to
strtoul
is made insimgear/xml/xmlparse.c
jsbsim/src/simgear/xml/xmlparse.c
Line 8533 in 0ac6194
The function
getDebugLevel
is internally used byxmlparse.c
to get the value of the environment variablesEXPAT_ENTROPY_DEBUG
,EXPAT_ACCOUNTING_DEBUG
andEXPAT_ENTITY_DEBUG
. These are used internally by Expat for debug purposes. It is unlikely that they are related to the issue #1227.Conclusion
atof
have been removed and replaced byatof_locale_c
. For the record, the behavior ofatof
depends on the environment variableLC_NUMERIC
.atof
andstrto
functions remain but they are located in functions which we do not use.simgear/props/props.cxx
and I am reluctant to useatof_locale_c
in this piece of code. If we want to be super safe, I'd rather remove the aforementioned functions fromSGPropertyNode
since we are not using them anyway.