mirror of
https://github.com/chromium/crashpad.git
synced 2025-01-01 10:50:29 +08:00
15103742e0
I opted to leave casts to types that were definitely the same size alone. reinterpret_cast<uintptr_t>(pointer) and reinterpret_cast<intptr_t>(pointer) should always be safe, for example. Casts to other integral types have been replaced with FromPointerCast<>(), which does zero-extension or sign-extension based on the target type. To make it possible to use FromPointerCast<>() with some use sites that were already using checked_cast<>(), FromPointerCast<>() now uses check_cast<>() when converting to a narrower type. Test: crashpad_util_test FromPointerCast*, others Change-Id: I4a71b4aa2d87f545c75524290a702f5f3138d675 Reviewed-on: https://chromium-review.googlesource.com/489701 Reviewed-by: Scott Graham <scottmg@chromium.org>
96 lines
3.3 KiB
C++
96 lines
3.3 KiB
C++
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||
//
|
||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
// you may not use this file except in compliance with the License.
|
||
// You may obtain a copy of the License at
|
||
//
|
||
// http://www.apache.org/licenses/LICENSE-2.0
|
||
//
|
||
// Unless required by applicable law or agreed to in writing, software
|
||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
// See the License for the specific language governing permissions and
|
||
// limitations under the License.
|
||
|
||
#ifndef CRASHPAD_UTIL_MISC_FROM_POINTER_CAST_H_
|
||
#define CRASHPAD_UTIL_MISC_FROM_POINTER_CAST_H_
|
||
|
||
#include <stdint.h>
|
||
|
||
#include <cstddef>
|
||
#include <type_traits>
|
||
|
||
#include "base/numerics/safe_conversions.h"
|
||
|
||
namespace crashpad {
|
||
|
||
#if DOXYGEN
|
||
|
||
//! \brief Casts from a pointer type to an integer.
|
||
//!
|
||
//! Compared to `reinterpret_cast<>()`, FromPointerCast<>() defines whether a
|
||
//! pointer type is sign-extended or zero-extended. Casts to signed integral
|
||
//! types are sign-extended. Casts to unsigned integral types are zero-extended.
|
||
//!
|
||
//! Use FromPointerCast<>() instead of `reinterpret_cast<>()` when casting a
|
||
//! pointer to an integral type that may not be the same width as a pointer.
|
||
//! There is no need to prefer FromPointerCast<>() when casting to an integral
|
||
//! type that’s definitely the same width as a pointer, such as `uintptr_t` and
|
||
//! `intptr_t`.
|
||
template <typename To, typename From>
|
||
FromPointerCast(From from) {
|
||
return reinterpret_cast<To>(from);
|
||
}
|
||
|
||
#else // DOXYGEN
|
||
|
||
// Cast std::nullptr_t to any type.
|
||
//
|
||
// In C++14, the nullptr_t check could use std::is_null_pointer<From>::value
|
||
// instead of the is_same<remove_cv<From>::type, nullptr_t>::type construct.
|
||
template <typename To, typename From>
|
||
typename std::enable_if<
|
||
std::is_same<typename std::remove_cv<From>::type, std::nullptr_t>::value,
|
||
To>::type
|
||
FromPointerCast(From) {
|
||
return To();
|
||
}
|
||
|
||
// Cast a pointer to any other pointer type.
|
||
template <typename To, typename From>
|
||
typename std::enable_if<std::is_pointer<From>::value &&
|
||
std::is_pointer<To>::value,
|
||
To>::type
|
||
FromPointerCast(From from) {
|
||
return reinterpret_cast<To>(from);
|
||
}
|
||
|
||
// Cast a pointer to an integral type. Sign-extend when casting to a signed
|
||
// type, zero-extend when casting to an unsigned type.
|
||
template <typename To, typename From>
|
||
typename std::enable_if<std::is_pointer<From>::value &&
|
||
std::is_integral<To>::value,
|
||
To>::type
|
||
FromPointerCast(From from) {
|
||
const auto intermediate =
|
||
reinterpret_cast<typename std::conditional<std::is_signed<To>::value,
|
||
intptr_t,
|
||
uintptr_t>::type>(from);
|
||
|
||
if (sizeof(To) >= sizeof(From)) {
|
||
// If the destination integral type is at least as wide as the source
|
||
// pointer type, use static_cast<>() and just return it.
|
||
return static_cast<To>(intermediate);
|
||
}
|
||
|
||
// If the destination integral type is narrower than the source pointer type,
|
||
// use checked_cast<>().
|
||
return base::checked_cast<To>(intermediate);
|
||
}
|
||
|
||
#endif // DOXYGEN
|
||
|
||
} // namespace crashpad
|
||
|
||
#endif // CRASHPAD_UTIL_MISC_FROM_POINTER_CAST_H_
|