Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fix failed check in tf.reshape.
Passing in too many dimensions causes an assertion
failure and a crash. Check input as a precondition and
fail gracefully.

PiperOrigin-RevId: 449893553
  • Loading branch information
poulsbo authored and tensorflower-gardener committed May 20, 2022
1 parent f08a1c6 commit 61f0f9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tensorflow/core/kernels/reshape_op.h
Expand Up @@ -45,6 +45,11 @@ class ReshapeOp : public OpKernel {
TensorShapeUtils::IsScalar(sizes.shape())),
errors::InvalidArgument("sizes input must be 1-D, not ",
sizes.shape().DebugString()));
OP_REQUIRES(
context, sizes.NumElements() < TensorShape::MaxDimensions(),
errors::InvalidArgument("too many dimensions: must be < ",
TensorShape::MaxDimensions(), ", but received ",
sizes.NumElements()));

// Compute the output shape. Determine product of specified
// dimensions, and find the index of the unspecified one.
Expand Down
9 changes: 9 additions & 0 deletions tensorflow/python/kernel_tests/array_ops/array_ops_test.py
Expand Up @@ -351,6 +351,15 @@ def testExpandDimsWithNonScalarDim(self):
"must be a tensor with a single value"):
array_ops.expand_dims(1, axis=[0, 1])

def testReshapeWithManyDims(self):
with self.assertRaisesRegex(errors.InvalidArgumentError,
"too many dimensions"):
self.evaluate(
array_ops.reshape(
tensor=[[1]],
shape=constant_op.constant([1 for i in range(254)],
dtype=dtypes.int64)))


@test_util.with_eager_op_as_function
class ReverseV2Test(test_util.TensorFlowTestCase):
Expand Down

0 comments on commit 61f0f9b

Please sign in to comment.