diff --git a/misc.h b/misc.h index 54648569..12529781 100644 --- a/misc.h +++ b/misc.h @@ -386,21 +386,6 @@ inline PTR PtrSub(PTR pointer, OFF offset) return pointer-static_cast(offset); } -/// \brief Determine pointer difference -/// \tparam PTR a pointer type -/// \param pointer1 the first pointer -/// \param pointer2 the second pointer -/// \details PtrByteDiff can be used to squash Clang and GCC -/// UBsan findings for pointer addition and subtraction. -/// pointer1 and pointer2 must point to the same object or -/// array (or one past the end), and yields the number of -/// bytes (not elements) difference. -template -inline uintptr_t PtrByteDiff(const PTR pointer1, const PTR pointer2) -{ - return static_cast(pointer1) - static_cast(pointer2); -} - /// \brief Determine pointer difference /// \tparam PTR a pointer type /// \param pointer1 the first pointer @@ -413,7 +398,22 @@ inline uintptr_t PtrByteDiff(const PTR pointer1, const PTR pointer2) template inline ptrdiff_t PtrDiff(const PTR pointer1, const PTR pointer2) { - return static_cast(pointer1 - pointer2); + return pointer1 - pointer2; +} + +/// \brief Determine pointer difference +/// \tparam PTR a pointer type +/// \param pointer1 the first pointer +/// \param pointer2 the second pointer +/// \details PtrByteDiff can be used to squash Clang and GCC +/// UBsan findings for pointer addition and subtraction. +/// pointer1 and pointer2 must point to the same object or +/// array (or one past the end), and yields the number of +/// bytes (not elements) difference. +template +inline size_t PtrByteDiff(const PTR pointer1, const PTR pointer2) +{ + return (size_t)(static_cast(pointer1) - static_cast(pointer2)); } #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)