Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CherryPick:2.2:PR #46974: Fix crash of tf.strings.substr when pos and len have different shapes #49288

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions tensorflow/core/kernels/substr_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class SubstrOp : public OpKernel {
const Tensor& len_tensor = context->input(2);
const TensorShape& input_shape = input_tensor.shape();
const TensorShape& pos_shape = pos_tensor.shape();
const TensorShape& len_shape = len_tensor.shape();
OP_REQUIRES(context, (pos_shape == len_shape),
errors::InvalidArgument(
"pos and len should have the same shape, got: ",
pos_shape.DebugString(), " vs. ", len_shape.DebugString()));

bool is_scalar = TensorShapeUtils::IsScalar(pos_shape);

Expand Down
9 changes: 9 additions & 0 deletions tensorflow/python/kernel_tests/substr_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,15 @@ def testInvalidUnit(self):
with self.assertRaises(ValueError):
string_ops.substr(b"test", 3, 1, unit="UTF8")

def testInvalidPos(self):
# Test case for GitHub issue 46900.
with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
x = string_ops.substr(b"abc", len=1, pos=[1, -1])
self.evaluate(x)

with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
x = string_ops.substr(b"abc", len=1, pos=[1, 2])
self.evaluate(x)

if __name__ == "__main__":
test.main()