Skip to content

Commit 87158f4

Browse files
Prevent heap OOB in sparse reduction ops.
PiperOrigin-RevId: 387934524 Change-Id: I894aa30f1e454f09b471d565b4a325da49322c1a
1 parent 9c7f40e commit 87158f4

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

tensorflow/core/kernels/sparse_reduce_op.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,20 @@ class SparseReduceOp : public OpKernel {
219219
sp.Reorder<T>(reduction.reorder_dims);
220220
for (const auto &g : sp.group(reduction.group_by_dims)) {
221221
Op::template Run<T>(ctx, reduced_val, g.template values<T>());
222+
OP_REQUIRES(ctx,
223+
output_strides.empty() ||
224+
(g.group().size() == output_strides.size()),
225+
errors::Internal(
226+
"Expected group size and output_strides size to match",
227+
", but got ", g.group().size(), " and ",
228+
output_strides.size()));
222229
const int64_t idx = CoordinatesToFlatIndex(g.group(), output_strides);
230+
OP_REQUIRES(ctx,
231+
idx >= 0 && idx < out_flat.size(),
232+
errors::Internal(
233+
"Obtained a write index of ", idx,
234+
" which is outside of bounds of [0, ",
235+
out_flat.size(), ")"));
223236
out_flat(idx) = reduced_val();
224237
VLOG(2) << "coords: " << absl::StrJoin(g.group(), ",")
225238
<< "; idx: " << idx << "; group " << Op::Name() << ": "

0 commit comments

Comments
 (0)