From: Adrian Szyndela Date: Wed, 1 Apr 2020 10:15:24 +0000 (+0200) Subject: tool: fix sizes in snprintfs X-Git-Tag: submit/tizen/20200813.014230~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2d7d604f2d3901d47c68044c3e81f5a9e868715e;p=platform%2Fcore%2Fapi%2Fpreference.git tool: fix sizes in snprintfs 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 --- diff --git a/src/tool/preference_tool.c b/src/tool/preference_tool.c index db030f8..a0fcfcc 100644 --- a/src/tool/preference_tool.c +++ b/src/tool/preference_tool.c @@ -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;