From c5900e36fae8ef7b0d5b9cc592f05c35294a1dbb Mon Sep 17 00:00:00 2001 From: woody Date: Wed, 20 Oct 2021 16:40:40 +0800 Subject: [PATCH] fix(png) memory leak for sjpg and use lv_mem_... in lv_png (#2704) * Unified format * fix memory leak for lv_sjpg.c * unified format for lib_png --- src/extra/libs/png/lodepng.c | 20 +++++++++++--------- src/extra/libs/png/lv_png.c | 4 ++-- src/extra/libs/sjpg/lv_sjpg.c | 6 +++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/extra/libs/png/lodepng.c b/src/extra/libs/png/lodepng.c index 19c231239..e88161fc6 100644 --- a/src/extra/libs/png/lodepng.c +++ b/src/extra/libs/png/lodepng.c @@ -76,7 +76,7 @@ static void* lodepng_malloc(size_t size) { #ifdef LODEPNG_MAX_ALLOC if(size > LODEPNG_MAX_ALLOC) return 0; #endif - return malloc(size); + return lv_mem_alloc(size); } /* NOTE: when realloc returns NULL, it leaves the original memory untouched */ @@ -84,11 +84,11 @@ static void* lodepng_realloc(void* ptr, size_t new_size) { #ifdef LODEPNG_MAX_ALLOC if(new_size > LODEPNG_MAX_ALLOC) return 0; #endif - return realloc(ptr, new_size); + return lv_mem_realloc(ptr, new_size); } static void lodepng_free(void* ptr) { - free(ptr); + lv_mem_free(ptr); } #else /*LODEPNG_COMPILE_ALLOCATORS*/ /* TODO: support giving additional void* payload to the custom allocators */ @@ -348,7 +348,7 @@ static long lodepng_filesize(const char* filename) { lv_fs_res_t res = lv_fs_open(&f, filename, LV_FS_MODE_RD); if(res != LV_FS_RES_OK) return -1; uint32_t size = 0; - if(lv_fs_seek(&f, 0, SEEK_END) != 0) { + if(lv_fs_seek(&f, 0, LV_FS_SEEK_END) != 0) { lv_fs_close(&f); return -1; } @@ -385,11 +385,13 @@ unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* fil /*write given buffer to the file, overwriting the file, it doesn't append to it.*/ unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) { - FILE* file; - file = fopen(filename, "wb" ); - if(!file) return 79; - fwrite(buffer, 1, buffersize, file); - fclose(file); + lv_fs_file_t f; + lv_fs_res_t res = lv_fs_open(&f, filename, LV_FS_MODE_WR); + if(res != LV_FS_RES_OK) return 79; + + uint32_t bw; + res = lv_fs_write(&f, buffer, buffersize, &bw); + lv_fs_close(&f); return 0; } diff --git a/src/extra/libs/png/lv_png.c b/src/extra/libs/png/lv_png.c index 64a567a0a..0448764ac 100644 --- a/src/extra/libs/png/lv_png.c +++ b/src/extra/libs/png/lv_png.c @@ -146,7 +146,7 @@ static lv_res_t decoder_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * /*Decode the loaded image in ARGB8888 */ error = lodepng_decode32(&img_data, &png_width, &png_height, png_data, png_data_size); - free(png_data); /*Free the loaded file*/ + lv_mem_free(png_data); /*Free the loaded file*/ if(error) { printf("error %u: %s\n", error, lodepng_error_text(error)); return LV_RES_INV; @@ -188,7 +188,7 @@ static void decoder_close(lv_img_decoder_t *decoder, lv_img_decoder_dsc_t *dsc) { (void)decoder; /*Unused*/ if (dsc->img_data) { - free((uint8_t *)dsc->img_data); + lv_mem_free((uint8_t *)dsc->img_data); dsc->img_data = NULL; } } diff --git a/src/extra/libs/sjpg/lv_sjpg.c b/src/extra/libs/sjpg/lv_sjpg.c index 1e485a1bc..8d8e8f8e5 100644 --- a/src/extra/libs/sjpg/lv_sjpg.c +++ b/src/extra/libs/sjpg/lv_sjpg.c @@ -833,11 +833,11 @@ static void decoder_close( lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * ds if(sjpeg->io.lv_file.file_d) { lv_fs_close(&(sjpeg->io.lv_file)); } - lv_sjpg_free(sjpeg); + lv_sjpg_cleanup(sjpeg); break; case LV_IMG_SRC_VARIABLE: - lv_sjpg_free(sjpeg); + lv_sjpg_cleanup(sjpeg); break; default: @@ -847,7 +847,7 @@ static void decoder_close( lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * ds static int is_jpg( const uint8_t *raw_data ) { - const uint8_t jpg_signature[] = {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46};//ÿØÿà�JFIF + const uint8_t jpg_signature[] = {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46}; return memcmp( jpg_signature, raw_data, sizeof( jpg_signature ) ) == 0; }