2.4 KiB
2.4 KiB
Math Span-Safe Parsing, Checked Cast, and Portable Overflow
Task ID: M-MATH-02 Phase: P1 Status: APPROVED Contracts: C-01, C-02, C-05, C-07
1. Context and baseline
include/Void/Math/Numeric.hpp currently passes string_view::data() to std::strtol/std::strtod (unsafe on non-NUL-terminated substrings), uses long long-based in_range/narrow_cast (broken for unsigned 64-bit / floating conversions), and lacks checked_cast, checked_mul_add, and portable non-builtin overflow arithmetic.
2. Deliverables
- Extended
include/Void/Math/Numeric.hpp:Error::Inexact,Error::NonFinite,Rounding,kMaxNumericTokenBytes = 256, guarded__builtin_*_overflow+ portableconstexprfallback,checked_mul_add,checked_cast,parse_integer,parse_real, and span-safeparse_int/parse_doubleadapters. - Comprehensive boundary tests in
tests/standalone/Math/Numeric.cpp.
3. Exact files and interfaces
include/Void/Math/Numeric.hpp:checked_cast<To, From>(value, Rounding mode = Rounding::exact) noexcept -> std::expected<To, Error>across all non-boolarithmetic pairs.parse_integer<T>(std::string_view, int base = 10) noexcept -> std::expected<T, Error>.parse_real<T>(std::string_view) noexcept -> std::expected<T, Error>.checked_mul_add<T>(T a, T b, T c) noexcept -> std::expected<T, Error>.
4. Edge cases
- Non-NUL-terminated substrings adjacent to digits (
"429"sliced to"42"must parse42, never429). parse_integer/parse_realreject empty strings,> 256bytes, leading whitespace, leading'+', trailing characters, and"nan"/"inf".- Float->int checks exact exclusive
2^63(9223372036854775808.0) /2^64(18446744073709551616.0) boundaries, andnearest_evenimplements deterministic ties-to-even independent ofFE_ROUND. - Int->float exactness checks significant bits without UB when negating
INT64_MIN; preserves-0.0sign on float narrowing.
5. Acceptance criteria
- Existing
Numerictests plus all boundary tests pass under-Werrorand ASan/UBSan.
6. Verification commands
dev cpp-gcc -c cmake -S . -B /tmp/void-p1-math -G Ninja -DCMAKE_CXX_COMPILER=g++ -DCMAKE_BUILD_TYPE=Debug -DVOID_ENABLED_MODULES="Math;Testing" -DVOID_WARNINGS_AS_ERRORS=ON
dev cpp-gcc -c cmake --build /tmp/void-p1-math --parallel 4
dev cpp-gcc -c /tmp/void-p1-math/tests/VoidTests --filter "Numeric*"