Skip to content

Integer overflow is undefined behavior #289

@jamesjer

Description

@jamesjer

While building CL11 1.8.0 on an x86_64 machine with gcc 9.1.1 (Fedora Rawhide), this test in HelpersTest fails:

[----------] 4 tests from CheckedMultiply
[ RUN ] CheckedMultiply.Int
/builddir/build/BUILD/CLI11-1.8.0/tests/HelpersTest.cpp:398: Failure
Value of: CLI::detail::checked_multiply(a, b)
Actual: true
Expected: false
[ FAILED ] CheckedMultiply.Int (0 ms)

The issue is that checked_multiply relies on integer overflow wrapping around, but that is undefined behavior according to the standard. When compiling with gcc, -fwrapv must be passed to the compiler so that it doesn't optimize away such code. I have verified that the test passes after adding that option to the build flags.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions