pass: hal: Fix defects including the case that the handle could be lost 88/119488/4
authorWook Song <wook16.song@samsung.com>
Fri, 17 Mar 2017 01:53:50 +0000 (10:53 +0900)
committerWook Song <wook16.song@samsung.com>
Mon, 27 Mar 2017 06:25:30 +0000 (15:25 +0900)
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 <wook16.song@samsung.com>
Reviewed-by: Chanwoo Choi <cw00.choi@samsung.com>
src/pass/pass-hal.c

index 851f93b04c9778e247a2197b6c906668f10c717f..129d025e2fb3f9d3d6af266a2fd953f7e192aa53 100644 (file)
@@ -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);
 }