Fix minor issues 81/130781/2
authorKichan Kwon <k_c.kwon@samsung.com>
Wed, 24 May 2017 02:42:47 +0000 (11:42 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 24 May 2017 04:57:56 +0000 (13:57 +0900)
- Check overflow
- Initialize value

Change-Id: I005a58129eb47e5e7e9840849f274ce9082380bd
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/runtime_info_usage.c

index 8b5908f..fa72ec8 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -46,8 +47,9 @@ static const runtime_info_dbus_info_s dbus_info[] = {
        { "ProcCpuUsage", "process cpu" },
 };
 
-#define kBtoKiB(val) (int)(((long long)val * 1024)/1000)
-#define pagetoKiB(val) (int)((long long)val * 4096 / 1000)
+#define MIN(x, y) (x) > (y) ? (x) : (y)
+#define kBtoKiB(val) (int)MIN(((long long)(val) * 1024 / 1000), INT_MAX)
+#define pagetoKiB(val) (int)MIN(((long long)(val) * 4096 / 1000), INT_MAX)
 
 //LCOV_EXCL_START : system error
 static int runtime_info_get_dbus_error(const char *err_name)
@@ -257,7 +259,7 @@ API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_
 
        /* Populate the entries of info array using the data received from resourced */
        *info = (process_memory_info_s *)malloc(size * sizeof(process_memory_info_s));
-       if (!info) {
+       if (!(*info)) {
                _E("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
                dbus_message_unref(replymsg);
                return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
@@ -301,7 +303,14 @@ API int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage)
 {
        FILE *cpuinfo_fp;
        char buf[256];
-       unsigned long long user, nice, system, idle, iowait, irq, softirq, total_uptime;
+       unsigned long long user = 0;
+       unsigned long long nice = 0;
+       unsigned long long system = 0;
+       unsigned long long idle = 0;
+       unsigned long long iowait = 0;
+       unsigned long long irq = 0;
+       unsigned long long softirq = 0;
+       unsigned long long total_uptime = 0;
 
        if (usage == NULL) {
                _E("INVALID_PARAMETER(0x%08x) : invalid output param",
@@ -318,19 +327,19 @@ API int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage)
 
        while (fgets(buf, sizeof(buf), cpuinfo_fp) != NULL) {
                if (!strncmp(buf, "cpu ", 4) &&
-                   sscanf(buf+4, "%llu %llu %llu %llu %llu %llu %llu",
+                   sscanf(buf + 4, "%llu %llu %llu %llu %llu %llu %llu",
                                &user, &nice, &system, &idle,
                                &iowait, &irq, &softirq) == 7)
                        break;
        }
        fclose(cpuinfo_fp);
 
-       total_uptime = user+nice+system+idle+iowait+irq+softirq;
+       total_uptime = user + nice + system + idle + iowait + irq + softirq;
 
-       usage->user = (double)user*100/total_uptime;
-       usage->nice = (double)nice*100/total_uptime;
-       usage->system = (double)system*100/total_uptime;
-       usage->iowait = (double)iowait*100/total_uptime;
+       usage->user = (double)user * 100 / total_uptime;
+       usage->nice = (double)nice * 100 / total_uptime;
+       usage->system = (double)system * 100 / total_uptime;
+       usage->iowait = (double)iowait * 100 / total_uptime;
 
        return RUNTIME_INFO_ERROR_NONE;
 }