Skip to content

fix(Foundation): Fix UBSan errors in VarHolder.h#5091

Merged
matejk merged 1 commit intomainfrom
fix-varholder-ubsan-5090
Dec 16, 2025
Merged

fix(Foundation): Fix UBSan errors in VarHolder.h#5091
matejk merged 1 commit intomainfrom
fix-varholder-ubsan-5090

Conversation

@matejk
Copy link
Copy Markdown
Contributor

@matejk matejk commented Dec 16, 2025

Summary

Fixes #5090 - UndefinedBehaviorSanitizer errors in VarHolder.h

Fixed issues:

  1. numValDigits for signed integers: Negating INT_MIN caused signed overflow (UB). Now uses safe unsigned arithmetic: U(0) - static_cast<U>(value).

  2. numValDigits for floating point: Casting large floats (e.g., 1e20) to int64_t caused UB. Now uses std::ilogb() to get bit count directly from the exponent.

  3. POCO_VAR_RANGE_EXCEPTION macro: Casting out-of-range values to target type was UB. Added rangeExcCastStr() helper that safely casts integral sources to intmax_t/uintmax_t, and returns "?" for floating-point sources.

C++17 modernization (while fixing the above):

  • Added [[nodiscard]] to clone(), type(), size(), and all is*() query methods
  • Merged multiple SFINAE overloads into single functions using if constexpr:
    • checkUpperLimit/checkLowerLimit: 4 overloads → 2 functions
    • convertToSmaller: 3 overloads → 1 function
    • convertToSigned/convertToUnsigned: 6 overloads → 2 functions
  • Removed unpreserveSign() function (inlined safe arithmetic into numValDigits)
  • Fixed Var.h to suppress [[nodiscard]] warning for intentionally ignored clone() return

Test plan

  • Built with UBSan enabled (-fsanitize=undefined -fno-sanitize=vptr)
  • VarTest passes with no UBSan errors
  • All Foundation tests pass

@matejk matejk force-pushed the fix-varholder-ubsan-5090 branch from 60ef488 to 1738055 Compare December 16, 2025 06:12
@matejk matejk force-pushed the fix-varholder-ubsan-5090 branch from 1738055 to 98a77e1 Compare December 16, 2025 06:55
@matejk matejk merged commit e1ba55b into main Dec 16, 2025
80 checks passed
@matejk matejk deleted the fix-varholder-ubsan-5090 branch December 16, 2025 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UBSan: Integer overflow errors in VarHolder.h

1 participant