Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:10

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 // Contains utilities for making UBSan happy.
0019 
0020 #pragma once
0021 
0022 #include <cstring>
0023 #include <memory>
0024 #include <type_traits>
0025 
0026 #include "arrow/util/macros.h"
0027 
0028 namespace arrow {
0029 namespace util {
0030 
0031 namespace internal {
0032 
0033 constexpr uint8_t kNonNullFiller = 0;
0034 
0035 }  // namespace internal
0036 
0037 /// \brief Returns maybe_null if not null or a non-null pointer to an arbitrary memory
0038 /// that shouldn't be dereferenced.
0039 ///
0040 /// Memset/Memcpy are undefined when a nullptr is passed as an argument use this utility
0041 /// method to wrap locations where this could happen.
0042 ///
0043 /// Note: Flatbuffers has UBSan warnings if a zero length vector is passed.
0044 /// https://github.com/google/flatbuffers/pull/5355 is trying to resolve
0045 /// them.
0046 template <typename T>
0047 inline T* MakeNonNull(T* maybe_null = NULLPTR) {
0048   if (ARROW_PREDICT_TRUE(maybe_null != NULLPTR)) {
0049     return maybe_null;
0050   }
0051 
0052   return const_cast<T*>(reinterpret_cast<const T*>(&internal::kNonNullFiller));
0053 }
0054 
0055 template <typename T>
0056 inline std::enable_if_t<std::is_trivially_copyable_v<T>, T> SafeLoadAs(
0057     const uint8_t* unaligned) {
0058   std::remove_const_t<T> ret;
0059   std::memcpy(&ret, unaligned, sizeof(T));
0060   return ret;
0061 }
0062 
0063 template <typename T>
0064 inline std::enable_if_t<std::is_trivially_copyable_v<T>, T> SafeLoad(const T* unaligned) {
0065   std::remove_const_t<T> ret;
0066   std::memcpy(&ret, static_cast<const void*>(unaligned), sizeof(T));
0067   return ret;
0068 }
0069 
0070 template <typename U, typename T>
0071 inline std::enable_if_t<std::is_trivially_copyable_v<T> &&
0072                             std::is_trivially_copyable_v<U> && sizeof(T) == sizeof(U),
0073                         U>
0074 SafeCopy(T value) {
0075   std::remove_const_t<U> ret;
0076   std::memcpy(&ret, static_cast<const void*>(&value), sizeof(T));
0077   return ret;
0078 }
0079 
0080 template <typename T>
0081 inline std::enable_if_t<std::is_trivially_copyable_v<T>, void> SafeStore(void* unaligned,
0082                                                                          T value) {
0083   std::memcpy(unaligned, &value, sizeof(T));
0084 }
0085 
0086 }  // namespace util
0087 }  // namespace arrow