Currently, the _MiB literal operator returns a value of type size_t. This is brittle and confusing:
- It is inherently impossible to represent larger values, like storage device usage.
- Similarly, it is not possible to even type an upper cap on the memory, see the failure in #34692 (comment)
- Using
size_tisn't required here. The function is evaluated at compile time, and even 32-bit compilers can evaluate anuint64_tat compile time. - Using
size_there encourages it to be used in more places, which will likely lead to more bugs and CVEs, such as #34109, #33724, etc.
Fix all issues, by:
- Marking the operator
consteval, to ensure it is really only called at compile-time. - Returning an
uint64_tvalue. - Using it in the place where it was previously not possible.
Review note:
This should have no downside, because the C++11 narrowing checks continue to work as expected. For example, typing uint8_t{1_MiB}; will continue to fail with the correct error message error: constant expression evaluates to 1048576 which cannot be narrowed to type 'uint8_t' (aka 'unsigned char') [-Wc++11-narrowing] (like it does on current master).