Make nsTString_CharT::ToDouble/ToFloat output an error code if the string doesn't parse to a finite number
Categories
(Core :: XPCOM, defect, P3)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox109 | --- | fixed |
People
(Reporter: jwatt, Assigned: nika)
Details
Attachments
(3 files, 2 obsolete files)
Updated•13 years ago
|
| Reporter | ||
Comment 1•13 years ago
|
||
Comment 2•13 years ago
|
||
Comment 3•13 years ago
|
||
Comment 4•13 years ago
|
||
Comment 6•13 years ago
|
||
| Reporter | ||
Comment 7•13 years ago
|
||
Comment 8•13 years ago
|
||
Comment 9•13 years ago
|
||
| Reporter | ||
Comment 10•13 years ago
|
||
Comment 11•13 years ago
|
||
| Reporter | ||
Updated•12 years ago
|
| Reporter | ||
Comment 12•12 years ago
|
||
Comment 13•12 years ago
|
||
| Assignee | ||
Comment 14•3 years ago
|
||
I ran into this again while working on bug 1772006. The HTML standard for parsing floating point numbers specifies that values rounding to infinity should produce an error rather than +/-inf (https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values), which actually has an impact on our behaviour (e.g. <progress value=1e309 max=10> is complete in Firefox as value parses to +inf, and indeterminate in Chromium). I have some local patches which change our parsing over to use double-conversion::StringToDoubleConverter to be more in-line with the standard, change the error handling to return NaN in most cases, and which make +-inf an error.
While looking into this, I discovered one location which depends on the existing edge-case behaviour right now: preference handling. ToFloat is called on a string and the result is unconditionally assigned into aResult: 1, 2. Many callers of preference APIs, including preferences themselves, assume that the outparameter won't be assigned to in error cases, meaning that for invalid floating point values, whatever value was returned is observed.
Unfortunately just changing us to not clobber the outparam on error also doesn't work perfectly, as preferences also appears to depend on infinity being an OK result. Tests like this set a preference to Number.MAX_VALUE (DBL_MAX) which is outside the range of valid float values, and thus becomes positive infinity when read.
Given that we don't really control prefs and users could theoretically depend on the parsing behaviour for invalid float preferences in some way or another, I'm somewhat reluctant to change how we parse these values. Preference float parsing should likely continue to be built on PR_strtod for a bit longer.
| Assignee | ||
Comment 15•3 years ago
|
||
The annotations added in bug 1770158 only covered StringToDouble,
however as noted in the comments in that file, that isn't sufficient for
accurately parsing floating point numbers. This patch extends the
annotations to the single-precision floating point numbers as well and
will be used in the next part to clean up the implementation of
nsTString::ToDouble.
Depends on D148303
| Assignee | ||
Comment 16•3 years ago
|
||
The main use of this method appears to be when parsing attributes with floating
point values in HTML. By changing from PR_strtod, this change does have some
semantic differences around edge-cases, though I've tried to keep it compliant
with the wording in the standard.
Here are some of the edge-cases I investigated:
- We appear to build nspr with INFNAN_CHECK undefined, meaning that we did not
parse strings like "inf", "infinity" and "nan" both before and after this
change. - PR_strtod already ignored locale (built with USE_LOCALE undefined), so
locale behaviour shouldn't have changed. - Neither PR_strtod nor the spec support hex floats, so I didn't enable them
in double-conversion. - Leading whitespace is skipped by both PR_strtod and in the spec, so is
skipped after this change. - Numbers too large to fit in a double (e.g. 2E308) were previously returned
as inf or -inf by PR_strtod. The spec specifies that these values should return
an error instead, so they have been changed to produce an error with this
change.
This is a web-visible change (<progress value=2E308 max=10> is indeterminate
in Chromium and complete in Firefox before this change, it will be
indeterminate for both after the change). - Parsing for floats & doubles are now handled seperately due to
differences in the maximum and minimum representable values.
Depends on D148304
| Assignee | ||
Comment 17•3 years ago
|
||
These can be set by users, and callers often don't do a great job of
handling errors, so maintaining the existing error behaviour is our
safest option here. In addition, there are some tests which set prefs to
very large values, resulting in inf being used as the returned
preference value.
In the future it may be nice to make the error handling behaviour act
more consistently with other branches (not clobbering the outparam on
error), though that was left out from this change.
Depends on D148305
Updated•3 years ago
|
Updated•3 years ago
|
Comment 18•3 years ago
|
||
There are some r+ patches which didn't land and no activity in this bug for 2 weeks.
:jwatt, could you have a look please?
If you still have some work to do, you can add an action "Plan Changes" in Phabricator.
For more information, please visit auto_nag documentation.
Comment 19•3 years ago
|
||
:nika, IIUC the patches are yours? Do you still plan to land them?
Comment 20•3 years ago
|
||
Comment 21•3 years ago
|
||
| bugherder | ||
https://hg.mozilla.org/mozilla-central/rev/ba5d28166805
https://hg.mozilla.org/mozilla-central/rev/d32ab1fc40ed
https://hg.mozilla.org/mozilla-central/rev/00327070cdc6
| Assignee | ||
Comment 22•3 years ago
|
||
(In reply to Jens Stutte [:jstutte] from comment #19)
:nika, IIUC the patches are yours? Do you still plan to land them?
slipped through the cracks and landed now.
Updated•3 years ago
|
Updated•3 years ago
|
Description
•