Skip to content

Commit

Permalink
Merge pull request #53998 from pranve/cherrypick-fcd18ce3101f245b083b…
Browse files Browse the repository at this point in the history
…30655c27b239dc72221e-on-r2.7

Prevent integer overflow in `OpLevelCostEstimator::CalculateTensorSize`.
  • Loading branch information
mihaimaruseac committed Jan 26, 2022
2 parents dc767e0 + 3896e38 commit a95dfba
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tensorflow/core/grappler/costs/op_level_cost_estimator.cc
Expand Up @@ -1554,7 +1554,13 @@ int64_t OpLevelCostEstimator::CalculateTensorSize(
int64_t count = CalculateTensorElementCount(tensor, found_unknown_shapes);
int size = DataTypeSize(BaseType(tensor.dtype()));
VLOG(2) << "Count: " << count << " DataTypeSize: " << size;
return count * size;
int64_t tensor_size = MultiplyWithoutOverflow(count, size);
if (tensor_size < 0) {
VLOG(1) << "Overflow encountered when computing tensor size, multiplying "
<< count << " with " << size;
return -1;
}
return tensor_size;
}

int64_t OpLevelCostEstimator::CalculateInputSize(const OpInfo& op_info,
Expand Down

0 comments on commit a95dfba

Please sign in to comment.