Fix build warning based on GCC-9 05/221305/3
authorJusung Son <jusung07.son@samsung.com>
Tue, 31 Dec 2019 04:48:22 +0000 (13:48 +0900)
committerJusung Son <jusung07.son@samsung.com>
Thu, 2 Jan 2020 08:04:57 +0000 (17:04 +0900)
Change-Id: Idbd045a60a94deb6dfaf6b86ec51a499bfd91c16
Signed-off-by: Jusung Son <jusung07.son@samsung.com>
src/preference.c
src/preference_inoti.c
src/tool/preference_tool.c

index 2cf02cad679d68e43f23888232753e57cc21870b..cbf815cdfe2cac0ad2994a0a9d564a6fecde3888 100644 (file)
@@ -834,6 +834,7 @@ static int _preference_get_key_filesys(keynode_t *keynode, int* io_errno)
        char *value = NULL;
        char *tmp_value;
        int value_size = 0;
+       int tmp_value_size = 0;
 
 retry_open:
        errno = 0;
@@ -950,25 +951,30 @@ retry:
        case PREFERENCE_TYPE_STRING:
                while (fgets(file_buf, sizeof(file_buf), fp)) {
                        if (value) {
-                               value_size = value_size + strlen(file_buf);
-                               tmp_value = (char *) realloc(value, value_size);
+                               tmp_value_size = value_size + sizeof(file_buf);
+                               tmp_value = (char *) realloc(value, tmp_value_size);
                                if (tmp_value == NULL) {
                                        func_ret = PREFERENCE_ERROR_OUT_OF_MEMORY;
                                        free(value);
                                        value = NULL;
                                        break;
                                }
+
+                               memset(tmp_value + value_size , 0x00, sizeof(file_buf));
+                               strncat(tmp_value, file_buf, tmp_value_size - strlen(tmp_value));
                                value = tmp_value;
-                               strncat(value, file_buf, strlen(file_buf));
+
+                               value_size = tmp_value_size;
+                               memset(file_buf, 0x00, sizeof(file_buf));
                        } else {
-                               value_size = strlen(file_buf) + 1;
+                               value_size = sizeof(file_buf) + 1;
                                value = (char *)malloc(value_size);
                                if (value == NULL) {
                                        func_ret = PREFERENCE_ERROR_OUT_OF_MEMORY;
                                        break;
                                }
                                memset(value, 0x00, value_size);
-                               strncpy(value, file_buf, strlen(file_buf));
+                               strncpy(value, file_buf, value_size - 1);
                        }
                }
 
index 5512629b9311d85989a2d28af1d66a77ad3c0580..9fc8cfe7d1324a4f5be5d52ae739faaf583ac1e8 100644 (file)
@@ -366,7 +366,7 @@ int _preference_kdb_del_notify(keynode_t *keynode)
        int func_ret = PREFERENCE_ERROR_NONE;
        GList *noti_list;
 
-       retvm_if(keyname == NULL, PREFERENCE_ERROR_INVALID_PARAMETER, "Invalid argument: keyname(%s)", keyname);
+       retvm_if(keyname == NULL, PREFERENCE_ERROR_INVALID_PARAMETER, "Invalid argument: keyname is null");
 
        ret = _preference_get_key_path(keynode, path);
        if (ret != PREFERENCE_ERROR_NONE) {
index 57fb7a0039f6c15cd09ac1d9ee41c8843641b33f..db030f856807869e17505e7823de7f0397058c88 100644 (file)
@@ -221,12 +221,13 @@ static gid_t __get_gid(const char *name)
        return entry.gr_gid;
 }
 
-static int _make_key_path(const char *pkgid, const char *keyname, char *path)
+static int _make_key_path(const char *pkgid, const char *keyname, char **path)
 {
        const char *key;
        gchar *convert_key;
        char pref_dir[PATH_MAX];
-       char cmd[PATH_MAX];
+       char *cmd;
+       int size;
        mode_t dir_mode = 0664 | 0111;
        const char *pkg_path;
        uid_t uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
@@ -258,13 +259,23 @@ static int _make_key_path(const char *pkgid, const char *keyname, char *path)
                }
        }
 
-       snprintf(cmd, sizeof(cmd),
+       size = snprintf(0, 0, "/usr/bin/chsmack -t -a User::Pkg::\"%s\" %s",
+                       pkgid, pref_dir);
+       cmd = (char *)malloc(size + 1);
+       if (cmd == NULL) {
+               printf("Out of memory\n");
+               return -1;
+       }
+
+       snprintf(cmd, size,
                        "/usr/bin/chsmack -t -a User::Pkg::\"%s\" %s",
                        pkgid, pref_dir);
        if (__system(cmd)) {
                printf("[pref] cmd error()\n");
+               free(cmd);
                return -1;
        }
+       free(cmd);
 
        convert_key = g_compute_checksum_for_string(G_CHECKSUM_SHA1,
                        keyname, strlen(keyname));
@@ -274,7 +285,16 @@ static int _make_key_path(const char *pkgid, const char *keyname, char *path)
        }
 
        key = (const char *)convert_key;
-       snprintf(path, PATH_MAX, "%s/%s", pref_dir, key);
+
+       size = snprintf(0, 0, "%s/%s", pref_dir, key);
+       *path = (char *)malloc(size + 1);
+       if (*path == NULL) {
+               printf("Out of memory\n");
+               g_free(convert_key);
+               return -1;
+       }
+
+       snprintf(*path, size, "%s/%s", pref_dir, key);
        g_free(convert_key);
 
        return 0;
@@ -353,20 +373,21 @@ static int _create_new_preference_key(preference_op_type OP, const char *pkgid,
 {
        FILE *fp;
        int ret;
-       char key_path[PATH_MAX] = {0,};
+       char *key_path;
        size_t keyname_len;
        int type_i;
        int temp_i;
        double temp_d;
        locale_t loc;
 
-       _make_key_path(pkgid, key, key_path);
+       _make_key_path(pkgid, key, &key_path);
 retry:
        fp = fopen(key_path, "r+");
        if (fp == NULL) {
                if (_create_key(key_path, pkgid) == -1) {
                        printf("preference key failed to create.(%d/%s)\n",
                                        errno, strerror(errno));
+                       free(key_path);
                        return -1;
                }
 
@@ -379,6 +400,7 @@ retry:
        if (ret <= 0) {
                printf("preference write key name length error(%d/%s)\n",
                                                errno, strerror(errno));
+               free(key_path);
                fclose(fp);
                return -1;
        }
@@ -387,6 +409,7 @@ retry:
        if (ret <= 0) {
                printf("preference write key name length error(%d/%s)\n",
                                                errno, strerror(errno));
+               free(key_path);
                fclose(fp);
                return -1;
        }
@@ -423,6 +446,7 @@ retry:
                        type_i = PREFERENCE_TYPE_STRING;
                } else {
                        printf("key type is invalid.int|double|bool|string\n");
+                       free(key_path);
                        fclose(fp);
                        return -1;
                }
@@ -480,6 +504,8 @@ retry:
                break;
        }
 
+       free(key_path);
+
        uselocale(LC_GLOBAL_LOCALE);
        fflush(fp);
        if (fp) {
@@ -503,6 +529,7 @@ static int _print_pref_value_from_file_path(const char *path,
        char file_buf[BUF_LEN] = {0,};
        char *value_str = NULL;
        size_t value_size = 0;
+       size_t new_value_size = 0;
        size_t diff;
        size_t file_buf_size;
        char *new_value_str;
@@ -580,29 +607,33 @@ static int _print_pref_value_from_file_path(const char *path,
        case PREFERENCE_TYPE_STRING:
                while (fgets(file_buf, sizeof(file_buf), fp)) {
                        if (value_str) {
-                               file_buf_size = strlen(file_buf);
+                               file_buf_size = sizeof(file_buf);
                                diff = INT_MAX - file_buf_size;
                                if (value_size > diff) {
                                        printf("Integer overflow\n");
                                        break;
                                }
 
-                               value_size += file_buf_size;
+                               new_value_size = value_size + file_buf_size;
                                new_value_str = (char *)realloc(value_str,
-                                               value_size);
+                                               new_value_size);
                                if (new_value_str == NULL)
                                        break;
 
+                               memset(new_value_str + value_size, 0x00, sizeof(file_buf));
+                               strncat(new_value_str, file_buf, new_value_size - strlen(new_value_str));
+
                                value_str = new_value_str;
-                               strncat(value_str, file_buf, strlen(file_buf));
+                               value_size = new_value_size;
+                               memset(file_buf, 0x00, sizeof(file_buf));
                        } else {
-                               value_size = strlen(file_buf) + 1;
+                               value_size = sizeof(file_buf) + 1;
                                value_str = (char *)malloc(value_size);
                                if (value_str == NULL)
                                        break;
 
                                memset(value_str, 0x00, value_size);
-                               strncpy(value_str, file_buf, strlen(file_buf));
+                               strncpy(value_str, file_buf, value_size - 1);
                        }
                }
 
@@ -741,23 +772,24 @@ static void _preference_set_key(const char *pkgid, const char *key,
 
 static void _print_preference_key(const char *pkgid, const char *key)
 {
-       char key_path[PATH_MAX] = {0,};
+       char *key_path;
 
-       _make_key_path(pkgid, key, key_path);
+       _make_key_path(pkgid, key, &key_path);
        _print_pref_value_from_file_path(key_path, key);
+
+       free(key_path);
 }
 
 static void _print_preference_in_package(const char *pkgid)
 {
        char pref_dir[PATH_MAX];
        DIR *dir;
-       int dfd;
+       int dfd, res, size;
        struct stat st;
-       int res;
        struct dirent *ent = NULL;
        const char *name;
        char *keyname = NULL;
-       char file_full_path[PATH_MAX];
+       char *file_full_path;
        const char *pkg_path;
 
        pkg_path = __get_package_path(pkgid);
@@ -790,10 +822,18 @@ static void _print_preference_in_package(const char *pkgid)
                                continue;
                }
 
-               snprintf(file_full_path, sizeof(file_full_path), "%s/%s",
-                               pref_dir, name);
+               size = snprintf(0, 0, "%s/%s", pref_dir, name);
+               file_full_path = (char *)malloc(size + 1);
+               if (file_full_path == NULL) {
+                       printf("Out of memory\n");
+                       closedir(dir);
+                       return;
+               }
+
+               snprintf(file_full_path, size, "%s/%s", pref_dir, name);
                _get_key_name(file_full_path, &keyname);
                _print_pref_value_from_file_path(file_full_path, keyname);
+               free(file_full_path);
                if (keyname) {
                        free(keyname);
                        keyname = NULL;
@@ -911,11 +951,12 @@ static int _convert_pref_file(const char *pref_dir)
 {
        DIR *dir;
        struct dirent *ent;
-       char old_file[BUF_LEN];
-       char new_file[BUF_LEN];
+       char *old_file;
+       char *new_file;
        gchar *convert_key;
        char _key[PREFERENCE_KEY_PATH_LEN] = {0,};
        char *chrptr = NULL;
+       int size;
 
        dir = opendir(pref_dir);
        if (!dir)
@@ -925,8 +966,15 @@ static int _convert_pref_file(const char *pref_dir)
                if (ent->d_type != DT_REG)
                        continue;
 
-               snprintf(old_file, sizeof(old_file), "%s/%s", pref_dir,
-                               ent->d_name);
+               size = snprintf(0, 0, "%s/%s", pref_dir, ent->d_name);
+               old_file = (char *)malloc(size + 1);
+               if (old_file == NULL) {
+                       printf("Out of memory\n");
+                       closedir(dir);
+                       return -1;
+               }
+
+               snprintf(old_file, size, "%s/%s", pref_dir, ent->d_name);
 
                strncpy(_key, ent->d_name, PREFERENCE_KEY_PATH_LEN - 1);
 
@@ -943,12 +991,24 @@ static int _convert_pref_file(const char *pref_dir)
                                _key, strlen(_key));
                if (convert_key == NULL) {
                        printf("fail to convert key\n");
+                       free(old_file);
                        closedir(dir);
                        return -1;
                }
 
-               snprintf(new_file, sizeof(new_file), "%s/%s", pref_dir,
-                               convert_key);
+
+               size = snprintf(0, 0, "%s/%s", pref_dir, convert_key);
+               new_file = (char *)malloc(size + 1);
+               if (new_file == NULL) {
+                       printf("Out of memory\n");
+                       free(old_file);
+                       g_free(convert_key);
+                       closedir(dir);
+                       return -1;
+               }
+
+               snprintf(new_file, size, "%s/%s", pref_dir, convert_key);
+
                g_free(convert_key);
 
                if (rename(old_file, new_file) < 0) {
@@ -959,6 +1019,9 @@ static int _convert_pref_file(const char *pref_dir)
 
                if (_add_key_info_to_file(new_file, _key) < 0)
                        printf("convert %s file failed\n", new_file);
+
+               free(old_file);
+               free(new_file);
        }
 
        closedir(dir);
@@ -986,7 +1049,7 @@ static int _convert_file_name(const char *app_dir)
                                strcmp(ent->d_name, "..") == 0)
                        continue;
 
-               snprintf(buf, sizeof(buf), "%s/%s/data/.pref",
+               snprintf(buf, sizeof(buf) - 1, "%s/%s/data/.pref",
                                app_dir, ent->d_name);
                if (access(buf, F_OK) == -1)
                        continue;