Fix memory leack for reallocation and infinite loop in foreach 78/140278/2 accepted/tizen/4.0/unified/20170816.012134 accepted/tizen/4.0/unified/20170816.015117 accepted/tizen/unified/20170728.195105 submit/tizen/20170726.032629 submit/tizen_4.0/20170811.094300 submit/tizen_4.0/20170814.115522 submit/tizen_4.0_unified/20170814.115522
authorJiyong Min <jiyong.min@samsung.com>
Mon, 24 Jul 2017 09:32:11 +0000 (18:32 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Tue, 25 Jul 2017 00:48:36 +0000 (09:48 +0900)
Change-Id: Ifc905abc999daf8cff8f3cb901d5312fabdc8c40
Signed-off-by: Jiyong Min <jiyong.min@samsung.com>
packaging/capi-media-image-util.spec
src/image_util.c

index e15c657..1b70a28 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-media-image-util
 Summary:    A Image Utility library in Tizen Native API
-Version:    0.1.21
+Version:    0.1.22
 Release:    2
 Group:      Multimedia/API
 License:    Apache-2.0
index a099a6b..f452830 100755 (executable)
@@ -247,16 +247,16 @@ static gboolean _is_supported_colorspace(image_util_colorspace_e colorspace, ima
 {
        switch (type) {
        case IMAGE_UTIL_JPEG:
-               image_util_retvm_if((_convert_jpeg_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[jpeg]not supported format");
+               image_util_retvm_if((_convert_jpeg_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[jpeg] %d not supported format", colorspace);
                break;
        case IMAGE_UTIL_PNG:
-               image_util_retvm_if((_convert_png_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[png]not supported format");
+               image_util_retvm_if((_convert_png_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[png] %d not supported format", colorspace);
                break;
        case IMAGE_UTIL_GIF:
-               image_util_retvm_if((_convert_gif_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[gif]not supported format");
+               image_util_retvm_if((_convert_gif_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[gif] %d not supported format", colorspace);
                break;
        case IMAGE_UTIL_BMP:
-               image_util_retvm_if((_convert_bmp_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[bmp]not supported format");
+               image_util_retvm_if((_convert_bmp_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[bmp] %d not supported format", colorspace);
                break;
        default:
                image_util_retvm_if((_convert_bmp_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "Invalid image type");
@@ -266,7 +266,7 @@ static gboolean _is_supported_colorspace(image_util_colorspace_e colorspace, ima
 
 int image_util_foreach_supported_jpeg_colorspace(image_util_supported_jpeg_colorspace_cb callback, void *user_data)
 {
-       image_util_colorspace_e idx = _NOT_SUPPORTED_COLORSPACE;
+       int idx = _NOT_SUPPORTED_COLORSPACE;
 
        DEPRECATION_LOGW("image_util_foreach_supported_jpeg_colorspace()", "image_util_foreach_supported_colorspace()");
 
@@ -866,7 +866,7 @@ int image_util_extract_color_from_memory(const unsigned char *image_buffer, int
 
 int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data)
 {
-       image_util_colorspace_e idx = _NOT_SUPPORTED_COLORSPACE;
+       int idx = _NOT_SUPPORTED_COLORSPACE;
 
        image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
 
@@ -1687,16 +1687,17 @@ int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long w
                        }
 
                        if (_handle->image_count <= _handle->current_resolution_count) {
-                               gif_data->frames = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
-                               if (gif_data->frames == NULL) {
+                               mm_util_gif_frame_data **tmp_buffer = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
+                               if (tmp_buffer == NULL) {
                                        image_util_error("Error - OUT_OF_MEMORY");
-                                       IMAGE_UTIL_SAFE_FREE(_handle);
+                                       _image_util_encode_destroy_gif_buffer(gif_data);
                                        return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
                                }
+                               gif_data->frames = tmp_buffer;
                                gif_data->frames[_handle->image_count] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data));
                                if (gif_data->frames[_handle->image_count] == NULL) {
                                        image_util_error("Error - OUT_OF_MEMORY");
-                                       IMAGE_UTIL_SAFE_FREE(_handle);
+                                       _image_util_encode_destroy_gif_buffer(gif_data);
                                        return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
                                }
                                _handle->image_count++;
@@ -1830,16 +1831,17 @@ int image_util_encode_set_gif_frame_delay_time(image_util_encode_h handle, unsig
                return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
        }
        if (_handle->image_count <= _handle->current_delay_count) {
-               gif_data->frames = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
-               if (gif_data->frames == NULL) {
+               mm_util_gif_frame_data **tmp_buffer = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
+               if (tmp_buffer == NULL) {
                        image_util_error("Error - OUT_OF_MEMORY");
-                       IMAGE_UTIL_SAFE_FREE(_handle);
+                       _image_util_encode_destroy_gif_buffer(gif_data);
                        return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
                }
+               gif_data->frames = tmp_buffer;
                gif_data->frames[_handle->image_count] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data));
                if (gif_data->frames[_handle->image_count] == NULL) {
                        image_util_error("Error - OUT_OF_MEMORY");
-                       IMAGE_UTIL_SAFE_FREE(_handle);
+                       _image_util_encode_destroy_gif_buffer(gif_data);
                        return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
                }
                _handle->image_count++;
@@ -1867,6 +1869,7 @@ int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigne
 
        /* initialize buffer and value for source buffer */
        if (_handle->image_type == IMAGE_UTIL_GIF) {
+               void *tmp_buffer = NULL;
                if (_handle->is_encoded) {
                        _image_util_encode_destroy_gif_buffer(_handle->image_h);
                        err = _image_util_encode_create_gif_buffer(_handle->image_h);
@@ -1880,10 +1883,10 @@ int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigne
                        _handle->current_resolution_count = 0;
                        _handle->current_delay_count = 0;
                }
-               _handle->src_buffer = (void *)realloc(_handle->src_buffer, (_handle->current_buffer_count + 1) * sizeof(void *));
-
-               if (_handle->src_buffer != NULL)
-                       _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
+               tmp_buffer = (void *)realloc(_handle->src_buffer, (_handle->current_buffer_count + 1) * sizeof(void *));
+               image_util_retvm_if((tmp_buffer == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "memory reallocation failed");
+               _handle->src_buffer = tmp_buffer;
+               _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
        } else {
                if (_handle->src_buffer == NULL)
                        _handle->src_buffer = (void *)calloc(1, sizeof(void *));
@@ -1903,16 +1906,17 @@ int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigne
                        return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
                }
                if (_handle->image_count <= _handle->current_buffer_count) {
-                       gif_data->frames = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
-                       if (gif_data->frames == NULL) {
+                       mm_util_gif_frame_data **tmp_buffer = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
+                       if (tmp_buffer == NULL) {
                                image_util_error("Error - OUT_OF_MEMORY");
-                               IMAGE_UTIL_SAFE_FREE(_handle);
+                               _image_util_encode_destroy_gif_buffer(gif_data);
                                return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
                        }
+                       gif_data->frames = tmp_buffer;
                        gif_data->frames[_handle->image_count] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data));
                        if (gif_data->frames[_handle->image_count] == NULL) {
                                image_util_error("Error - OUT_OF_MEMORY");
-                               IMAGE_UTIL_SAFE_FREE(_handle);
+                               _image_util_encode_destroy_gif_buffer(gif_data);
                                return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
                        }
                        _handle->image_count++;