diff --git a/deps/v8/src/interpreter/bytecode-generator.cc b/deps/v8/src/interpreter/bytecode-generator.cc index f78330bea1d58d..cf51e4c975290f 100644 --- a/deps/v8/src/interpreter/bytecode-generator.cc +++ b/deps/v8/src/interpreter/bytecode-generator.cc @@ -1888,17 +1888,28 @@ bool IsSwitchOptimizable(SwitchStatement* stmt, SwitchInfo* info) { } // GCC also jump-table optimizes switch statements with 6 cases or more. - if (!(static_cast(info->covered_cases.size()) >= - FLAG_switch_table_min_cases && - IsSpreadAcceptable(info->MaxCase() - info->MinCase(), - cases->length()))) { - // Invariant- covered_cases has all cases and only cases that will go in the - // jump table. - info->covered_cases.clear(); - return false; - } else { - return true; - } + if (static_cast(info->covered_cases.size()) >= + FLAG_switch_table_min_cases) { + // Due to case spread will be used as the size of jump-table, + // we need to check if it doesn't overflow by casting its + // min and max bounds to int64_t, and calculate if the difference is less + // than or equal to INT_MAX. + int64_t min = static_cast(info->MinCase()); + int64_t max = static_cast(info->MaxCase()); + int64_t spread = max - min + 1; + + DCHECK_GT(spread, 0); + + // Check if casted spread is acceptable and doesn't overflow. + if (spread <= INT_MAX && + IsSpreadAcceptable(static_cast(spread), cases->length())) { + return true; + } + } + // Invariant- covered_cases has all cases and only cases that will go in the + // jump table. + info->covered_cases.clear(); + return false; } } // namespace