From: Wook Song Date: Fri, 17 Mar 2017 01:53:50 +0000 (+0900) Subject: pass: hal: Fix defects including the case that the handle could be lost X-Git-Tag: submit/tizen/20170328.004502~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dc8936008dbd50311f1df0ff722ebd2f4f9f2cdc;p=platform%2Fcore%2Fsystem%2Fpass.git pass: hal: Fix defects including the case that the handle could be lost This patch fixes the following code-level defects according to static program analysis result: 1. NULL_AFTER_DEREF: A pointer which was dereferenced is compared to NULL value. 2. HANDLE_LEAK: A handle was created by calling function 'fopen' and lost at some point. 3. NO_EFFECT: the entire array is compared to 0. 4. NO_CAST.INTEGER_OVERFLOW: Values of an arithmetic expression could be subject to overflow due to a failure to cast operands to a larger data type before perfoming arithmetic overflow before widen. Change-Id: Icd25de37efc3b52aacece3476e697486a1934c3a Signed-off-by: Wook Song Reviewed-by: Chanwoo Choi --- diff --git a/src/pass/pass-hal.c b/src/pass/pass-hal.c index 851f93b..129d025 100644 --- a/src/pass/pass-hal.c +++ b/src/pass/pass-hal.c @@ -544,14 +544,19 @@ int pass_get_resource(struct pass *pass) /* Get the load_table of each resource to estimate the system load. */ int pass_get_cpu_stats(struct pass_policy *policy) { - struct pass_cpu_stats *stats = policy->pass_cpu_stats; - struct pass_resource *pass_res = to_pass_resource(policy); + struct pass_cpu_stats *stats; + struct pass_resource *pass_res; char str[BUFF_MAX]; FILE *fp_stats = NULL; int i, j, ret; + if (!policy) + return -EINVAL; + + stats = policy->pass_cpu_stats; + pass_res = to_pass_resource(policy); - if (!policy || !pass_res->cdata.path_load_table || !stats) { + if (!stats) { _E("invalid parameter of structure pass_cpu_stats"); return -EINVAL; } @@ -561,8 +566,10 @@ int pass_get_cpu_stats(struct pass_policy *policy) return -EIO; /* Read the title and drop the buffer because it is not used */ - if (!fgets(str, BUFF_MAX, fp_stats)) + if (!fgets(str, BUFF_MAX, fp_stats)) { + fclose(fp_stats); return -EIO; + } for (i = 0; i < policy->num_pass_cpu_stats; i++) { ret = fscanf(fp_stats, "%lld %d %d %d", @@ -570,15 +577,20 @@ int pass_get_cpu_stats(struct pass_policy *policy) &stats[i].freq, &stats[i].freq_new, &stats[i].nr_runnings); - if (ret < 0) + if (ret < 0) { + fclose(fp_stats); return -EIO; + } for (j = 0; j < pass_res->cdata.num_cpus; j++) { ret = fscanf(fp_stats, "%d", &stats[i].load[j]); - if (ret < 0) + if (ret < 0) { + fclose(fp_stats); return -EIO; + } } } + fclose(fp_stats); return 0; @@ -591,5 +603,5 @@ int64_t pass_get_time_ms(void) gettimeofday(&now, NULL); - return (int64_t)(now.tv_sec * 1000 + now.tv_usec / 1000); + return ((int64_t) now.tv_sec * 1000 + (int64_t) now.tv_usec / 1000); }