tool: fix sizes in snprintfs 17/229517/1
authorAdrian Szyndela <adrian.s@samsung.com>
Wed, 1 Apr 2020 10:15:24 +0000 (12:15 +0200)
committerAdrian Szyndela <adrian.s@samsung.com>
Wed, 1 Apr 2020 11:54:07 +0000 (13:54 +0200)
Several snprintfs were cutting off last letters of strings.
This commit fixes them.

The size passed to snprintf includes space for nul-byte.

This could be fixed by adding '+ 1' to final snprintfs,
but all the uses of size after computed with first snprintfs
would add '+ 1', so it can be added along with computing.

Change-Id: Ie8e62d120709a3ca1433626f1f5b8eeadc2a2b4d

src/tool/preference_tool.c

index db030f856807869e17505e7823de7f0397058c88..a0fcfcc224efa6d2a91731e2f80e7ebea59c37ff 100644 (file)
@@ -259,9 +259,9 @@ static int _make_key_path(const char *pkgid, const char *keyname, char **path)
                }
        }
 
-       size = snprintf(0, 0, "/usr/bin/chsmack -t -a User::Pkg::\"%s\" %s",
+       size = 1 + snprintf(0, 0, "/usr/bin/chsmack -t -a User::Pkg::\"%s\" %s",
                        pkgid, pref_dir);
-       cmd = (char *)malloc(size + 1);
+       cmd = (char *)malloc(size);
        if (cmd == NULL) {
                printf("Out of memory\n");
                return -1;
@@ -286,8 +286,8 @@ static int _make_key_path(const char *pkgid, const char *keyname, char **path)
 
        key = (const char *)convert_key;
 
-       size = snprintf(0, 0, "%s/%s", pref_dir, key);
-       *path = (char *)malloc(size + 1);
+       size = 1 + snprintf(0, 0, "%s/%s", pref_dir, key);
+       *path = (char *)malloc(size);
        if (*path == NULL) {
                printf("Out of memory\n");
                g_free(convert_key);
@@ -822,8 +822,8 @@ static void _print_preference_in_package(const char *pkgid)
                                continue;
                }
 
-               size = snprintf(0, 0, "%s/%s", pref_dir, name);
-               file_full_path = (char *)malloc(size + 1);
+               size = 1 + snprintf(0, 0, "%s/%s", pref_dir, name);
+               file_full_path = (char *)malloc(size);
                if (file_full_path == NULL) {
                        printf("Out of memory\n");
                        closedir(dir);
@@ -966,8 +966,8 @@ static int _convert_pref_file(const char *pref_dir)
                if (ent->d_type != DT_REG)
                        continue;
 
-               size = snprintf(0, 0, "%s/%s", pref_dir, ent->d_name);
-               old_file = (char *)malloc(size + 1);
+               size = 1 + snprintf(0, 0, "%s/%s", pref_dir, ent->d_name);
+               old_file = (char *)malloc(size);
                if (old_file == NULL) {
                        printf("Out of memory\n");
                        closedir(dir);
@@ -997,8 +997,8 @@ static int _convert_pref_file(const char *pref_dir)
                }
 
 
-               size = snprintf(0, 0, "%s/%s", pref_dir, convert_key);
-               new_file = (char *)malloc(size + 1);
+               size = 1 + snprintf(0, 0, "%s/%s", pref_dir, convert_key);
+               new_file = (char *)malloc(size);
                if (new_file == NULL) {
                        printf("Out of memory\n");
                        free(old_file);
@@ -1049,7 +1049,7 @@ static int _convert_file_name(const char *app_dir)
                                strcmp(ent->d_name, "..") == 0)
                        continue;
 
-               snprintf(buf, sizeof(buf) - 1, "%s/%s/data/.pref",
+               snprintf(buf, sizeof(buf), "%s/%s/data/.pref",
                                app_dir, ent->d_name);
                if (access(buf, F_OK) == -1)
                        continue;