Open germandiagogomez opened 3 months ago
Hi @germandiagogomez, from what you've commented, it looks like the misalignment can happen only if the memory assigned to the Image::pixels
member is misaligned and the Image::rowstride
contains an invalid/unaligned value. Did you try allocating aligned memory? (_aligned_malloc / aligned_alloc)
Hello there.
I am using aseprite tga to convert files to tga for RmlUi consumption. When passing UB sanitizer I spot a potential bug (that probably does not manifest in x86, since unaligned access, even if slow, is allowed, but could in ARM). Since I was experimenting random times at which my file would not be saved, I suspect it could be related even in x86?
Anyway, the relevant line is here:
https://github.com/aseprite/tga/blob/cd3420c3a5797f1200fd1e1970ca29598f2c2436/tga.h#L195
This access is potentially misaligned. This code corrects it via a branchless implementation.
Since accessing
*((T*)m_ptr)
is potentially unaligned access and hence, illegal, a trick comes into play: every time we access the first result, the second result will be calculated aligned (with an incorrect value) but since the second result is not needed in that case, no undefined behavior access is done at all in that case. When the second var is accessed, then the access is aligned and legal (and correct).With this:
This would remove the unaligned access. Of course, equivalent and/or better optimized version can probably be done, but this one seems reasonable to me.