Commit c81442af authored by fs's avatar fs Committed by Commit bot

Stricter float-to-int conversion in SVGIntegerOptionalInteger

SVGIntegerOptionalInteger parses values as floats but stores them as
integers. Add helpers to avoid issues with overflow and to make this
conversion the same way in all places it's needed.
The "normal" parsing code would truncate the float value while the parsing
code for animation values would round. Make them both use truncation (and the
avoid duplicating the code.)

BUG=675130

Review-Url: https://codereview.chromium.org/2590433002
Cr-Commit-Position: refs/heads/master@{#439533}
parent effa7da6
<!DOCTYPE html>
<title>SVGIntegerOptionalInteger value range</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
test(function() {
var element = document.createElementNS('http://www.w3.org/2000/svg', 'feConvolveMatrix');
element.setAttribute('order', '2147483648');
assert_equals(element.orderX.baseVal, 2147483647);
element.setAttribute('order', '-2147483649');
assert_equals(element.orderX.baseVal, -2147483648);
}, document.title+', outside range of int.');
test(function() {
var element = document.createElementNS('http://www.w3.org/2000/svg', 'feConvolveMatrix');
element.setAttribute('order', '2147483578');
assert_equals(element.orderX.baseVal, 2147483647);
}, document.title+', not exactly representable as int.');
</script>
......@@ -52,17 +52,10 @@ SVGIntegerOptionalInteger* SVGIntegerOptionalInteger::clone() const {
SVGPropertyBase* SVGIntegerOptionalInteger::cloneForAnimation(
const String& value) const {
float floatX, floatY;
if (!parseNumberOptionalNumber(value, floatX, floatY)) {
return SVGIntegerOptionalInteger::create(SVGInteger::create(0),
SVGInteger::create(0));
}
int x = static_cast<int>(roundf(floatX));
int y = static_cast<int>(roundf(floatY));
return SVGIntegerOptionalInteger::create(SVGInteger::create(x),
SVGInteger::create(y));
SVGIntegerOptionalInteger* clone =
create(SVGInteger::create(0), SVGInteger::create(0));
clone->setValueAsString(value);
return clone;
}
String SVGIntegerOptionalInteger::valueAsString() const {
......@@ -83,8 +76,8 @@ SVGParsingError SVGIntegerOptionalInteger::setValueAsString(
x = y = 0;
}
m_firstInteger->setValue(x);
m_secondInteger->setValue(y);
m_firstInteger->setValue(clampTo<int>(x));
m_secondInteger->setValue(clampTo<int>(y));
return parseStatus;
}
......@@ -125,8 +118,8 @@ void SVGIntegerOptionalInteger::calculateAnimatedValue(
percentage, repeatCount, fromInteger->secondInteger()->value(),
toInteger->secondInteger()->value(),
toAtEndOfDurationInteger->secondInteger()->value(), y);
m_firstInteger->setValue(static_cast<int>(roundf(x)));
m_secondInteger->setValue(static_cast<int>(roundf(y)));
m_firstInteger->setValue(clampTo<int>(roundf(x)));
m_secondInteger->setValue(clampTo<int>(roundf(y)));
}
float SVGIntegerOptionalInteger::calculateDistance(SVGPropertyBase* other,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment