miniply icon indicating copy to clipboard operation
miniply copied to clipboard

UBSAN reports some alignment errors

Open jcelerier opened this issue 1 year ago • 0 comments

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.

jcelerier avatar Dec 01 '24 15:12 jcelerier