add "out of memory" exception handling. 33/54433/2
authorjiseob.jang <jiseob.jang@samsung.com>
Tue, 15 Dec 2015 08:10:58 +0000 (17:10 +0900)
committerjiseob.jang <jiseob.jang@samsung.com>
Tue, 15 Dec 2015 08:13:57 +0000 (17:13 +0900)
Change-Id: Icbedf1ba3cb3759c321c14b0c3764f4568db2ca5
Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
common/src/account_crypto_service.c
common/src/account_db_helper.c
common/src/account_ipc_marshal.c
common/src/account_key_handler.c

index 33a59708dda742895d1488905ed28052f63c18f1..1cb56b25f994b196f34ce2aafa64d3fae4ee56db 100644 (file)
@@ -41,6 +41,7 @@
 #define CRYPTO_ERROR -1
 #define CRYPTO_ERROR_NONE 0
 #define CRYPTO_ERROR_INVALID_PARAMETER TIZEN_ERROR_INVALID_PARAMETER
+#define CRYPTO_ERROR_OUT_OF_MEMORY TIZEN_ERROR_OUT_OF_MEMORY
 
 #define ACCESS_TOKEN_ALIAS  "access_token"
 
@@ -74,6 +75,11 @@ static int _encrypt_aes_cbc(const unsigned char* key, const int key_len, const u
 
     // assing a enough memory for decryption.
     ciphertext = (unsigned char*) malloc(data_len + 32);
+       if (ciphertext == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return CRYPTO_ERROR_OUT_OF_MEMORY;
+       }
+
        ACCOUNT_MEMSET(ciphertext, 0, data_len + 32);
 
        _INFO("before EVP_CIPHER_CTX_new");
@@ -149,6 +155,11 @@ static int _decrypt_aes_cbc(const unsigned char* key, const int key_len, const u
 
     // assing a enough memory for decryption.
     plaintext = (unsigned char*) malloc(data_len);
+       if (plaintext == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return CRYPTO_ERROR_OUT_OF_MEMORY;
+       }
+
        ACCOUNT_MEMSET(plaintext, 0, data_len);
 
        _INFO("before EVP_CIPHER_CTX_new");
index 741db6fffc809373ec2cdfa74d4a14571701ae2b..e79ad33ecaf487a1ee91e876ff6e22573d81d4b9 100644 (file)
@@ -140,6 +140,12 @@ char* _account_get_current_appid(int pid, uid_t uid)
                if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE)) {
                        appid_ret = _account_dup_text(EMAIL_APPID);
                        _ACCOUNT_FREE(cmdline);
+
+                       if (appid_ret == NULL) {
+                               ACCOUNT_FATAL("Memory Allocation Failed");
+                               return NULL;
+                       }
+
                        return appid_ret;
                } else {
                        ACCOUNT_DEBUG("No app id\n");
@@ -147,7 +153,13 @@ char* _account_get_current_appid(int pid, uid_t uid)
                        return NULL;
                }
        }
-       return _account_dup_text(appid);
+
+       appid_ret = _account_dup_text(appid);
+       if (appid_ret == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+       }
+
+       return appid_ret;
 }
 
 
@@ -817,9 +829,14 @@ static int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void
        }
 
        item = _account_dup_text(appid);
+       if (item == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return _ACCOUNT_ERROR_OUT_OF_MEMORY;
+       }
+
        *appid_list = g_slist_append(*appid_list, item);
 
-       return 0;
+       return _ACCOUNT_ERROR_NONE;
 }
 
 static int _account_type_query_app_id_exist(sqlite3 *account_db_handle, const char* app_id)
@@ -887,12 +904,20 @@ int _account_get_represented_appid_from_db(sqlite3 *account_user_db, sqlite3 *ac
        if(!strcmp(appid, "com.samsung.setting")){
                ACCOUNT_DEBUG("Setting exception\n");
                *verified_appid = _account_dup_text("com.samsung.setting");
+               if (*verified_appid == NULL) {
+                       ACCOUNT_FATAL("Memory Allocation Failed");
+                       return _ACCOUNT_ERROR_OUT_OF_MEMORY;
+               }
                return _ACCOUNT_ERROR_NONE;
        }
 
        if(!strcmp(appid, "com.samsung.samsung-account-front")){
                ACCOUNT_DEBUG("Setting exception\n");
                *verified_appid = _account_dup_text("com.samsung.samsung-account-front");
+               if (*verified_appid == NULL) {
+                       ACCOUNT_FATAL("Memory Allocation Failed");
+                       return _ACCOUNT_ERROR_OUT_OF_MEMORY;
+               }
                return _ACCOUNT_ERROR_NONE;
        }
 
@@ -937,6 +962,11 @@ int _account_get_represented_appid_from_db(sqlite3 *account_user_db, sqlite3 *ac
                if(tmp) {
                        if(_account_type_query_app_id_exist_from_all_db(account_user_db, account_global_db, tmp) ==  _ACCOUNT_ERROR_NONE) {
                                *verified_appid = _account_dup_text(tmp);
+                               if (*verified_appid == NULL) {
+                                       ACCOUNT_FATAL("Memory Allocation Failed");
+                                       error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
+                                       break;
+                               }
                                error_code = _ACCOUNT_ERROR_NONE;
                                _ACCOUNT_FREE(tmp);
                                break;
@@ -1053,12 +1083,19 @@ int _account_check_appid_group_with_package_name(const char* appid, char* packag
 static bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
 {
        account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
-
-       if (cap_data == NULL)
+       if (cap_data == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
                return FALSE;
+       }
+
        ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
 
        cap_data->type = _account_dup_text(capability_type);
+       if (cap_data->type == NULL) {
+               ACCOUNT_FATAL("_account_add_capability_to_account_cb :: malloc fail");
+               return FALSE;
+       }
+
        cap_data->value = capability_value;
        _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
 
@@ -1072,15 +1109,30 @@ static bool _account_add_custom_to_account_cb(const char* key, const char* value
        account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
 
        if (custom_data == NULL) {
-               ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
+               ACCOUNT_FATAL("_account_add_custom_to_account_cb :: malloc fail\n");
                return FALSE;
        }
        ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
 
        custom_data->account_id = account->id;
        custom_data->app_id = _account_dup_text(account->package_name);
+       if (custom_data->app_id == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return FALSE;
+       }
+
        custom_data->key = _account_dup_text(key);
+       if (custom_data->key == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return FALSE;
+       }
+
        custom_data->value = _account_dup_text(value);
+       if (custom_data->value == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return FALSE;
+       }
+
        _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
 
        account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
@@ -1447,6 +1499,10 @@ int _account_delete_account_by_package_name(sqlite3 *account_db_handle, const ch
                current_appid = _account_get_current_appid(pid, uid);
 
                package_name_temp = _account_dup_text(package_name);
+               if (package_name_temp == NULL) {
+                       ACCOUNT_FATAL("Memory Allocation Failed");
+                       return _ACCOUNT_ERROR_OUT_OF_MEMORY;
+               }
 
                ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
 
index 1e7dd0a1709aa7c8f8a44ecac4330d4d815826d8..9eeceff8934fcbeb7dc0a8502ca86ae43b12c907 100644 (file)
@@ -75,19 +75,23 @@ _variant_to_label(GVariant *variant)
        g_variant_get (variant, "(sss)", &app_id, &label, &locale);
 
        label_s* label_data = (label_s*) calloc(1, sizeof(label_s));
-
        if( label_data == NULL ) {
-               _ERR("provider_feature_s calloc failed - out of memory.");
+               ACCOUNT_FATAL("label_s calloc failed - out of memory.");
+               g_free(app_id);
+               g_free(label);
+               g_free(locale);
                return NULL;
        }
 
        label_data->app_id = g_strdup(app_id);
+
        label_data->label = g_strdup(label);
+
        label_data->locale = g_strdup(locale);
 
-       g_free (app_id);
-       g_free (label);
-       g_free (locale);
+       g_free(app_id);
+       g_free(label);
+       g_free(locale);
 
        return label_data;
 }
@@ -160,13 +164,13 @@ _variant_to_provider_feature(GVariant *variant)
        g_variant_get (variant, "(ss)", &key, &app_id);
 
        provider_feature_s* provider_feature_data = (provider_feature_s*) calloc(1, sizeof(provider_feature_s));
-
-       if( provider_feature_data == NULL ) {
+       if (provider_feature_data == NULL) {
                _ERR("provider_feature_s calloc failed - out of memory.");
                return NULL;
        }
 
        provider_feature_data->key = g_strdup(key);
+
        provider_feature_data->app_id = g_strdup(app_id);
 
        g_free (key);
@@ -356,9 +360,8 @@ account_s* umarshal_account(GVariant* in_data)
        }
 
        account_s* account = (account_s*)calloc(1, sizeof(account_s));
-       if (account == NULL)
-       {
-               _ERR("Out of memory");
+       if (account == NULL) {
+               _ERR("account_s calloc failed - out of memory");
                return NULL;
        }
 
@@ -537,6 +540,10 @@ GSList* unmarshal_account_list(GVariant* variant)
        while (g_variant_iter_loop (&iter, "a{sv}", &iter_row))
        {
                account_s* account = (account_s*)calloc(1, sizeof(account_s));
+               if (account == NULL) {
+                       ACCOUNT_FATAL("account_s calloc failed - out of memory.");
+                       break;
+               }
 
                while (g_variant_iter_loop(iter_row, "{sv}", &key, &value))
                {
@@ -666,10 +673,8 @@ account_s* create_empty_account_instance(void)
        _INFO("create_empty_account_instance start");
 
        account_s *data = (account_s*)malloc(sizeof(account_s));
-
-       if (data == NULL)
-       {
-               ACCOUNT_ERROR("Memory Allocation Failed");
+       if (data == NULL) {
+               ACCOUNT_ERROR("account_s malloc failed - out of memory");
                return NULL;
        }
 
@@ -700,10 +705,8 @@ account_type_s* create_empty_account_type_instance(void)
 {
        _INFO("create_empty_account_type_instance start");
        account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
-
-       if (data == NULL)
-       {
-               ACCOUNT_ERROR("Memory Allocation Failed");
+       if (data == NULL) {
+               ACCOUNT_ERROR("account_type_s malloc failed - out of memory");
                return NULL;
        }
 
@@ -881,9 +884,8 @@ account_type_s* umarshal_account_type(GVariant* in_data)
        }
 
        account_type_s* account_type = (account_type_s*)calloc(1, sizeof(account_type_s));
-       if (account_type == NULL)
-       {
-               _ERR("Out of memory");
+       if (account_type == NULL) {
+               ACCOUNT_FATAL("account_type_s calloc failed - out of memory");
                return NULL;
        }
 
@@ -1096,6 +1098,10 @@ GSList* unmarshal_capability_list(GVariant* variant)
        while (g_variant_iter_loop (&iter, "a{sv}", &iter_row))
        {
                account_capability_s* account_capability = (account_capability_s*)calloc(1, sizeof(account_capability_s));
+               if (account_capability == NULL) {
+                       ACCOUNT_FATAL("account_capability_s calloc Failed - out of memory");
+                       break;
+               }
 
                while (g_variant_iter_loop(iter_row, "{sv}", &key, &value))
                {
@@ -1159,6 +1165,10 @@ GSList* unmarshal_custom_list(GVariant* variant)
        while (g_variant_iter_loop (&iter, "a{sv}", &iter_row))
        {
                account_custom_s* account_custom = (account_custom_s*)calloc(1, sizeof(account_custom_s));
+               if (account_custom == NULL) {
+                       ACCOUNT_FATAL("account_custom_s calloc failed - out of memory");
+                       break;
+               }
 
                while (g_variant_iter_loop(iter_row, "{sv}", &key, &value))
                {
@@ -1273,9 +1283,13 @@ int* unmarshal_user_int_array(GVariant* variant)
 //     gchar* var_type = g_variant_print (variant, TRUE);
 //     _INFO("var_type = %s", var_type);
 
-       g_variant_iter_init (&iter, variant);
+       user_data_int = (int*)calloc(USER_INT_CNT, sizeof(int)*USER_INT_CNT);
+       if (user_data_int == NULL) {
+               ACCOUNT_FATAL("user_data_int_array calloc failed - out of memory");
+               return NULL;
+       }
 
-       user_data_int = (int*)calloc(USER_INT_CNT, sizeof(int));
+       g_variant_iter_init (&iter, variant);
 
        for(i=0; i<USER_INT_CNT; i++)
        {
@@ -1316,9 +1330,13 @@ char** unmarshal_user_txt_array(GVariant* variant)
 //     gchar* var_type = g_variant_print (variant, TRUE);
 //     _INFO("var_type = %s", var_type);
 
-       g_variant_iter_init (&iter, variant);
+       user_data_txts = (char**)calloc(USER_TXT_CNT, sizeof(char*)*USER_TXT_CNT);
+       if (user_data_txts == NULL) {
+               ACCOUNT_FATAL("user_data_txt_array calloc failed - out of memory");
+               return NULL;
+       }
 
-       user_data_txts = (char**)calloc(USER_TXT_CNT, sizeof(char*));
+       g_variant_iter_init (&iter, variant);
 
        for(i=0; i<USER_TXT_CNT; i++)
        {
index cf0ce6ced4c77600979020b9308d0d3970ddbfe8..ed4054cd9d84ec06178da443b780c1c9ce6758a4 100644 (file)
@@ -74,6 +74,11 @@ static int _get_app_mkey(unsigned char** mkey, int* mkey_len)
        _INFO("before mkey_buffer->size=[%d]", mkey_buffer->size);
        *mkey_len = mkey_buffer->size;
        *mkey = (unsigned char *) malloc((*mkey_len)+1);
+       if (*mkey == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return CKMC_ERROR_OUT_OF_MEMORY;
+       }
+
        memset(*mkey, 0, (*mkey_len)+1);
        memcpy(*mkey, mkey_buffer->data, *mkey_len);
 //     (*mkey)[*mkey_len] = '\0';
@@ -98,6 +103,11 @@ static int _create_app_mkey(unsigned char **mkey, int *mkey_len)
        _INFO("start _create_app_mkey");
 
        random = (unsigned char *) malloc(MKEY_LEN);
+       if (random == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return CKMC_ERROR_OUT_OF_MEMORY;
+       }
+
        _INFO("before _get_random");
        ret = _get_random(MKEY_LEN, &random);
        if(CKMC_ERROR_NONE != ret) {
@@ -148,6 +158,11 @@ static int _get_app_dek(char *mkey, const char *pkg_id, unsigned char **dek, int
 
        *dek_len = dek_buffer->size;
        *dek = (unsigned char *) malloc((*dek_len)+1);
+       if (*dek == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return CKMC_ERROR_OUT_OF_MEMORY;
+       }
+
        _INFO("before memcpy dek_buffer");
        memcpy(*dek, dek_buffer->data, (*dek_len)+1);
        _INFO("before dek_buffer free");
@@ -171,6 +186,11 @@ static int _create_app_dek(char *mkey, const char *pkg_id, unsigned char **dek,
        sprintf(alias, "%s%s", ACCOUNT_MANAGER_DEK_ALIAS_PFX, pkg_id);
 
        random = (unsigned char *) malloc(DEK_LEN);
+       if (random == NULL) {
+               ACCOUNT_FATAL("Memory Allocation Failed");
+               return CKMC_ERROR_OUT_OF_MEMORY;
+       }
+
        ret = _get_random(DEK_LEN, &random);
        if(CKMC_ERROR_NONE != ret) {
                return CKMC_ERROR_UNKNOWN;