memcpy doesn't work correctly if the buffers are not aligned

This commit is contained in:
Bertrand Songis
2020-02-11 08:04:26 +01:00
parent 02810fc6ab
commit 71b7b12e19
2 changed files with 45 additions and 1 deletions
+2 -1
View File
@@ -901,6 +901,7 @@ void *stb_realloc(void *ptr, unsigned int oldsz, unsigned int newsz)
}
#endif // #if defined(TRACE_STB_MALLOC)
#include "memcpy.h"
#include "thirdparty/Stb/stb_image.h"
// fill 'data' with 'size' bytes. return number of bytes actually read
@@ -954,7 +955,7 @@ BitmapBuffer * BitmapBuffer::load_stb(const char * filename)
// convert to RGB565 or ARGB4444 format
BitmapBuffer * bmp = new BitmapBuffer(n == 4 ? BMP_ARGB4444 : BMP_RGB565, w, h);
if (bmp == NULL) {
if (bmp == nullptr) {
TRACE("load_stb() malloc failed");
stbi_image_free(img);
return nullptr;
+43
View File
@@ -0,0 +1,43 @@
/*
* Copyright (C) OpenTX
*
* Source:
* https://github.com/opentx/libopenui
*
* This file is a part of libopenui library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef _MEMCPY_H_
#define _MEMCPY_H_
#include <string.h>
#if !defined(SIMU)
inline void safeMemcpy(void * dest, const void * src, unsigned int size)
{
if (((uint32_t)dest & 0x03) || ((uint32_t)src & 0x03)) {
for (int i = 0; i < size; i++) {
((uint8_t *)dest)[i] = ((uint8_t *)src)[i];
}
}
else {
memcpy(dest, src, size);
}
}
#define memcpy safeMemcpy
#endif
#endif // _HELPERS_H_