Skip to content

Commit

Permalink
Prevent crash due to integer overflow followed by allocating negative…
Browse files Browse the repository at this point in the history
… sized array.

PiperOrigin-RevId: 414891322
Change-Id: I5df390e0dc1d9f115209293708950cdf9306931c
  • Loading branch information
mihaimaruseac authored and tensorflower-gardener committed Dec 8, 2021
1 parent adbbabd commit 6f4d3e8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tensorflow/core/kernels/count_ops.cc
Expand Up @@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <limits>

#include "absl/container/flat_hash_map.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/op_requires.h"
Expand All @@ -23,6 +25,9 @@ limitations under the License.

namespace tensorflow {

// Don't allocate too large `BatchedMap<T>` objects
static int kMaxBatches = std::numeric_limits<int>::max();

template <class T>
using BatchedMap = std::vector<absl::flat_hash_map<int64_t, T>>;

Expand Down Expand Up @@ -235,6 +240,10 @@ class SparseCount : public OpKernel {

bool is_1d = shape.NumElements() == 1;
int num_batches = is_1d ? 1 : shape_vector(0);
OP_REQUIRES(
context, 0 < num_batches && num_batches < kMaxBatches,
errors::InvalidArgument("Cannot allocate ", num_batches,
" batches, is the dense shape too wide?"));

const auto values_values = values.flat<T>();
const auto weight_values = weights.flat<W>();
Expand Down

0 comments on commit 6f4d3e8

Please sign in to comment.