Fix minor issues 48/152448/3 submit/tizen_4.0/20170926.084635
authorKichan Kwon <k_c.kwon@samsung.com>
Tue, 26 Sep 2017 07:20:27 +0000 (16:20 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Tue, 26 Sep 2017 08:43:01 +0000 (17:43 +0900)
- Prevent to access out of array
- Use larger constant for shifting more than 32
- Free before exit

Change-Id: I0297f8dca302148430332c521629ff0f14323536
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/block/block.c
src/common/procfs.c
src/common/smaps.c
src/common/storage-helper.c
src/heart/heart-battery.c
src/memory/lowmem-limit.c
src/memory/vmpressure-lowmem-handler.c

index c8367a4cbc47f1d5dad89571a377a0d13cbfbbb8..acb5b3bacc2cd3fc4d76d7468a534e9a8e2fb00f 100755 (executable)
@@ -69,7 +69,7 @@ static void add_monitoring_path(gpointer elem, gpointer not_used)
                return;
        }
 
-       strncpy(bmi->path, monitoring_path, sizeof(bmi->path));
+       strncpy(bmi->path, monitoring_path, sizeof(bmi->path) - 1);
        block_monitor_list = g_slist_prepend(block_monitor_list, bmi);
 }
 
index c1dd6c3bccfd7a485ecb9805703d0e6e14e26ea5..ab3d32808e65f6d20ae8a9a27b160a76096d5bc9 100644 (file)
@@ -705,7 +705,7 @@ int proc_get_meminfo(struct meminfo *mi, enum meminfo_mask mask)
                enum meminfo_id i;
 
                for (i = 0; i < MEMINFO_ID_MAX; i++)
-                       if (remain_mask & (1 << i))
+                       if (remain_mask & (1ULL << i))
                                _E("Failed to get meminfo: '%s'",
                                   meminfo_id_to_string(i));
        }
index 6a04c023000c31a08920ad47cf9fb3af1aefdaf4..960ab20d4f435603c59b8c291cf5a434f07c2e24 100644 (file)
@@ -177,7 +177,7 @@ int smaps_get(pid_t pid, struct smaps **maps, enum smap_mask mask)
                        unsigned int v = 0;
 
                        l = strcspn(buf, ":");
-                       if (l >= sizeof(buf) || !buf[l])
+                       if (l >= sizeof(buf) - 2 || !buf[l])
                                goto totmaps_error;
                        buf[l] = 0;
 
@@ -233,6 +233,9 @@ totmaps_error:
                        }
 
                        l = strcspn(buf, ":");
+                       if (l >= sizeof(buf) - 2 || !buf[l])
+                               goto on_error;
+
                        if (!l)
                                break;
 
index 42751d4e4c8b7dc8d7c8c9ec3ffebb053d5e8e6d..a1cb2d45ceb5eae477308c7d02fc3072797ba651 100644 (file)
@@ -91,13 +91,16 @@ resourced_ret_c get_storage_root_paths(int type, GSList **paths)
                        tzplatform_set_user(elem->uid);
                        root_path = strdup(tzplatform_getenv(TZ_USER_CONTENT));
                        tzplatform_reset_user();
-                       if (!root_path)
+                       if (!root_path) {
                                _E("Fail to get content path of uid %d", elem->uid);
+                               continue;
+                       }
 
                        if (!access(root_path, R_OK)) {
                                _D("Find new root path : %s", root_path);
                                *paths = g_slist_append(*paths, root_path);
-                       }
+                       } else
+                               free(root_path);
                }
                break;
        case EXTERNAL:
index cd53b132141c2a6be1020a453c3783ef5f3e5c52..a01da758402e03c6fa53dde4a5d04cf025fe2597 100644 (file)
@@ -2725,7 +2725,7 @@ static int heart_battery_config(struct parse_result *result, void *user_data)
                        average_pwr = atof(result->value);
                        _I("average_power: %lf", average_pwr);
                } else if (!strncmp(result->name, "OCV_SOC_POLY_COEF", sizeof("OCV_SOC_POLY_COEF") + 1)) {
-                       char *token, *saveptr;
+                       char *token, *saveptr = NULL;
                        int i;
                        token = strtok_r(result->value, " ", &saveptr);
                        if (token) {
index 227a270699bc9b6656a61aceba9256c61b431f17..9eb0395690aab92f243d7ea1166f95161140fccc 100644 (file)
@@ -196,13 +196,13 @@ static bool lowmem_limit_cb(int fd, void *data)
 {
        static char *prev_dir;
        char *cg_dir = (char *)data;
-       int ret, oom_score_adj = 0;
+       int ret;
        unsigned int usage = 0, max_mem = 0, anon_usage = 0;
        uint64_t dummy_efd;
        pid_t main_pid;
        char appname[PROC_NAME_MAX];
        struct memory_limit_event *mle;
-       struct memory_limit_log *mlog;
+       struct memory_limit_log *mlog = NULL;
        _cleanup_free_ struct cgroup_memory_stat *mem_stat = NULL;
 
        ret = read(fd, &dummy_efd, sizeof(dummy_efd));
@@ -271,11 +271,13 @@ static bool lowmem_limit_cb(int fd, void *data)
                        _D("there is no victim, removed cgroup : %s", cg_dir);
                        goto invalid_cgroup;
                }
-               proc_get_oom_score_adj(main_pid, &oom_score_adj);
-               proc_get_cmdline(main_pid, appname);
+
+               ret = proc_get_cmdline(main_pid, appname);
+               if (ret)
+                       _D("fail to get appname");
 
                _E("kill %d (name %s, memory %d) by exceeding the threshold (%d)",
-                   main_pid, appname, max_mem, mle->threshold);
+                   main_pid, appname ? appname : NULL, max_mem, mle->threshold);
        } else {
                char *filename;
 
@@ -284,7 +286,6 @@ static bool lowmem_limit_cb(int fd, void *data)
                        _D("there is no victim, removed cgroup : %s", cg_dir);
                        goto invalid_cgroup;
                }
-               proc_get_oom_score_adj(main_pid, &oom_score_adj);
 
                filename = strrchr(cg_dir, '/');
                snprintf(appname, sizeof(appname), "kerneloom_%s", filename ? filename+1 : cg_dir);
@@ -309,6 +310,8 @@ static bool lowmem_limit_cb(int fd, void *data)
 
 invalid_cgroup:
        g_hash_table_remove(memory_limit_hash, cg_dir);
+       if (mlog)
+               free(mlog);
        return false;
 }
 
index 0baf51b2bd3f745e3fb88b77fc68dfb2f919c18b..47bd394e6c94dab5cfe9906d7ad36ab1c2ac75a5 100644 (file)
@@ -2118,7 +2118,7 @@ static int lowmem_exit(void)
                g_slist_free_full(memcg_tree[i]->cgroups, free);
                free(memcg_tree[i]);
        }
-       if (event_level != (char*)MEMCG_DEFAULT_EVENT_LEVEL)
+       if (strncmp(event_level, MEMCG_DEFAULT_EVENT_LEVEL, sizeof(MEMCG_DEFAULT_EVENT_LEVEL)))
                free(event_level);
 
        lowmem_deactivate_worker();
@@ -2200,6 +2200,8 @@ void lowmem_restore_memcg(struct proc_app_info *pai)
        pai->memory.memcg_info = mi;
        if (index == MEMCG_LIMIT)
                pai->memory.use_mem_limit = true;
+
+       free(cgpath);
 }
 
 static struct module_ops memory_modules_ops = {