Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: third_party/WebKit/Source/core/css/CSSCalculationValue.cpp

Issue 2597103002: Prevent floating point overflow when using calc() with large values (Closed)
Patch Set: Remove layout affected test case Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 bool isZero() const override { return !m_value->getDoubleValue(); } 178 bool isZero() const override { return !m_value->getDoubleValue(); }
179 179
180 String customCSSText() const override { return m_value->cssText(); } 180 String customCSSText() const override { return m_value->cssText(); }
181 181
182 void accumulatePixelsAndPercent( 182 void accumulatePixelsAndPercent(
183 const CSSToLengthConversionData& conversionData, 183 const CSSToLengthConversionData& conversionData,
184 PixelsAndPercent& value, 184 PixelsAndPercent& value,
185 float multiplier) const override { 185 float multiplier) const override {
186 switch (m_category) { 186 switch (m_category) {
187 case CalcLength: 187 case CalcLength:
188 value.pixels += 188 value.pixels = clampTo<float>(
189 m_value->computeLength<float>(conversionData) * multiplier; 189 value.pixels +
190 m_value->computeLength<double>(conversionData) * multiplier);
190 break; 191 break;
191 case CalcPercent: 192 case CalcPercent:
192 ASSERT(m_value->isPercentage()); 193 ASSERT(m_value->isPercentage());
193 value.percent += m_value->getDoubleValue() * multiplier; 194 value.percent = clampTo<float>(value.percent +
195 m_value->getDoubleValue() * multiplier);
194 break; 196 break;
195 case CalcNumber: 197 case CalcNumber:
196 // TODO(alancutter): Stop treating numbers like pixels unconditionally 198 // TODO(alancutter): Stop treating numbers like pixels unconditionally
197 // in calcs to be able to accomodate border-image-width 199 // in calcs to be able to accomodate border-image-width
198 // https://drafts.csswg.org/css-backgrounds-3/#the-border-image-width 200 // https://drafts.csswg.org/css-backgrounds-3/#the-border-image-width
199 value.pixels += 201 value.pixels = clampTo<float>(value.pixels +
200 m_value->getDoubleValue() * conversionData.zoom() * multiplier; 202 m_value->getDoubleValue() *
203 conversionData.zoom() * multiplier);
201 break; 204 break;
202 default: 205 default:
203 ASSERT_NOT_REACHED(); 206 ASSERT_NOT_REACHED();
204 } 207 }
205 } 208 }
206 209
207 double doubleValue() const override { 210 double doubleValue() const override {
208 if (hasDoubleValue(typeWithCalcResolved())) 211 if (hasDoubleValue(typeWithCalcResolved()))
209 return m_value->getDoubleValue(); 212 return m_value->getDoubleValue();
210 ASSERT_NOT_REACHED(); 213 ASSERT_NOT_REACHED();
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 leftType, isInteger); 392 leftType, isInteger);
390 CSSPrimitiveValue::UnitCategory leftUnitCategory = 393 CSSPrimitiveValue::UnitCategory leftUnitCategory =
391 CSSPrimitiveValue::unitTypeToUnitCategory(leftType); 394 CSSPrimitiveValue::unitTypeToUnitCategory(leftType);
392 if (leftUnitCategory != CSSPrimitiveValue::UOther && 395 if (leftUnitCategory != CSSPrimitiveValue::UOther &&
393 leftUnitCategory == 396 leftUnitCategory ==
394 CSSPrimitiveValue::unitTypeToUnitCategory(rightType)) { 397 CSSPrimitiveValue::unitTypeToUnitCategory(rightType)) {
395 CSSPrimitiveValue::UnitType canonicalType = 398 CSSPrimitiveValue::UnitType canonicalType =
396 CSSPrimitiveValue::canonicalUnitTypeForCategory( 399 CSSPrimitiveValue::canonicalUnitTypeForCategory(
397 leftUnitCategory); 400 leftUnitCategory);
398 if (canonicalType != CSSPrimitiveValue::UnitType::Unknown) { 401 if (canonicalType != CSSPrimitiveValue::UnitType::Unknown) {
399 double leftValue = 402 double leftValue = clampTo<double>(
400 leftSide->doubleValue() * 403 leftSide->doubleValue() *
401 CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor( 404 CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(
402 leftType); 405 leftType));
403 double rightValue = 406 double rightValue = clampTo<double>(
404 rightSide->doubleValue() * 407 rightSide->doubleValue() *
405 CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor( 408 CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(
406 rightType); 409 rightType));
407 return CSSCalcPrimitiveValue::create( 410 return CSSCalcPrimitiveValue::create(
408 evaluateOperator(leftValue, rightValue, op), canonicalType, 411 evaluateOperator(leftValue, rightValue, op), canonicalType,
409 isInteger); 412 isInteger);
410 } 413 }
411 } 414 }
412 } 415 }
413 } 416 }
414 } else { 417 } else {
415 // Simplify multiplying or dividing by a number for simplifiable types. 418 // Simplify multiplying or dividing by a number for simplifiable types.
416 ASSERT(op == CalcMultiply || op == CalcDivide); 419 ASSERT(op == CalcMultiply || op == CalcDivide);
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 619
617 double evaluate(double leftSide, double rightSide) const { 620 double evaluate(double leftSide, double rightSide) const {
618 return evaluateOperator(leftSide, rightSide, m_operator); 621 return evaluateOperator(leftSide, rightSide, m_operator);
619 } 622 }
620 623
621 static double evaluateOperator(double leftValue, 624 static double evaluateOperator(double leftValue,
622 double rightValue, 625 double rightValue,
623 CalcOperator op) { 626 CalcOperator op) {
624 switch (op) { 627 switch (op) {
625 case CalcAdd: 628 case CalcAdd:
626 return leftValue + rightValue; 629 return clampTo<double>(leftValue + rightValue);
627 case CalcSubtract: 630 case CalcSubtract:
628 return leftValue - rightValue; 631 return clampTo<double>(leftValue - rightValue);
629 case CalcMultiply: 632 case CalcMultiply:
630 return leftValue * rightValue; 633 return clampTo<double>(leftValue * rightValue);
631 case CalcDivide: 634 case CalcDivide:
632 if (rightValue) 635 if (rightValue)
633 return leftValue / rightValue; 636 return clampTo<double>(leftValue / rightValue);
634 return std::numeric_limits<double>::quiet_NaN(); 637 return std::numeric_limits<double>::quiet_NaN();
635 } 638 }
636 return 0; 639 return 0;
637 } 640 }
638 641
639 const Member<CSSCalcExpressionNode> m_leftSide; 642 const Member<CSSCalcExpressionNode> m_leftSide;
640 const Member<CSSCalcExpressionNode> m_rightSide; 643 const Member<CSSCalcExpressionNode> m_rightSide;
641 const CalcOperator m_operator; 644 const CalcOperator m_operator;
642 }; 645 };
643 646
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 815
813 return expression ? new CSSCalcValue(expression, range) : nullptr; 816 return expression ? new CSSCalcValue(expression, range) : nullptr;
814 } 817 }
815 818
816 CSSCalcValue* CSSCalcValue::create(CSSCalcExpressionNode* expression, 819 CSSCalcValue* CSSCalcValue::create(CSSCalcExpressionNode* expression,
817 ValueRange range) { 820 ValueRange range) {
818 return new CSSCalcValue(expression, range); 821 return new CSSCalcValue(expression, range);
819 } 822 }
820 823
821 } // namespace blink 824 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698