Commit 5ac5832a authored by Liviu Tinta's avatar Liviu Tinta Committed by Chromium LUCI CQ

Use clampTo instead of static_cast<int> for layerX/Y

Because we use static_cast<int> to cast from double to int we expose
ourselves to float-cast-overflow.
We use clampTo to make sure the result is in int limits.

MouseEvent: :layerX, MouseEvent::layerY are returned as integers.
Bug: 1164520
Change-Id: I0881323f5508b3f4d3fb2f3f73d6b138aa4202c9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2622870Reviewed-by: default avatarRobert Flack <flackr@chromium.org>
Reviewed-by: default avatarMustaq Ahmed <mustaq@chromium.org>
Commit-Queue: Liviu Tinta <liviutinta@chromium.org>
Cr-Commit-Position: refs/heads/master@{#844536}
parent 17743601
......@@ -42,6 +42,7 @@
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
namespace blink {
......@@ -493,22 +494,14 @@ int MouseEvent::layerX() {
if (!has_cached_relative_position_)
ComputeRelativePosition();
// TODO(mustaq): Remove the PointerEvent specific code when mouse has
// fractional coordinates. See crbug.com/655786.
return IsPointerEvent() ? layer_location_.X()
: static_cast<int>(layer_location_.X());
return clampTo<int, double>(layer_location_.X());
}
int MouseEvent::layerY() {
if (!has_cached_relative_position_)
ComputeRelativePosition();
// TODO(mustaq): Remove the PointerEvent specific code when mouse has
// fractional coordinates. See crbug.com/655786.
return IsPointerEvent() ? layer_location_.Y()
: static_cast<int>(layer_location_.Y());
return clampTo<int, double>(layer_location_.Y());
}
double MouseEvent::offsetX() const {
......
......@@ -9,6 +9,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/events/mouse_event_init.h"
#include "third_party/blink/renderer/platform/geometry/double_point.h"
#include "third_party/blink/renderer/platform/geometry/int_point.h"
......@@ -16,6 +17,8 @@ namespace blink {
class MouseEventScreenClientPagePositionTest
: public ::testing::TestWithParam<std::tuple<double, double>> {};
class MouseEventLayerPositionTest
: public ::testing::TestWithParam<std::tuple<double, double>> {};
TEST_P(MouseEventScreenClientPagePositionTest, PositionAsExpected) {
MouseEvent& mouse_event = *MouseEvent::Create();
......@@ -57,4 +60,40 @@ INSTANTIATE_TEST_SUITE_P(
std::make_tuple(std::numeric_limits<double>::max() - 1.45,
std::floor(std::numeric_limits<double>::max() -
1.45))));
TEST_P(MouseEventLayerPositionTest, LayerPositionAsExpected) {
DoublePoint input_layer_location(std::get<0>(GetParam()),
std::get<0>(GetParam()));
IntPoint expected_layer_location(std::get<1>(GetParam()),
std::get<1>(GetParam()));
MouseEventInit& mouse_event_init = *MouseEventInit::Create();
mouse_event_init.setClientX(input_layer_location.X());
mouse_event_init.setClientY(input_layer_location.Y());
MouseEvent mouse_event("mousedown", &mouse_event_init);
ASSERT_EQ(mouse_event.layerX(), expected_layer_location.X());
ASSERT_EQ(mouse_event.layerY(), expected_layer_location.Y());
}
INSTANTIATE_TEST_SUITE_P(
MouseEventLayerPositionNoOverflow,
MouseEventLayerPositionTest,
::testing::Values(
std::make_tuple(std::numeric_limits<int>::min() * 1.0,
std::numeric_limits<int>::min()),
std::make_tuple(std::numeric_limits<int>::min() * 1.0 - 1.45,
std::numeric_limits<int>::min()),
std::make_tuple(std::numeric_limits<int>::max() * 1.0,
std::numeric_limits<int>::max()),
std::make_tuple(std::numeric_limits<int>::max() * 1.0 + 1.45,
std::numeric_limits<int>::max()),
std::make_tuple(std::numeric_limits<double>::lowest(),
std::numeric_limits<int>::min()),
std::make_tuple(std::numeric_limits<double>::lowest() + 1.45,
std::numeric_limits<int>::min()),
std::make_tuple(std::numeric_limits<double>::max(),
std::numeric_limits<int>::max()),
std::make_tuple(std::numeric_limits<double>::max() - 1.45,
std::numeric_limits<int>::max())));
} // namespace blink
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