Use function directly 52/306052/4
authorminje.ahn <minje.ahn@samsung.com>
Thu, 15 Feb 2024 23:57:45 +0000 (08:57 +0900)
committerminje.ahn <minje.ahn@samsung.com>
Fri, 16 Feb 2024 08:26:26 +0000 (17:26 +0900)
1. Use g_free instead macro
2. Use g_strlcpy instead macro
3. Remove meaningless NULL setting

Change-Id: I58fe770c17242871041a8ca424e40052985fa9d1
Signed-off-by: minje.ahn <minje.ahn@samsung.com>
15 files changed:
anim/test/mm_util_anim_testsuite.c
common/include/mm_util_private.h
common/mm_util_image.c
common/mm_util_private.c
gif/mm_util_gif.c
gif/test/mm_util_gif_testsuite.c
heif/test/mm_util_heif_testsuite.c
imgcv/mm_util_imgcv.cpp
imgp/mm_util_imgp.c
imgp/test/mm_util_imgp_testsuite.c
jpeg/test/mm_util_jpeg_testsuite.c
jxl/test/mm_util_jxl_testsuite.c
magick/mm_util_info.c
magick/mm_util_magick.c
magick/test/mm_util_magick_testsuite.c

index 321a992..f7eae54 100644 (file)
@@ -284,7 +284,7 @@ static bool __test_encode_to_buffer(mm_util_img_codec_type type, const char *pat
        }
 
        mm_util_anim_enc_destroy(anim_enc);
-       MMUTIL_SAFE_FREE(encoded_data);
+       g_free(encoded_data);
 
        fprintf(stderr, "[ANIM_testsuite] The test finished.\n");
 
@@ -328,7 +328,7 @@ int main(int argc, char *argv[])
        }
 
 out:
-       MMUTIL_SAFE_FREE(g_path);
+       g_free(g_path);
        g_queue_free_full(g_queue_images, mm_image_destroy_image);
        g_queue_free_full(g_queue_files, g_free);
 
index c2038e7..76ea992 100644 (file)
@@ -23,7 +23,6 @@
 #define __MM_UTIL_PRIVATE_H__
 
 #include <stdio.h>
-#include <glib.h>
 #include "mm_util_type.h"
 #include "mm_util_debug.h"
 #include "mm_util_image.h"
@@ -33,17 +32,7 @@ extern "C" {
 #endif
 
 
-/* for memory management */
-#define MMUTIL_SAFE_FREE(x) do { \
-               g_free(x); \
-               x = NULL; \
-       } while(0)
-#define MMUTIL_SAFE_IMAGE_FREE(x) do { \
-               mm_image_destroy_image(x); \
-               x = NULL; \
-       } while(0)
-#define MMUTIL_SAFE_STRCPY(dst, src, n)  g_strlcpy(dst, src, n)
-#define MMUTIL_STRING_VALID(str)       (str != NULL && strlen(str) > 0)
+#define MMUTIL_STRING_VALID(str)         (str != NULL && strlen(str) > 0)
 
 /* for alignment */
 #define MM_UTIL_ROUND_UP_2(num) (((num)+1)&~1)
index ca63484..8f5b38d 100755 (executable)
@@ -15,6 +15,7 @@
 */
 
 #include <stdio.h>
+#include <glib.h>
 
 #include "mm_util_private.h"
 
index cbe5551..1d92b42 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include <stdio.h>
+#include <glib.h>
 #include <iniparser.h>
 
 #include "mm_util_private.h"
@@ -49,11 +50,11 @@ int mm_util_safe_fopen(const char *path, const char *mode, FILE **fp)
                        }
                        if (strncmp(path, _realpath, strlen(path))) {
                                mm_util_error("file is symbolic link");
-                               MMUTIL_SAFE_FREE(_realpath);
+                               free(_realpath);
                                return MM_UTIL_ERROR_NO_SUCH_FILE;
                        }
 
-                       MMUTIL_SAFE_FREE(_realpath);
+                       free(_realpath);
                } else {
                        mm_util_sec_debug("file(%s) will be created", path);
                }
index 5753b7e..e0f2991 100644 (file)
@@ -737,7 +737,7 @@ int mm_util_gif_encode_save_to_file(mm_gif_file_h gif_file_h, const char *path)
        ret = __write_gif_to_file(gif_file, path);
        mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "__write_gif_to_file failed (%d)", ret);
 
-       MMUTIL_SAFE_FREE(gif_file->io_buf.buf);
+       g_clear_pointer(&gif_file->io_buf.buf, g_free);
        gif_file->io_buf.buf_size = 0;
 
        return ret;
@@ -762,7 +762,7 @@ int mm_util_gif_encode_save_to_buffer(mm_gif_file_h gif_file_h, void **buffer, s
        ret = __write_gif_to_buffer(gif_file, buffer, buffer_size);
        mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "__write_gif_to_buffer failed (%d)", ret);
 
-       MMUTIL_SAFE_FREE(gif_file->io_buf.buf);
+       g_clear_pointer(&gif_file->io_buf.buf, g_free);
        gif_file->io_buf.buf_size = 0;
 
        return ret;
@@ -853,7 +853,7 @@ void mm_util_gif_encode_destroy(mm_gif_file_h gif_file_h)
        }
 
        g_free(gif_file->filename);
-       MMUTIL_SAFE_FREE(gif_file->io_buf.buf);
+       g_free(gif_file->io_buf.buf);
        g_free(gif_file);
 }
 
index 8790ae5..2dce135 100644 (file)
@@ -122,10 +122,10 @@ bool _test_decode(const mm_util_test_mode_e mode)
                ret = mm_util_file_write(DECODE_FILE_PATH, data, size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[GIF_testsuite] mm_util_file_write failed %d\n", ret);
-                       MMUTIL_SAFE_FREE(data);
+                       g_free(data);
                        return false;
                }
-               MMUTIL_SAFE_FREE(data);
+               g_free(data);
        } else if (mode == TEST_DECODE_BUFFER) {
                ret = mm_util_file_read(g_path, &g_read_data, &g_read_size);
                if (ret != MM_UTIL_ERROR_NONE) {
@@ -146,10 +146,10 @@ bool _test_decode(const mm_util_test_mode_e mode)
                ret = mm_util_file_write(DECODE_MEM_PATH, data, size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[GIF_testsuite] mm_util_file_write failed %d\n", ret);
-                       MMUTIL_SAFE_FREE(data);
+                       g_free(data);
                        return false;
                }
-               MMUTIL_SAFE_FREE(data);
+               g_free(data);
        }
 
        return true;
@@ -188,19 +188,19 @@ bool _test_encode(const mm_util_test_mode_e mode)
                ret = mm_util_encode_to_gif_memory(&g_decoded_data, 1, &encoded_data, &encoded_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[GIF_testsuite] mm_util_encode_gif_to_memory failed : %d\n", ret);
-                       MMUTIL_SAFE_FREE(encoded_data);
+                       g_free(encoded_data);
                        return false;
                }
 
                ret = mm_util_file_write(ENCODE_MEM_PATH, encoded_data, encoded_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[GIF_testsuite] mm_util_file_write failed %d\n", ret);
-                       MMUTIL_SAFE_FREE(encoded_data);
+                       g_free(encoded_data);
                        return false;
                }
        }
 
-       MMUTIL_SAFE_FREE(encoded_data);
+       g_free(encoded_data);
        return true;
 }
 
@@ -232,8 +232,10 @@ bool _test_auto()
 
                fprintf(stderr, "\t[GIF_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST SUCCESS\n", MODE_TO_STR[test_mode]);
 
-               MMUTIL_SAFE_FREE(g_read_data);
-               MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+               g_free(g_read_data);
+               g_read_data = NULL;
+               mm_image_destroy_image(g_decoded_data);
+               g_decoded_data = NULL;
                test_mode++;
        }
 
@@ -276,9 +278,9 @@ int main(int argc, char *argv[])
        }
 
 out:
-       MMUTIL_SAFE_FREE(g_path);
-       MMUTIL_SAFE_FREE(g_read_data);
-       MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+       g_free(g_path);
+       g_free(g_read_data);
+       mm_image_destroy_image(g_decoded_data);
 
        return 0;
 }
index 5565d17..e6f32cb 100644 (file)
@@ -174,8 +174,8 @@ static bool __test_auto()
                if ((test_mode == TEST_DECODE_FILE) || (test_mode == TEST_DECODE_BUFFER)) {
                        for (color = MM_UTIL_COLOR_YUV420; color < MM_UTIL_COLOR_NUM; color++) {
                                result |= __test_decode(test_mode, color, g_test_filename[test_mode][color]);
-                               MMUTIL_SAFE_FREE(g_read_data);
-                               MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+                               g_clear_pointer(&g_read_data, g_free);
+                               g_clear_pointer(&g_decoded_data, mm_image_destroy_image);
                        }
                }
 
@@ -214,8 +214,8 @@ int main(int argc, char *argv[])
 
 out:
        g_free(g_path);
-       MMUTIL_SAFE_FREE(g_read_data);
-       MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+       g_free(g_read_data);
+       mm_image_destroy_image(g_decoded_data);
 
        return 0;
 }
index 5f36ef3..2fd546c 100755 (executable)
@@ -20,6 +20,7 @@
  */
 #include <limits.h>
 #include <math.h>
+#include <glib.h>
 #include "mm_util_private.h"
 #include "mm_util_imgcv.h"
 #include "mm_util_imgcv_internal.h"
index e8876b5..6970a38 100644 (file)
@@ -378,7 +378,7 @@ int mm_util_convert_colorspace(mm_util_image_h src, mm_util_color_format_e color
 ERROR:
        /* Finalize */
        __mm_util_imgp_finalize(_module, _imgp_info_s);
-       MMUTIL_SAFE_FREE(output_buffer);
+       g_free(output_buffer);
 
        mm_util_fleave();
 
@@ -457,7 +457,7 @@ int mm_util_resize_image(mm_util_image_h src, unsigned int width, unsigned int h
 ERROR:
        /* Finalize */
        __mm_util_imgp_finalize(_module, _imgp_info_s);
-       MMUTIL_SAFE_FREE(output_buffer);
+       g_free(output_buffer);
 
        mm_util_fleave();
 
@@ -557,7 +557,7 @@ int mm_util_rotate_image(mm_util_image_h src, mm_util_rotate_type_e angle, mm_ut
 ERROR:
        /* Finalize */
        __mm_util_imgp_finalize(_module, _imgp_info_s);
-       MMUTIL_SAFE_FREE(output_buffer);
+       g_free(output_buffer);
 
        mm_util_fleave();
 
index 2019022..26a61e5 100644 (file)
@@ -243,12 +243,12 @@ int main(int argc, char *argv[])
        }
 
 TEST_FAIL:
-       MMUTIL_SAFE_IMAGE_FREE(_src);
-       MMUTIL_SAFE_IMAGE_FREE(_dst);
-       MMUTIL_SAFE_FREE(g_transformed.data);
-       MMUTIL_SAFE_FREE(g_args.data);
-       MMUTIL_SAFE_FREE(g_args.cmd);
-       MMUTIL_SAFE_FREE(g_args.path);
+       mm_image_destroy_image(_src);
+       mm_image_destroy_image(_dst);
+       g_free(g_transformed.data);
+       g_free(g_args.data);
+       g_free(g_args.cmd);
+       g_free(g_args.path);
 
        return 0;
 }
index a23e110..8c2f6f4 100644 (file)
@@ -146,10 +146,10 @@ bool _test_decode(const mm_util_test_mode_e mode)
                ret = mm_util_file_write(DECODE_FILE_PATH, data, size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[JPEG_testsuite] mm_util_file_write failed %d\n", ret);
-                       MMUTIL_SAFE_FREE(data);
+                       g_free(data);
                        return false;
                }
-               MMUTIL_SAFE_FREE(data);
+               g_free(data);
        } else if (mode == TEST_DECODE_BUFFER) {
                ret = mm_util_file_read(g_path, &g_read_data, &g_read_size);
                if (ret != MM_UTIL_ERROR_NONE) {
@@ -170,10 +170,10 @@ bool _test_decode(const mm_util_test_mode_e mode)
                ret = mm_util_file_write(DECODE_MEM_PATH, data, size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[JPEG_testsuite] mm_util_file_write failed %d\n", ret);
-                       MMUTIL_SAFE_FREE(data);
+                       g_free(data);
                        return false;
                }
-               MMUTIL_SAFE_FREE(data);
+               g_free(data);
        }
 
        return true;
@@ -212,18 +212,18 @@ bool _test_encode(const mm_util_test_mode_e mode)
                ret = mm_util_encode_to_jpeg_memory(g_decoded_data, (int)g_quality, &encoded_data, &encoded_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[JPEG_testsuite] mm_util_jpeg_encode_to_file failed : %d\n", ret);
-                       MMUTIL_SAFE_FREE(encoded_data);
+                       g_free(encoded_data);
                        return false;
                }
                ret = mm_util_file_write(ENCODE_MEM_PATH, encoded_data, encoded_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "\t[JPEG_testsuite] mm_util_file_write failed %d\n", ret);
-                       MMUTIL_SAFE_FREE(encoded_data);
+                       g_free(encoded_data);
                        return false;
                }
        }
 
-       MMUTIL_SAFE_FREE(encoded_data);
+       g_free(encoded_data);
        return true;
 }
 
@@ -255,8 +255,10 @@ bool _test_auto()
 
                fprintf(stderr, "\t[JPEG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST SUCCESS\n", MODE_TO_STR[test_mode]);
 
-               MMUTIL_SAFE_FREE(g_read_data);
-               MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+               g_free(g_read_data);
+               g_read_data = NULL;
+               mm_image_destroy_image(g_decoded_data);
+               g_decoded_data = NULL;
                test_mode++;
        }
 
@@ -300,8 +302,8 @@ int main(int argc, char *argv[])
 
 out:
        g_free(g_path);
-       MMUTIL_SAFE_FREE(g_read_data);
-       MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+       g_free(g_read_data);
+       mm_image_destroy_image(g_decoded_data);
 
        return 0;
 }
index 1ce9ff6..832d262 100644 (file)
@@ -186,8 +186,10 @@ static bool __test_decode(const mm_util_test_mode_e mode, mm_util_color_format_e
 {
        int ret = 0;
 
-       MMUTIL_SAFE_FREE(g_read_data);
-       MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+       g_free(g_read_data);
+       g_read_data = NULL;
+       mm_image_destroy_image(g_decoded_data);
+       g_decoded_data = NULL;
 
        if ((mode != TEST_DECODE_FILE) && (mode != TEST_DECODE_BUFFER))
                return true;
@@ -242,14 +244,14 @@ static bool __test_encode(const mm_util_test_mode_e mode, mm_util_color_format_e
                ret = mm_util_encode_jxl_to_buffer(g_decoded_data, g_enc_opt, &encoded_data, &encoded_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        g_print("\t[JXL_testsuite] mm_util_encode_jxl_to_buffer failed : %d\n", ret);
-                       MMUTIL_SAFE_FREE(encoded_data);
+                       g_free(encoded_data);
                        return false;
                }
                ret = mm_util_file_write(g_test_filename[mode][color], encoded_data, encoded_size);
                if (ret != MM_UTIL_ERROR_NONE)
                        g_print("\t[JXL_testsuite] mm_util_file_write failed : %d\n", ret);
 
-               MMUTIL_SAFE_FREE(encoded_data);
+               g_free(encoded_data);
        }
 
        return true;
@@ -327,9 +329,9 @@ int main(int argc, char *argv[])
        }
 
 out:
-       MMUTIL_SAFE_FREE(g_path);
-       MMUTIL_SAFE_FREE(g_read_data);
-       MMUTIL_SAFE_IMAGE_FREE(g_decoded_data);
+       g_free(g_path);
+       g_free(g_read_data);
+       mm_image_destroy_image(g_decoded_data);
        mm_util_enc_opt_destroy(g_enc_opt);
 
        return 0;
index 1c5f233..aa81153 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include <stdio.h>
+#include <glib.h>
 #include "mm_util_private.h"
 #include "mm_util_magick.h"
 
index e82d03b..ea3eb53 100644 (file)
@@ -355,13 +355,16 @@ static int __mm_util_write_image_to_file(Image *image, mm_util_enc_opt_t *option
        mm_util_retvm_if(!image, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
        mm_util_retvm_if(!MMUTIL_STRING_VALID(out_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid out_path");
 
+       if (g_strlcpy(image->filename, out_path, sizeof(image->filename)) >= MaxTextExtent) {
+               mm_util_error("Error: truncation occurred");
+               return MM_UTIL_ERROR_INVALID_OPERATION;
+       }
+
        GetExceptionInfo(&exception);
 
        _image_info = CloneImageInfo(0);
        mm_util_retvm_if(!_image_info, MM_UTIL_ERROR_INVALID_OPERATION, "Error: CloneImageInfo failed.");
 
-       MMUTIL_SAFE_STRCPY(image->filename, out_path, sizeof(image->filename));
-
        DeleteImageProfile(image, "EXIF");
        DeleteImageProfile(image, "8BIM");
        DeleteImageProfile(image, "ICM");
index 6696478..fdb1363 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <glib.h>
 #include <mm_util_magick.h>
 #include <mm_util_private.h>
 
@@ -108,9 +109,7 @@ static void __save_to_file(mm_util_image_h handle, const char *out_path)
        if (ret != MM_UTIL_ERROR_NONE)
                printf("Fail mm_util_file_write [%d]\n", ret);
 
-       MMUTIL_SAFE_FREE(buffer);
-
-       return;
+       g_free(buffer);
 }
 
 static int __get_buffer_for_test(mm_util_color_format_e req_format, unsigned char **buffer,
@@ -130,7 +129,7 @@ static int __get_buffer_for_test(mm_util_color_format_e req_format, unsigned cha
        if (ret != MM_UTIL_ERROR_NONE) {
                printf("Fail mm_image_get_image [%d]\n", ret);
                mm_image_destroy_image(dst_handle);
-               MMUTIL_SAFE_FREE(*buffer);
+               g_free(*buffer);
                return ret;
        }
 
@@ -239,7 +238,7 @@ static int __magick_rotate_B_P_test(mm_util_rotate_type_e rotation, mm_util_colo
        ret = mm_image_create_image(width, height, format, buffer, size, &src_handle);
        if (ret != MM_UTIL_ERROR_NONE) {
                printf("Fail mm_image_create_image [%d]\n", ret);
-               MMUTIL_SAFE_FREE(buffer);
+               g_free(buffer);
                return ret;
        }
 
@@ -249,7 +248,7 @@ static int __magick_rotate_B_P_test(mm_util_rotate_type_e rotation, mm_util_colo
 
        mm_image_destroy_image(src_handle);
 
-       MMUTIL_SAFE_FREE(buffer);
+       g_free(buffer);
 
        return ret;
 }
@@ -278,7 +277,7 @@ static int __magick_resize_B_P_test(unsigned int req_width, unsigned int req_hei
        ret = mm_image_create_image(width, height, format, buffer, size, &src_handle);
        if (ret != MM_UTIL_ERROR_NONE) {
                printf("Fail mm_image_create_image [%d]\n", ret);
-               MMUTIL_SAFE_FREE(buffer);
+               g_free(buffer);
                return ret;
        }
 
@@ -288,7 +287,7 @@ static int __magick_resize_B_P_test(unsigned int req_width, unsigned int req_hei
 
        mm_image_destroy_image(src_handle);
 
-       MMUTIL_SAFE_FREE(buffer);
+       g_free(buffer);
 
        return ret;
 }
@@ -315,7 +314,7 @@ static int __magick_rotate_B_B_test(mm_util_rotate_type_e rotation, mm_util_colo
        ret = mm_image_create_image(width, height, format, buffer, size, &src_handle);
        if (ret != MM_UTIL_ERROR_NONE) {
                printf("Fail mm_image_create_image [%d]\n", ret);
-               MMUTIL_SAFE_FREE(buffer);
+               g_free(buffer);
                return ret;
        }
 
@@ -323,7 +322,7 @@ static int __magick_rotate_B_B_test(mm_util_rotate_type_e rotation, mm_util_colo
        if (ret != MM_UTIL_ERROR_NONE)
                printf("Fail mm_util_rotate_B_B [%d]\n", ret);
 
-       MMUTIL_SAFE_FREE(buffer);
+       g_free(buffer);
 
        __save_to_file(dst_handle, __get_dst_raw_path(rotation));
 
@@ -358,7 +357,7 @@ static int __magick_resize_B_B_test(unsigned int req_width, unsigned int req_hei
        ret = mm_image_create_image(width, height, format, buffer, size, &src_handle);
        if (ret != MM_UTIL_ERROR_NONE) {
                printf("Fail mm_image_create_image [%d]\n", ret);
-               MMUTIL_SAFE_FREE(buffer);
+               g_free(buffer);
                return ret;
        }
 
@@ -366,7 +365,7 @@ static int __magick_resize_B_B_test(unsigned int req_width, unsigned int req_hei
        if (ret != MM_UTIL_ERROR_NONE)
                printf("Fail mm_util_resize_B_B [%d]\n", ret);
 
-       MMUTIL_SAFE_FREE(buffer);
+       g_free(buffer);
 
        __save_to_file(dst_handle, dst_path);
 
@@ -401,7 +400,7 @@ static int __magick_convert_B_B_test(mm_util_color_format_e in_format, mm_util_c
        ret = mm_image_create_image(width, height, format, buffer, size, &src_handle);
        if (ret != MM_UTIL_ERROR_NONE) {
                printf("Fail mm_image_create_image [%d]\n", ret);
-               MMUTIL_SAFE_FREE(buffer);
+               g_free(buffer);
                return ret;
        }
 
@@ -409,7 +408,7 @@ static int __magick_convert_B_B_test(mm_util_color_format_e in_format, mm_util_c
        if (ret != MM_UTIL_ERROR_NONE)
                printf("Fail mm_util_convert_B_B [%d]\n", ret);
 
-       MMUTIL_SAFE_FREE(buffer);
+       g_free(buffer);
 
        __save_to_file(dst_handle, dst_path);
 
@@ -463,7 +462,7 @@ static int __magick_decode_buffer_test(const char *path, mm_util_color_format_e
        if (ret != MM_UTIL_ERROR_NONE)
                printf("Fail mm_util_decode_image_from_buffer [0x%x]\n", ret);
 
-       MMUTIL_SAFE_FREE(src_buffer);
+       g_free(src_buffer);
        return ret;
 }
 
@@ -553,7 +552,7 @@ static int __magick_encode_buffer_test(mm_util_image_h decoded_image, mm_util_im
 
 END:
        mm_util_enc_opt_destroy(enc_option);
-       MMUTIL_SAFE_FREE(buffer);
+       g_free(buffer);
 
        return ret;
 }
@@ -799,4 +798,3 @@ int main(int argc, char *argv[])
 
        return ret;
 }
-