Fix static analysis issues 93/184193/1
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 16 Jul 2018 07:42:01 +0000 (16:42 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 16 Jul 2018 07:42:01 +0000 (16:42 +0900)
- Checks the return values of functions

Change-Id: Ia55b7fc278ec1dd95aed30eafacc3a41eb2adce4
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/widget_service.c

index 6a780b2..bdcaa2c 100644 (file)
@@ -771,11 +771,11 @@ static int _get_widget_supported_sizes(const char *widget_id, uid_t uid,
                "SELECT width, height FROM support_size WHERE classid=?";
        int ret;
        sqlite3 *db;
-       sqlite3_stmt *stmt;
+       sqlite3_stmt *stmt = NULL;
        int count = 0;
        int i;
-       int *width;
-       int *height;
+       int *width = NULL;
+       int *height = NULL;
 
        db = _open_db(uid);
        if (db == NULL)
@@ -785,8 +785,8 @@ static int _get_widget_supported_sizes(const char *widget_id, uid_t uid,
        if (ret != SQLITE_OK) {
                /* LCOV_EXCL_START */
                _E("prepare error: %s", sqlite3_errmsg(db));
-               sqlite3_close_v2(db);
-               return WIDGET_ERROR_FAULT;
+               ret = WIDGET_ERROR_FAULT;
+               goto err;
                /* LCOV_EXCL_STOP */
        }
 
@@ -796,31 +796,44 @@ static int _get_widget_supported_sizes(const char *widget_id, uid_t uid,
                count++;
 
        if (count == 0) {
-               sqlite3_finalize(stmt);
-               sqlite3_close_v2(db);
                *cnt = 0;
                *w = NULL;
                *h = NULL;
-               return WIDGET_ERROR_NOT_EXIST;
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto err;
        }
 
        sqlite3_reset(stmt);
        width = malloc(sizeof(int) * count);
+       if (!width) {
+               _E("Failed to allocate width");
+               ret = WIDGET_ERROR_FAULT; /* It shoud be WIDGET_ERROR_OUT_OF_MEMORY */
+               goto err;
+       }
+
        height = malloc(sizeof(int) * count);
+       if (!height) {
+               _E("Failed to allocate height");
+               ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
+               goto err;
+       }
 
        for (i = 0; i < count; i++) {
-               sqlite3_step(stmt);
+               ret = sqlite3_step(stmt);
+               if (ret != SQLITE_ROW) {
+                       _E("sqlite3_step() is failed. err(%s)",
+                                       sqlite3_errmsg(db));
+                       ret = WIDGET_ERROR_FAULT;
+                       goto err;
+               }
                _get_column_int(stmt, 0, &width[i]);
                _get_column_int(stmt, 1, &height[i]);
        }
 
        if (_convert_to_support_size(width, height, count)) {
                _E("failed to convert size");
-               free(width);
-               free(height);
-               sqlite3_finalize(stmt);
-               sqlite3_close_v2(db);
-               return WIDGET_ERROR_FAULT;
+               ret = WIDGET_ERROR_FAULT;
+               goto err;
        }
        *w = width;
        *h = height;
@@ -830,6 +843,18 @@ static int _get_widget_supported_sizes(const char *widget_id, uid_t uid,
        sqlite3_close_v2(db);
 
        return WIDGET_ERROR_NONE;
+
+err:
+       if (height)
+               free(height);
+       if (width)
+               free(width);
+       if (stmt)
+               sqlite3_finalize(stmt);
+       if (db)
+               sqlite3_close_v2(db);
+
+       return ret;
 }
 
 
@@ -840,12 +865,12 @@ static int _get_widget_supported_info(const char *widget_id, uid_t uid,
                "SELECT width, height FROM support_size WHERE classid=?";
        int ret;
        sqlite3 *db;
-       sqlite3_stmt *stmt;
+       sqlite3_stmt *stmt = NULL;
        int count = 0;
        int i;
-       int *width;
-       int *height;
-       int *type;
+       int *width = NULL;
+       int *height = NULL;
+       int *type = NULL;
 
        db = _open_db(uid);
        if (db == NULL)
@@ -855,8 +880,8 @@ static int _get_widget_supported_info(const char *widget_id, uid_t uid,
        if (ret != SQLITE_OK) {
                /* LCOV_EXCL_START */
                _E("prepare error: %s", sqlite3_errmsg(db));
-               sqlite3_close_v2(db);
-               return WIDGET_ERROR_FAULT;
+               ret = WIDGET_ERROR_FAULT;
+               goto err;
                /* LCOV_EXCL_STOP */
        }
 
@@ -866,33 +891,51 @@ static int _get_widget_supported_info(const char *widget_id, uid_t uid,
                count++;
 
        if (count == 0) {
-               sqlite3_finalize(stmt);
-               sqlite3_close_v2(db);
                *cnt = 0;
                *w = NULL;
                *h = NULL;
-               return WIDGET_ERROR_NOT_EXIST;
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto err;
        }
 
        sqlite3_reset(stmt);
        width = malloc(sizeof(int) * count);
+       if (!width) {
+               _E("Failed to allocate width");
+               ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
+               goto err;
+       }
+
        height = malloc(sizeof(int) * count);
+       if (!height) {
+               _E("Failed to allocate height");
+               ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
+               goto err;
+       }
+
        type = malloc(sizeof(int) * count);
+       if (!type) {
+               _E("Failed to allocate type");
+               ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
+               goto err;
+       }
 
        for (i = 0; i < count; i++) {
-               sqlite3_step(stmt);
+               ret = sqlite3_step(stmt);
+               if (ret != SQLITE_ROW) {
+                       _E("sqlite3_step() is failed. err(%s)",
+                                       sqlite3_errmsg(db));
+                       ret = WIDGET_ERROR_FAULT;
+                       goto err;
+               }
                _get_column_int(stmt, 0, &width[i]);
                _get_column_int(stmt, 1, &height[i]);
        }
 
        if (_convert_to_support_info(width, height, type, count)) {
                _E("failed to convert size");
-               free(width);
-               free(height);
-               free(type);
-               sqlite3_finalize(stmt);
-               sqlite3_close_v2(db);
-               return WIDGET_ERROR_FAULT;
+               ret = WIDGET_ERROR_FAULT;
+               goto err;
        }
        *t = type;
        *w = width;
@@ -903,6 +946,20 @@ static int _get_widget_supported_info(const char *widget_id, uid_t uid,
        sqlite3_close_v2(db);
 
        return WIDGET_ERROR_NONE;
+
+err:
+       if (type)
+               free(type);
+       if (height)
+               free(height);
+       if (width)
+               free(width);
+       if (stmt)
+               sqlite3_finalize(stmt);
+       if (db)
+               sqlite3_close_v2(db);
+
+       return ret;
 }
 
 /* LCOV_EXCL_START */