Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UBSAN reports some alignment errors #13

Open
jcelerier opened this issue Dec 1, 2024 · 0 comments
Open

UBSAN reports some alignment errors #13

jcelerier opened this issue Dec 1, 2024 · 0 comments

Comments

@jcelerier
Copy link

jcelerier commented Dec 1, 2024

ex.

miniply.cpp:430:58: 
runtime error: load of misaligned address 0xfffeb2a4881c for type 'const double', which requires 8 byte alignment
0xfffeb2a4881c: note: pointer points here
  af 99 7e 00 48 e1 7a 14  ae 07 43 c0 00 00 00 00  00 00 13 c0 a4 70 3d 0a  d7 a3 f8 bf a7 92 77 00
              ^ 

Most likely

  template <class T>
  static void copy_and_convert_to(T* dest, const uint8_t* src, PLYPropertyType srcType)
  {
    switch (srcType) {
    case PLYPropertyType::Char:   *dest = static_cast<T>(*reinterpret_cast<const int8_t*>(src)); break;
    case PLYPropertyType::UChar:  *dest = static_cast<T>(*reinterpret_cast<const uint8_t*>(src)); break;
    case PLYPropertyType::Short:  *dest = static_cast<T>(*reinterpret_cast<const int16_t*>(src)); break;
    case PLYPropertyType::UShort: *dest = static_cast<T>(*reinterpret_cast<const uint16_t*>(src)); break;
    case PLYPropertyType::Int:    *dest = static_cast<T>(*reinterpret_cast<const int*>(src)); break;
    case PLYPropertyType::UInt:   *dest = static_cast<T>(*reinterpret_cast<const uint32_t*>(src)); break;
    case PLYPropertyType::Float:  *dest = static_cast<T>(*reinterpret_cast<const float*>(src)); break;
    case PLYPropertyType::Double: *dest = static_cast<T>(*reinterpret_cast<const double*>(src)); break;
    case PLYPropertyType::None:   break;
    }
  }

should be using std::bit_cast if C++20 or memcpy before as as it stands, there are platforms on which

case PLYPropertyType::Double: *dest = static_cast<T>(*reinterpret_cast<const double*>(src)); break;

will fault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant