aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2017-08-03 15:34:26 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-08-03 15:41:20 -0700
commitd906c963269dd1522c7693c8f944e6a846b86221 (patch)
tree8af37ae96388aa1d2f3c2000b8104cd45e6ac669
parenta6b2cc4b4c2ae377a06f1fb4119d3f5bc764c337 (diff)
Fix signed integer overflows detected with -fsanitize=signed-integer-overflow
PiperOrigin-RevId: 164193035
-rw-r--r--tensorflow/compiler/xla/tests/scalar_computations_test.cc2
-rw-r--r--tensorflow/core/framework/shape_inference.cc7
-rw-r--r--tensorflow/core/lib/strings/numbers.cc2
-rw-r--r--tensorflow/core/util/memmapped_file_system_test.cc2
4 files changed, 8 insertions, 5 deletions
diff --git a/tensorflow/compiler/xla/tests/scalar_computations_test.cc b/tensorflow/compiler/xla/tests/scalar_computations_test.cc
index 9f49d30c1b..25ca035366 100644
--- a/tensorflow/compiler/xla/tests/scalar_computations_test.cc
+++ b/tensorflow/compiler/xla/tests/scalar_computations_test.cc
@@ -123,7 +123,7 @@ XLA_TEST_F(ScalarComputationsTest, AddTwoScalarsU64) {
XLA_TEST_F(ScalarComputationsTest, AddTwoScalarsS64) {
ComputationBuilder builder(client_, TestName());
const int64 a = static_cast<int64>(1) << 62;
- const int64 b = a + 1;
+ const int64 b = a - 1;
builder.Add(builder.ConstantR0<int64>(a), builder.ConstantR0<int64>(b));
ComputeAndCompareR0<int64>(&builder, a + b, {});
diff --git a/tensorflow/core/framework/shape_inference.cc b/tensorflow/core/framework/shape_inference.cc
index 6947f68002..ca6eb5b7fb 100644
--- a/tensorflow/core/framework/shape_inference.cc
+++ b/tensorflow/core/framework/shape_inference.cc
@@ -867,8 +867,11 @@ Status InferenceContext::Add(DimensionHandle first, DimensionOrConstant second,
} else if (first_value == kUnknownDim || second_value == kUnknownDim) {
*out = UnknownDim();
} else {
- // Invariant: Both values are known and positive.
- const int64 sum = first_value + second_value;
+ // Invariant: Both values are known and positive. Still in run-time we can
+ // get pair of values which cannot be store in output. Check below will
+ // report error. We still need to avoid undefined behavior of signed
+ // overflow and use unsigned addition.
+ const int64 sum = static_cast<uint64>(first_value) + second_value;
if (sum < 0) {
return errors::InvalidArgument("Dimension size overflow from adding ",
first_value, " and ", second_value);
diff --git a/tensorflow/core/lib/strings/numbers.cc b/tensorflow/core/lib/strings/numbers.cc
index 0dea0f89f9..3c85737702 100644
--- a/tensorflow/core/lib/strings/numbers.cc
+++ b/tensorflow/core/lib/strings/numbers.cc
@@ -234,7 +234,7 @@ bool safe_strtou64(StringPiece str, uint64* value) {
SkipSpaces(&str);
if (!isdigit(SafeFirstChar(str))) return false;
- int64 result = 0;
+ uint64 result = 0;
do {
int digit = SafeFirstChar(str) - '0';
if ((kuint64max - digit) / 10 < result) {
diff --git a/tensorflow/core/util/memmapped_file_system_test.cc b/tensorflow/core/util/memmapped_file_system_test.cc
index a5f24b08b3..616eb5dac3 100644
--- a/tensorflow/core/util/memmapped_file_system_test.cc
+++ b/tensorflow/core/util/memmapped_file_system_test.cc
@@ -52,7 +52,7 @@ Status CreateMemmappedFileSystemFile(const string& filename, bool corrupted,
// Save a tensor after the proto to check that alignment works.
test::FillFn<float>(test_tensor,
- [](int i) { return static_cast<float>(i * i * i); });
+ [](int i) { return static_cast<float>(i) * i * i; });
TF_RETURN_IF_ERROR(writer.SaveTensor(*test_tensor, kTensor2FileName));
if (!corrupted) {