Skip to content

[C++] Avoid construction of unaligned pointers #28790

Description

@asfimport

IMHO it's a foot gun to allow construction of pointers which are not aligned. For example, see #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:

template <typenameT>
TSafeLoadAs(constvoid* buf, size_tindex) {
Tvalue;
std::memcpy(&value, reinterpret_cast<constT*>(buf) + index, sizeof(value));
returnvalue;
}
// ...constvoid* key_left_ptr = ...;
autokey_left = SafeLoadAs<uint64_t>(key_left_ptr, istripe);

Reporter: Ben Kietzman / @bkietz

Note: This issue was originally created as ARROW-13084. Please see the migration documentation for further details.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions