Repository navigation
Handle signed overflow in checked_div and checked_mod - #2742
Open
ZainnQureshii wants to merge 1 commit into
Open
ZainnQureshii wants to merge 1 commit into
ZainnQureshii wants to merge 1 commit into
Conversation
checked_div and checked_mod only rejected a zero divisor, so for signed built-in types min / -1 and min % -1 still ran and hit undefined behaviour. Return false for that pair too, guarded by if constexpr so class types are unchanged, and fix checkedModZeroDivisor to call checked_mod. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
checked_divandchecked_modinfolly/lang/CheckedMath.honly rejectdivisor == 0. For a signed built-in type at least as wide asint(no promotion),min / -1andmin % -1are undefined behaviour (the quotient does not fit), sochecked_div(&r, INT64_MIN, int64_t{-1})runs the division instead of returningfalse. UBSan reportsdivision of -9223372036854775808 by -1 cannot be represented in type 'long long'at both call sites.Both functions now return
falsewith*result = {}for that pair, the same way they handle a zero divisor. The check is underif constexprfor built-in signed integers only, usingfolly::is_integral_vandfolly::is_signed_vso__int128is covered on standard libraries whosestd::traits leave it out (libstdc++ in strict-std=c++XXmode). The minimum is computed from the sign bit viafolly::make_unsigned_trather thanstd::numeric_limits, so the guard also works where strict-mode libstdc++ (GCC 10.1 and 10.2) leavesnumeric_limits<__int128>unspecialised, which would makemin()return0and reject0 / -1. Class types keep their old behaviour.checked_modtests the promoted operand type, soint8_t(-128) % int8_t(-1)(computed inint) still returnstruewith0.One visible change for narrow types:
checked_divonint8_t(-128) / int8_t(-1)orint16_t(-32768) / int16_t(-1)used to returntruewith the wrapped value (-128,-32768), because the division happens inintand only the store narrows. It now returnsfalse, since the true quotient does not fit inT.The test
checkedModZeroDivisorwas callingchecked_div; it now callschecked_mod. Because the header now usesfolly/Traits.h,checked_mathlists//folly:traitsinfolly/lang/BUCKandfolly_traitsinfolly/lang/CMakeLists.txt, and the test target lists it infolly/lang/test/BUCK.Test plan
folly/lang/test/CheckedMathTest.cppbuilt with clang++-std=c++20 -fsanitize=undefinedagainst GoogleTest:checkedDivSignedOverflow,checkedModSignedOverflow,checkedDivNarrowSignedOverflowandcheckedDivModInt128SignedOverflowfail, with UBSan overflow reports atCheckedMath.h:185and:195. Result: 41 tests pass and four fail, with six UBSan overflow reports.-DFOLLY_NO_CONFIG -DFOLLY_HAVE_INT128_T=1so the__int128tests run), including the newcheckedDivModInt128ZeroDividend, which checks that0 / -1and0 % -1still succeed.The full CMake/CTest build, Buck, native GCC builds and other platforms were not run. The GCC 10.1, 10.2, 10.3 and 11.1
<limits>headers were compiled with Clang in small probes (strict and GNU C++17/C++20) to check the__int128specialisation, and libc++ probes passed in the same modes.AI assistance
Prepared with AI assistance (Claude Code) and reviewed with GitHub Copilot.
🤖 Generated with Claude Code