Fix svace issue 65/240265/9
authorJeon Sang-Heon <sh95.jeon@samsung.com>
Wed, 5 Aug 2020 11:18:16 +0000 (11:18 +0000)
committerJeon Sang-Heon <sh95.jeon@samsung.com>
Fri, 7 Aug 2020 17:26:54 +0000 (17:26 +0000)
- Check file pointer is null
- Initialize curl error enum code
- WGID: 445288

Change-Id: I074f18b48c095dd7d75bc03ce33218ac080ae1c9
Signed-off-by: Jeon Sang-Heon <sh95.jeon@samsung.com>
src/plugin/http_util.c

index 18494859e6f8b1be2cbd3621ba219dc7bc5f6c56..7b826dc07d760a74ad394fd07dce420721914539 100644 (file)
@@ -310,8 +310,9 @@ int http_util_download_file(const char *download_url, const char *download_path,
 
        FILE *fp = NULL;
        CURL *curl = NULL;
-       CURLcode err;
-       int curl_ret_code = 0;
+       CURLcode err = CURLE_OK;
+       int ret_code = 0;
+       int curl_ret_code = CURLE_OK;
 
        curl = curl_easy_init();
        retvm_if(!curl, -EIO, "Failed to init curl");
@@ -332,6 +333,10 @@ int http_util_download_file(const char *download_url, const char *download_path,
 #endif
 
        fp = fopen(download_path, "wb");
+       if (fp == NULL) {
+               _E("Failed to open %s", download_path);
+               goto clean;
+       }
 
        curl_ret_code = curl_easy_setopt(curl, CURLOPT_URL, download_url);
        if (CURLE_OK != curl_ret_code) {
@@ -355,11 +360,14 @@ int http_util_download_file(const char *download_url, const char *download_path,
 
 clean:
        curl_easy_cleanup(curl);
-       fclose(fp);
 
-       if (CURLE_OK != curl_ret_code) {
-               return -EIO;
-       }
+       if (fp != NULL)
+               fclose(fp);
+       else
+               ret_code = -EIO;
+
+       if (CURLE_OK != curl_ret_code)
+               ret_code = -EIO;
 
 #ifdef DOWNLOAD_MONITORING
        int status;
@@ -370,10 +378,10 @@ clean:
                _I("Failed to download file[%s, %d]", curl_easy_strerror(err), err);
                if (remove(download_path) < 0)
                        _E("Failed to remove [%s]", download_path);
-               return -EIO;
+               ret_code = -EIO;
        }
 
-       return 0;
+       return ret_code;
 }
 
 int http_util_send_request(fmwup_http_e type, const char *req_url, char **res_header, char **res_body)