From 72b3529f326eead6d715ac5b0f52464bac716419 Mon Sep 17 00:00:00 2001 From: Samuel Freilich Date: Thu, 24 Mar 2022 09:04:22 -0700 Subject: Validate inputs in StrokeModel.Update are not inf/NaN So far not checking more specific bounds for pressure/orientation/direction to not disturb existing uses. PiperOrigin-RevId: 437007881 --- ink_stroke_modeler/BUILD.bazel | 5 +++++ ink_stroke_modeler/CMakeLists.txt | 5 +++++ ink_stroke_modeler/stroke_modeler.cc | 4 ++++ ink_stroke_modeler/types.cc | 36 ++++++++++++++++++++++++++++++++++++ ink_stroke_modeler/types.h | 4 ++++ 5 files changed, 54 insertions(+) create mode 100644 ink_stroke_modeler/types.cc diff --git a/ink_stroke_modeler/BUILD.bazel b/ink_stroke_modeler/BUILD.bazel index 68b9849..d48f1ea 100644 --- a/ink_stroke_modeler/BUILD.bazel +++ b/ink_stroke_modeler/BUILD.bazel @@ -42,8 +42,13 @@ cc_test( cc_library( name = "types", + srcs = ["types.cc"], hdrs = ["types.h"], visibility = ["//visibility:public"], + deps = [ + "//ink_stroke_modeler/internal:validation", + "@com_google_absl//absl/status", + ], ) cc_test( diff --git a/ink_stroke_modeler/CMakeLists.txt b/ink_stroke_modeler/CMakeLists.txt index adfb5dc..736663a 100644 --- a/ink_stroke_modeler/CMakeLists.txt +++ b/ink_stroke_modeler/CMakeLists.txt @@ -43,8 +43,13 @@ ink_cc_test( ink_cc_library( NAME types + SRCS + types.cc HDRS types.h + DEPS + absl::status + InkStrokeModeler::validation ) ink_cc_test( diff --git a/ink_stroke_modeler/stroke_modeler.cc b/ink_stroke_modeler/stroke_modeler.cc index 917d2d6..37d17f5 100644 --- a/ink_stroke_modeler/stroke_modeler.cc +++ b/ink_stroke_modeler/stroke_modeler.cc @@ -103,6 +103,10 @@ absl::StatusOr> StrokeModeler::Update(const Input &input) { "Stroke model has not yet been initialized"); } + if (absl::Status status = ValidateInput(input); !status.ok()) { + return status; + } + if (last_input_) { if (last_input_->input == input) { return absl::InvalidArgumentError("Received duplicate input"); diff --git a/ink_stroke_modeler/types.cc b/ink_stroke_modeler/types.cc new file mode 100644 index 0000000..9864ea3 --- /dev/null +++ b/ink_stroke_modeler/types.cc @@ -0,0 +1,36 @@ +#include "ink_stroke_modeler/types.h" + +#include "absl/status/status.h" +#include "ink_stroke_modeler/internal/validation.h" + +// This convenience macro evaluates the given expression, and if it does not +// return an OK status, returns and propagates the status. +#define RETURN_IF_ERROR(expr) \ + do { \ + if (auto status = (expr); !status.ok()) return status; \ + } while (false) + +namespace ink { +namespace stroke_model { + +absl::Status ValidateInput(const Input& input) { + switch (input.event_type) { + case Input::EventType::kUp: + case Input::EventType::kMove: + case Input::EventType::kDown: + break; + default: + return absl::InvalidArgumentError("Unknown Input.event_type."); + } + RETURN_IF_ERROR(ValidateIsFiniteNumber(input.position.x, "Input.position.x")); + RETURN_IF_ERROR(ValidateIsFiniteNumber(input.position.y, "Input.position.y")); + RETURN_IF_ERROR(ValidateIsFiniteNumber(input.time.Value(), "Input.time")); + RETURN_IF_ERROR(ValidateIsFiniteNumber(input.pressure, "Input.pressure")); + RETURN_IF_ERROR(ValidateIsFiniteNumber(input.tilt, "Input.tilt")); + RETURN_IF_ERROR( + ValidateIsFiniteNumber(input.orientation, "Input.orientation")); + return absl::OkStatus(); +} + +} // namespace stroke_model +} // namespace ink diff --git a/ink_stroke_modeler/types.h b/ink_stroke_modeler/types.h index 922c0f7..cc77565 100644 --- a/ink_stroke_modeler/types.h +++ b/ink_stroke_modeler/types.h @@ -20,6 +20,8 @@ #include #include +#include "absl/status/status.h" + namespace ink { namespace stroke_model { @@ -148,6 +150,8 @@ bool operator!=(const Input &lhs, const Input &rhs); std::ostream &operator<<(std::ostream &s, Input::EventType event_type); std::ostream &operator<<(std::ostream &s, const Input &input); +absl::Status ValidateInput(const Input &input); + // A modeled input produced by the stroke modeler. struct Result { // The position and velocity of the stroke tip. -- cgit v1.2.3