Resolve probable infinite loop issue due to integer overflow. 16/288916/4 accepted/tizen/unified/20230314.093346
authorAkash Kumar <akash1.kumar@samsung.com>
Fri, 24 Feb 2023 09:24:48 +0000 (14:54 +0530)
committerAkash Kumar <akash1.kumar@samsung.com>
Thu, 9 Mar 2023 09:45:25 +0000 (15:15 +0530)
Change-Id: I7ad2bc1d6ef28510ed6f17fb004425dca4637a56
Signed-off-by: Akash Kumar <akash1.kumar@samsung.com>
provider/download-provider-client.c

index 0ed6ef2..d1a2e0f 100755 (executable)
@@ -322,12 +322,13 @@ static int __dp_get_download_id(dp_client_fmt *client)
        }
 
        retry_count = 0;
+       long long temp_id = download_id;
        do {
                retry_count++;
-               if (download_id < INT_MAX) {
-                       TRACE_INFO("download_id [%d]", download_id);
+               if (temp_id < INT_MAX) {
+                       TRACE_INFO("download_id [%lld]", temp_id);
                        check_duplicate = dp_db_check_duplicated_int(client->dbhandle,
-                                       DP_TABLE_LOGGING, DP_DB_COL_ID, download_id, &errorcode);
+                                       DP_TABLE_LOGGING, DP_DB_COL_ID, (int)temp_id, &errorcode);
                        if (errorcode == DP_ERROR_NONE) {
                                if (check_duplicate == 0)
                                        break;
@@ -335,24 +336,25 @@ static int __dp_get_download_id(dp_client_fmt *client)
                                TRACE_ERROR("ERROR [%d]", errorcode);
                        }
                        if (retry_count < 3) {
-                               download_id++;
+                               temp_id++;
                        } else if (retry_count < 10) {
                                gettimeofday(&tval, NULL);
                                tval.tv_usec = (tval.tv_usec & 0x0fff);
-                               download_id += 1171 * retry_count + tval.tv_usec;
+                               temp_id += 1171 * retry_count + tval.tv_usec;
                        } else if (retry_count < 20) {
                                gettimeofday(&tval, NULL);
                                tval.tv_usec = (tval.tv_usec & 0xff33ff) + retry_count;
-                               download_id += tval.tv_usec;
+                               temp_id += tval.tv_usec;
                        } else {
-                               TRACE_ERROR("failed to generate unique download_id [%d]", download_id);
+                               TRACE_ERROR("failed to generate unique download_id [%lld]", temp_id);
                                return -1;
                        }
                } else {
                        TRACE_ERROR("reached INT_MAX limit");
-                       download_id = DP_FIRST_DOWNLOAD_ID;
+                       temp_id = DP_FIRST_DOWNLOAD_ID;
                }
        } while (check_duplicate != 0);
+       download_id = (int)temp_id;
        return download_id;
 }