You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@arrow.apache.org by "Ben Kietzman (Jira)" <ji...@apache.org> on 2021/06/15 15:12:00 UTC

[jira] [Created] (ARROW-13084) [C++] Avoid construction of unaligned pointers

Ben Kietzman created ARROW-13084:
------------------------------------

             Summary: [C++] Avoid construction of unaligned pointers
                 Key: ARROW-13084
                 URL: https://issues.apache.org/jira/browse/ARROW-13084
             Project: Apache Arrow
          Issue Type: Improvement
          Components: C++
            Reporter: Ben Kietzman


IMHO it's a foot gun to allow construction of pointers which are not aligned. For example, see https://github.com/apache/arrow/pull/10489 where {{const int64_t* key_left_ptr}} was dereferenced without being wrapped in SafeLoad, resulting in undefined behavior. Unaligned pointers are convenient because they apply the correct multiple of {{sizeof(T)}} to integer arithmetic, but there's no way to warn at the point of access that they must be wrapped in SafeLoad.

I propose we remove the overload of SafeLoad which accesses an unaligned pointer and replace it with an indexed overload of SafeLoadAs. This will avoid boilerplate of multiplying by {{sizeof(T)}} but will make clear with typing that access requires SafeLoadAs:

{code}
template <typename T>
T SafeLoadAs(const void* buf, size_t index) {
  T value;
  std::memcpy(&value, reinterpret_cast<const T*>(buf) + index, sizeof(value));
  return value;
}

// ...
    const void* key_left_ptr = ...;
    auto key_left = SafeLoadAs<uint64_t>(key_left_ptr, istripe);
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)