buffer overflow check + error message

This commit is contained in:
Slider0007
2023-02-02 00:01:41 +01:00
parent d40d5ba0f6
commit 939271f2a0

View File

@@ -18,6 +18,9 @@ using namespace std;
static const char *TAG = "C IMG BASIS"; static const char *TAG = "C IMG BASIS";
bool jpgFileTooLarge = false; // JPG creation verfication
//#define DEBUG_DETAIL_ON //#define DEBUG_DETAIL_ON
@@ -63,12 +66,18 @@ void writejpghelp(void *context, void *data, int size)
ImageData* _zw = (ImageData*) context; ImageData* _zw = (ImageData*) context;
uint8_t *voidstart = _zw->data; uint8_t *voidstart = _zw->data;
uint8_t *datastart = (uint8_t*) data; uint8_t *datastart = (uint8_t*) data;
voidstart += _zw->size;
if ((_zw->size < MAX_JPG_SIZE)) { // Abort copy -> no buffer anymore
voidstart += _zw->size;
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
*(voidstart + i) = *(datastart + i); *(voidstart + i) = *(datastart + i);
_zw->size += size; _zw->size += size;
}
else {
jpgFileTooLarge = true;
}
} }
@@ -80,6 +89,10 @@ ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality); stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
RGBImageRelease(); RGBImageRelease();
if (jpgFileTooLarge) {
jpgFileTooLarge = false;
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: JPG creation aborted! Preallocated buffer not sufficient: " + std::to_string(MAX_JPG_SIZE));
}
return ii; return ii;
} }
@@ -92,6 +105,10 @@ void CImageBasis::writeToMemoryAsJPG(ImageData* i, const int quality)
stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality); stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
RGBImageRelease(); RGBImageRelease();
if (jpgFileTooLarge) {
jpgFileTooLarge = false;
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: JPG creation aborted! Preallocated buffer not sufficient: " + std::to_string(MAX_JPG_SIZE));
}
memCopy((uint8_t*) ii, (uint8_t*) i, sizeof(ImageData)); memCopy((uint8_t*) ii, (uint8_t*) i, sizeof(ImageData));
delete ii; delete ii;
} }
@@ -364,7 +381,7 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
RGBImageLock(); RGBImageLock();
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::CreateEmptyImage"); LogFile.WriteHeapInfo("CreateEmptyImage");
#endif #endif
int memsize = width * height * channels; int memsize = width * height * channels;
@@ -372,9 +389,8 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
if (rgb_image == NULL) if (rgb_image == NULL)
{ {
//ESP_LOGE(TAG, "CImageBasis::CreateEmptyImage: No more free memory!! Needed: %d %d %d %d", width, height, channels, memsize); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CreateEmptyImage: Can't allocate enough memory: " + std::to_string(memsize));
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis::CreateEmptyImage: Can't allocate enough memory: " + std::to_string(memsize)); LogFile.WriteHeapInfo("CreateEmptyImage");
LogFile.WriteHeapInfo("CImageBasis::CreateEmptyImage");
RGBImageRelease(); RGBImageRelease();
return; return;
} }
@@ -396,7 +412,7 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
void CImageBasis::EmptyImage() void CImageBasis::EmptyImage()
{ {
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::EmptyImage"); LogFile.WriteHeapInfo("EmptyImage");
#endif #endif
stbi_uc* p_source; stbi_uc* p_source;
@@ -430,7 +446,7 @@ void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
{ {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image with size 0 loaded --> reboot to be done! " LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image with size 0 loaded --> reboot to be done! "
"Check that your camera module is working and connected properly."); "Check that your camera module is working and connected properly.");
LogFile.WriteHeapInfo("CImageBasis::LoadFromMemory"); LogFile.WriteHeapInfo("LoadFromMemory");
doReboot(); doReboot();
} }
@@ -450,7 +466,7 @@ CImageBasis::CImageBasis(CImageBasis *_copyfrom)
RGBImageLock(); RGBImageLock();
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::CImageBasis_copyfrom - Start"); LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start");
#endif #endif
int memsize = width * height * channels; int memsize = width * height * channels;
@@ -458,8 +474,8 @@ CImageBasis::CImageBasis(CImageBasis *_copyfrom)
if (rgb_image == NULL) if (rgb_image == NULL)
{ {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis::CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize)); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize));
LogFile.WriteHeapInfo("CImageBasis::CImageBasis-Copyfrom"); LogFile.WriteHeapInfo("CImageBasis-Copyfrom");
RGBImageRelease(); RGBImageRelease();
return; return;
} }
@@ -468,7 +484,7 @@ CImageBasis::CImageBasis(CImageBasis *_copyfrom)
RGBImageRelease(); RGBImageRelease();
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::CImageBasis_copyfrom - done"); LogFile.WriteHeapInfo("CImageBasis_copyfrom - done");
#endif #endif
} }
@@ -485,7 +501,7 @@ CImageBasis::CImageBasis(int _width, int _height, int _channels)
RGBImageLock(); RGBImageLock();
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::CImageBasis_width,height,ch - Start"); LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start");
#endif #endif
int memsize = width * height * channels; int memsize = width * height * channels;
@@ -493,8 +509,8 @@ CImageBasis::CImageBasis(int _width, int _height, int _channels)
if (rgb_image == NULL) if (rgb_image == NULL)
{ {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis::CImageBasis-width,height,ch: Can't allocate enough memory: " + std::to_string(memsize)); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: Can't allocate enough memory: " + std::to_string(memsize));
LogFile.WriteHeapInfo("CImageBasis::CImageBasis-width,height,ch"); LogFile.WriteHeapInfo("CImageBasis-width,height,ch");
RGBImageRelease(); RGBImageRelease();
return; return;
} }
@@ -502,7 +518,7 @@ CImageBasis::CImageBasis(int _width, int _height, int _channels)
RGBImageRelease(); RGBImageRelease();
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::CImageBasis_width,height,ch - done"); LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done");
#endif #endif
} }
@@ -522,14 +538,14 @@ CImageBasis::CImageBasis(std::string _image)
RGBImageLock(); RGBImageLock();
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::CImageBasis_image - Start"); LogFile.WriteHeapInfo("CImageBasis_image - Start");
#endif #endif
rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels); rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
if (rgb_image == NULL) { if (rgb_image == NULL) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis::CImageBasis-image: Failed to load " + _image + "! Is it corrupted?"); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?");
LogFile.WriteHeapInfo("CImageBasis::CImageBasis-image"); LogFile.WriteHeapInfo("CImageBasis-image");
RGBImageRelease(); RGBImageRelease();
return; return;
} }
@@ -543,7 +559,7 @@ CImageBasis::CImageBasis(std::string _image)
#endif #endif
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("CImageBasis::CImageBasis_image - done"); LogFile.WriteHeapInfo("CImageBasis_image - done");
#endif #endif
} }
@@ -643,7 +659,7 @@ void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
{ {
if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels)) if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
{ {
ESP_LOGE(TAG, "CImageBasis::Resize - Target image size does not fit!"); ESP_LOGE(TAG, "Resize - Target image size does not fit!");
return; return;
} }