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

r2.9 cherry-pick: 660ce5a89eb "[Security] Add a check for empty variant tensor input to CompositeTensorVariantToComponents." #58239

Merged
merged 1 commit into from
Oct 24, 2022
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
6 changes: 6 additions & 0 deletions tensorflow/core/kernels/composite_tensor_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/op_requires.h"
#include "tensorflow/core/framework/variant.h"
#include "tensorflow/core/framework/variant_encode_decode.h"
#include "tensorflow/core/kernels/composite_tensor_variant.h"
Expand Down Expand Up @@ -66,6 +67,11 @@ class CompositeTensorVariantToComponents : public OpKernel {

void Compute(OpKernelContext* context) override {
Tensor encoded_t = context->input(0);
OP_REQUIRES(
context, encoded_t.flat<Variant>().size() > 0,
errors::InvalidArgument("Input `encoded` must not be an empty variant "
"tensor, but got ",
encoded_t.DebugString()));
auto* encoded = encoded_t.flat<Variant>()(0).get<CompositeTensorVariant>();

// Check that the encoded TypeSpec is compatible with the expected TypeSpec.
Expand Down
14 changes: 14 additions & 0 deletions tensorflow/python/kernel_tests/composite_tensor_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

from tensorflow.python.eager import backprop
from tensorflow.python.eager import context
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.framework import test_util
from tensorflow.python.ops import composite_tensor_ops
from tensorflow.python.ops import gen_composite_tensor_ops
from tensorflow.python.ops import gradients_impl
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import parsing_ops
Expand Down Expand Up @@ -83,6 +85,18 @@ def testEncodingErrors(self, value, spec, message):
with self.assertRaisesRegex(ValueError, message):
composite_tensor_ops.composite_tensor_to_variants(value(), spec)

def testDecodingEmptyNonScalarTensorError(self):
if not context.executing_eagerly():
# Creating a variant tensor of an empty list is not allowed in eager mode.
return

with self.assertRaisesRegex(errors.InvalidArgumentError,
'must not be an empty variant tensor'):
gen_composite_tensor_ops.CompositeTensorVariantToComponents(
encoded=constant_op.constant([], dtype=dtypes.variant),
metadata='',
Tcomponents=[dtypes.int32])

def testRoundTripThroughTensorProto(self):
value = ragged_factory_ops.constant([[1, 2], [3], [4, 5, 6]])
encoded = composite_tensor_ops.composite_tensor_to_variants(value)
Expand Down