Skip to content

Commit d7be289

Browse files
psiddhGithub Executorch
andauthored
Fix division by zero in reduce_util.h grain size calculation (pytorch#16462)
Add zero check for reduction_size before dividing to calculate grain_size, preventing crash when fuzzer triggers edge case with empty reduction dimension. ### Test plan [PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable. --------- Co-authored-by: Github Executorch <github_executorch@arm.com>
1 parent d3b4a24 commit d7be289

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

‎kernels/portable/cpu/util/reduce_util.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,12 @@ template <typename Func>
814814
const Func& func) {
815815
#ifdef ET_USE_THREADPOOL
816816
const ssize_t reduction_size = get_reduced_dim_product(in, dim);
817-
const auto grain_size = std::max(
818-
static_cast<ssize_t>(1),
819-
static_cast<ssize_t>(executorch::extension::internal::GRAIN_SIZE) /
820-
reduction_size);
817+
const auto grain_size = reduction_size == 0
818+
? 1
819+
: std::max(
820+
static_cast<ssize_t>(1),
821+
static_cast<ssize_t>(executorch::extension::internal::GRAIN_SIZE) /
822+
reduction_size);
821823
#else // ET_USE_THREADPOOL
822824
const auto grain_size = 1;
823825
#endif // ET_USE_THREADPOOL

0 commit comments

Comments
 (0)