aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Freilich <sfreilich@google.com>2022-03-24 09:04:22 -0700
committerCopybara-Service <copybara-worker@google.com>2022-03-24 09:04:45 -0700
commit72b3529f326eead6d715ac5b0f52464bac716419 (patch)
treeab41de15b8ab42f0885eb2627ecc4b3872b25d97
parentda7bbe8fb80d6e0da5099da11842d9b12bf2fb51 (diff)
downloadink-stroke-modeler-72b3529f326eead6d715ac5b0f52464bac716419.tar.gz
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
-rw-r--r--ink_stroke_modeler/BUILD.bazel5
-rw-r--r--ink_stroke_modeler/CMakeLists.txt5
-rw-r--r--ink_stroke_modeler/stroke_modeler.cc4
-rw-r--r--ink_stroke_modeler/types.cc36
-rw-r--r--ink_stroke_modeler/types.h4
5 files changed, 54 insertions, 0 deletions
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<std::vector<Result>> 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 <cmath>
#include <ostream>
+#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.