Fix coverity issue 82/212982/3
authorjiyong.min <jiyong.min@samsung.com>
Thu, 29 Aug 2019 05:18:45 +0000 (14:18 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Thu, 29 Aug 2019 06:28:38 +0000 (15:28 +0900)
 - Out-of-bounds issue to read image_format_mimetype_table
 - 'MM_UTIL_COLOR_NUM' is larger than array_size but the array
   can access with array_size(table_size).
: So 'MM_UTIL_COLOR_NUM' is replaced to real array_size.

Change-Id: I7804cd4d510499b46d96183a1afc918fc0ffc444

src/image_util_private.c

index d8f7b9e..1d5f883 100755 (executable)
@@ -32,7 +32,7 @@ typedef struct {
        const char *mimetype_name;
 } image_format_mimetype_pair_s;
 
-static const image_format_mimetype_pair_s image_format_mimetype_table[MM_UTIL_COLOR_NUM] = {
+static const image_format_mimetype_pair_s image_format_mimetype_table[] = {
        { MM_UTIL_COLOR_YUV420, MEDIA_FORMAT_YV12,              "MEDIA_FORMAT_YV12" },
        { MM_UTIL_COLOR_YUV422, MEDIA_FORMAT_422P,              "MEDIA_FORMAT_422P" },
        { MM_UTIL_COLOR_I420,           MEDIA_FORMAT_I420,              "MEDIA_FORMAT_I420" },
@@ -50,6 +50,8 @@ static const image_format_mimetype_pair_s image_format_mimetype_table[MM_UTIL_CO
        { MM_UTIL_COLOR_NV61,           -1,                                             "Not support" }
 };
 
+static const unsigned int image_format_mimetype_table_size = sizeof(image_format_mimetype_table) / sizeof(image_format_mimetype_pair_s);
+
 static int _convert_colorspace_tbl[] = {
        MM_UTIL_COLOR_YUV420,           /* IMAGE_UTIL_COLORSPACE_YUV420 */
        MM_UTIL_COLOR_YUV422,           /* IMAGE_UTIL_COLORSPACE_YUV422 */
@@ -273,15 +275,16 @@ static media_format_mimetype_e __image_format_to_mimetype(mm_util_color_format_e
 {
        unsigned int i = 0;
 
-       for (i = 0; i < MM_UTIL_COLOR_NUM; i++) {
+       for (i = 0; i < image_format_mimetype_table_size; i++) {
                if (image_format_mimetype_table[i].image_format == format) {
                        image_util_debug("imgp fmt: %d mimetype fmt: %s", format, image_format_mimetype_table[i].mimetype_name);
                        return image_format_mimetype_table[i].mimetype;
                }
        }
 
-       image_util_error("imgp fmt: %d", format);
+       image_util_error("not supported image format: %d", format);
 
+       /* if format can't be changed to mimetype, return max */
        return MEDIA_FORMAT_MAX;
 }
 
@@ -289,15 +292,17 @@ static mm_util_color_format_e __mimetype_to_image_format(media_format_mimetype_e
 {
        unsigned int i = 0;
 
-       for (i = 0; i < MM_UTIL_COLOR_NUM; i++) {
+       for (i = 0; i < image_format_mimetype_table_size; i++) {
                if (image_format_mimetype_table[i].mimetype == mimetype) {
-                       image_util_debug("mimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name, image_format_mimetype_table[i].image_format);
+                       image_util_debug("mimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name,
+                                               image_format_mimetype_table[i].image_format);
                        return image_format_mimetype_table[i].image_format;
                }
        }
 
-       image_util_error("mimetype: %s", image_format_mimetype_table[i].mimetype_name);
+       image_util_error("not supported mimetype: %d", mimetype);
 
+       /* if mimetype can't be changed to image format, return max */
        return MM_UTIL_COLOR_NUM;
 }