Fix prevent issues 54/50954/7
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 4 Nov 2015 02:32:11 +0000 (11:32 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 4 Nov 2015 23:20:19 +0000 (08:20 +0900)
- Deference null return value
- Out-of-bounds read
- Resource leak
- Uninitialized scalar variable
- Use after free

Change-Id: I786da01dc1382e4735844158c22d8eb4bb371632
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/ail_convert.c
src/ail_db.c
src/ail_desktop.c
src/ail_filter.c
src/ail_package.c
src/ail_private.h
tool/src/ail_list.c
tool/src/ail_package.c
tool/src/createdb_user.c
tool/src/initdb_user.c
tool/src/syncdb_user.c

index 21cfcc1..40e143a 100755 (executable)
@@ -19,8 +19,6 @@
  *
  */
 
-
-
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -28,6 +26,7 @@
 #include "ail_private.h"
 #include "ail_convert.h"
 
+#define SIZEOFARRAY(x) (sizeof(x)/sizeof(x[0]))
 
 struct _ail_str_map_t {
        ail_prop_str_e prop;
@@ -35,29 +34,27 @@ struct _ail_str_map_t {
 };
 
 static struct _ail_str_map_t str_prop_map[] = {
-       {E_AIL_PROP_PACKAGE_STR,                AIL_PROP_PACKAGE_STR},
-       {E_AIL_PROP_EXEC_STR,                   AIL_PROP_EXEC_STR},
-       {E_AIL_PROP_NAME_STR,                   AIL_PROP_NAME_STR},
-       {E_AIL_PROP_TYPE_STR,                   AIL_PROP_TYPE_STR},
-       {E_AIL_PROP_ICON_STR,                   AIL_PROP_ICON_STR},
-       {E_AIL_PROP_CATEGORIES_STR,             AIL_PROP_CATEGORIES_STR},
-       {E_AIL_PROP_VERSION_STR,                AIL_PROP_VERSION_STR},
-       {E_AIL_PROP_MIMETYPE_STR,               AIL_PROP_MIMETYPE_STR},
-       {E_AIL_PROP_X_SLP_SERVICE_STR,          AIL_PROP_X_SLP_SERVICE_STR},
-       {E_AIL_PROP_X_SLP_PACKAGETYPE_STR,      AIL_PROP_X_SLP_PACKAGETYPE_STR},
+       {E_AIL_PROP_PACKAGE_STR, AIL_PROP_PACKAGE_STR},
+       {E_AIL_PROP_EXEC_STR, AIL_PROP_EXEC_STR},
+       {E_AIL_PROP_NAME_STR, AIL_PROP_NAME_STR},
+       {E_AIL_PROP_TYPE_STR, AIL_PROP_TYPE_STR},
+       {E_AIL_PROP_ICON_STR, AIL_PROP_ICON_STR},
+       {E_AIL_PROP_CATEGORIES_STR, AIL_PROP_CATEGORIES_STR},
+       {E_AIL_PROP_VERSION_STR, AIL_PROP_VERSION_STR},
+       {E_AIL_PROP_MIMETYPE_STR, AIL_PROP_MIMETYPE_STR},
+       {E_AIL_PROP_X_SLP_SERVICE_STR, AIL_PROP_X_SLP_SERVICE_STR},
+       {E_AIL_PROP_X_SLP_PACKAGETYPE_STR, AIL_PROP_X_SLP_PACKAGETYPE_STR},
        {E_AIL_PROP_X_SLP_PACKAGECATEGORIES_STR, AIL_PROP_X_SLP_PACKAGECATEGORIES_STR},
-       {E_AIL_PROP_X_SLP_PACKAGEID_STR,        AIL_PROP_X_SLP_PACKAGEID_STR},
-/*     {E_AIL_PROP_X_SLP_URI_STR,              AIL_PROP_X_SLP_URI_STR}, */
-       {E_AIL_PROP_X_SLP_SVC_STR,              AIL_PROP_X_SLP_SVC_STR},
-       {E_AIL_PROP_X_SLP_EXE_PATH,             AIL_PROP_X_SLP_EXE_PATH},
-       {E_AIL_PROP_X_SLP_APPID_STR,            AIL_PROP_X_SLP_APPID_STR},
-       {E_AIL_PROP_X_SLP_PKGID_STR,            AIL_PROP_X_SLP_PKGID_STR},
-       {E_AIL_PROP_X_SLP_DOMAIN_STR,           AIL_PROP_X_SLP_DOMAIN_STR},
-       {E_AIL_PROP_X_SLP_SUBMODEMAINID_STR,    AIL_PROP_X_SLP_SUBMODEMAINID_STR},
-       {E_AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR, AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR}
+       {E_AIL_PROP_X_SLP_PACKAGEID_STR, AIL_PROP_X_SLP_PACKAGEID_STR},
+       {E_AIL_PROP_X_SLP_SVC_STR, AIL_PROP_X_SLP_SVC_STR},
+       {E_AIL_PROP_X_SLP_EXE_PATH, AIL_PROP_X_SLP_EXE_PATH},
+       {E_AIL_PROP_X_SLP_APPID_STR, AIL_PROP_X_SLP_APPID_STR},
+       {E_AIL_PROP_X_SLP_PKGID_STR, AIL_PROP_X_SLP_PKGID_STR},
+       {E_AIL_PROP_X_SLP_DOMAIN_STR, AIL_PROP_X_SLP_DOMAIN_STR},
+       {E_AIL_PROP_X_SLP_SUBMODEMAINID_STR, AIL_PROP_X_SLP_SUBMODEMAINID_STR},
+       {E_AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR, AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR}
 };
 
-
 struct _ail_int_map_t {
        ail_prop_int_e prop;
        const char *property;
@@ -68,7 +65,6 @@ static struct _ail_int_map_t int_prop_map[] = {
        {E_AIL_PROP_X_SLP_INSTALLEDTIME_INT, AIL_PROP_X_SLP_INSTALLEDTIME_INT}
 };
 
-
 struct _ail_bool_map_t {
        ail_prop_bool_e prop;
        const char *property;
@@ -79,22 +75,20 @@ static struct _ail_bool_map_t bool_prop_map[] = {
        {E_AIL_PROP_X_SLP_TASKMANAGE_BOOL, AIL_PROP_X_SLP_TASKMANAGE_BOOL},
        {E_AIL_PROP_X_SLP_MULTIPLE_BOOL, AIL_PROP_X_SLP_MULTIPLE_BOOL},
        {E_AIL_PROP_X_SLP_REMOVABLE_BOOL, AIL_PROP_X_SLP_REMOVABLE_BOOL},
-/*     {E_AIL_PROP_X_SLP_ISHORIZONTALSCALE_BOOL, AIL_PROP_X_SLP_ISHORIZONTALSCALE_BOOL}, */
        {E_AIL_PROP_X_SLP_ENABLED_BOOL, AIL_PROP_X_SLP_ENABLED_BOOL},
        {E_AIL_PROP_X_SLP_SUBMODE_BOOL, AIL_PROP_X_SLP_SUBMODE_BOOL}
 };
 
-
 inline ail_prop_str_e _ail_convert_to_prop_str(const char *property)
 {
-       int i = 0;
+       int i;
        ail_prop_str_e prop = -1;
 
        retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
 
-       for (i=0 ; i<(E_AIL_PROP_STR_MAX - E_AIL_PROP_STR_MIN + 1) ; i++) {
+       for (i = 0; i < SIZEOFARRAY(str_prop_map); i++) {
                if (strcmp(property, str_prop_map[i].property) == 0) {
-                       prop =  str_prop_map[i].prop;
+                       prop = str_prop_map[i].prop;
                        break;
                }
        }
@@ -104,14 +98,14 @@ inline ail_prop_str_e _ail_convert_to_prop_str(const char *property)
 
 inline ail_prop_int_e _ail_convert_to_prop_int(const char *property)
 {
-       int i = 0;
+       int i;
        ail_prop_int_e prop = -1;
 
        retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
 
-       for (i=0 ; i<(E_AIL_PROP_INT_MAX - E_AIL_PROP_INT_MIN + 1) ; i++) {
+       for (i = 0; i < SIZEOFARRAY(int_prop_map); i++) {
                if (strcmp(property, int_prop_map[i].property) == 0) {
-                       prop =  int_prop_map[i].prop;
+                       prop = int_prop_map[i].prop;
                        break;
                }
        }
@@ -121,18 +115,17 @@ inline ail_prop_int_e _ail_convert_to_prop_int(const char *property)
 
 inline ail_prop_bool_e _ail_convert_to_prop_bool(const char *property)
 {
-       int i = 0;
+       int i;
        ail_prop_bool_e prop = -1;
 
        retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
 
-       for (i=0 ; i<(E_AIL_PROP_BOOL_MAX - E_AIL_PROP_BOOL_MIN + 1) ; i++) {
+       for (i = 0; i < SIZEOFARRAY(bool_prop_map); i++) {
                if (strcmp(property, bool_prop_map[i].property) == 0) {
-                       prop =  bool_prop_map[i].prop;
+                       prop = bool_prop_map[i].prop;
                        break;
                }
        }
 
        return prop;
 }
-
index 462c545..4819088 100755 (executable)
 #define QUERY_CREATE_VIEW_LOCAL "CREATE temp VIEW localname as select distinct * from (select  * from main.localname m union select * from Global.localname g)"
 
 #define SET_SMACK_LABEL(x,uid) \
-       if(smack_setlabel((x), (((uid) == GLOBAL_USER)?"*":"User"), SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"User/*\" %s", x); \
-       else _D("chsmack -a \"User/*\" %s", x);
-       
-       
-#define retv_with_dbmsg_if(expr, val) do { \
-       if (expr) { \
-               _E("db_info.dbUserro: %s", sqlite3_errmsg(db_info.dbUserro)); \
-               _E("db_info.dbGlobalro: %s", sqlite3_errmsg(db_info.dbGlobalro)); \
-               _E("db_info.dbUserrw: %s", sqlite3_errmsg(db_info.dbUserrw)); \
-               _E("db_info.dbGlobalrw: %s", sqlite3_errmsg(db_info.dbGlobalrw)); \
-               _E("db_info.dbUserro errcode: %d", sqlite3_extended_errcode(db_info.dbUserro)); \
-               _E("db_info.dbGlobalro errcode: %d", sqlite3_extended_errcode(db_info.dbGlobalro)); \
-               _E("db_info.dbUserrw errcode: %d", sqlite3_extended_errcode(db_info.dbUserrw)); \
-               _E("db_info.dbGlobalrw errcode: %d", sqlite3_extended_errcode(db_info.dbGlobalrw)); \
-               return (val); \
-       } \
-} while (0)
+       do { \
+               if (smack_setlabel((x), (((uid) == GLOBAL_USER) ? "*" : "User"), SMACK_LABEL_ACCESS)) \
+                       _E("failed chsmack -a \"User/*\" %s", x); \
+               else \
+                       _D("chsmack -a \"User/*\" %s", x); \
+       } while (0)
+
+#define retv_with_dbmsg_if(expr, val) \
+       do { \
+               if (expr) { \
+                       _E("db_info.dbUserro: %s", sqlite3_errmsg(db_info.dbUserro)); \
+                       _E("db_info.dbGlobalro: %s", sqlite3_errmsg(db_info.dbGlobalro)); \
+                       _E("db_info.dbUserrw: %s", sqlite3_errmsg(db_info.dbUserrw)); \
+                       _E("db_info.dbGlobalrw: %s", sqlite3_errmsg(db_info.dbGlobalrw)); \
+                       _E("db_info.dbUserro errcode: %d", sqlite3_extended_errcode(db_info.dbUserro)); \
+                       _E("db_info.dbGlobalro errcode: %d", sqlite3_extended_errcode(db_info.dbGlobalro)); \
+                       _E("db_info.dbUserrw errcode: %d", sqlite3_extended_errcode(db_info.dbUserrw)); \
+                       _E("db_info.dbGlobalrw errcode: %d", sqlite3_extended_errcode(db_info.dbGlobalrw)); \
+                       return (val); \
+               } \
+       } while (0)
+
+#define tryvm_with_dbmsg_if(expr, val) \
+       do { \
+               if (expr) { \
+                       _E("db_info.dbUserro: %s", sqlite3_errmsg(db_info.dbUserro)); \
+                       _E("db_info.dbGlobalro: %s", sqlite3_errmsg(db_info.dbGlobalro)); \
+                       _E("db_info.dbUserrw: %s", sqlite3_errmsg(db_info.dbUserrw)); \
+                       _E("db_info.dbGlobalrw: %s", sqlite3_errmsg(db_info.dbGlobalrw)); \
+                       _E("db_info.dbUserro errcode: %d", sqlite3_extended_errcode(db_info.dbUserro)); \
+                       _E("db_info.dbGlobalro errcode: %d", sqlite3_extended_errcode(db_info.dbGlobalro)); \
+                       _E("db_info.dbUserrw errcode: %d", sqlite3_extended_errcode(db_info.dbUserrw)); \
+                       _E("db_info.dbGlobalrw errcode: %d", sqlite3_extended_errcode(db_info.dbGlobalrw)); \
+                       goto catch; \
+               } \
+       } while (0)
 
 static __thread struct {
-        sqlite3         *dbUserro;
-        sqlite3         *dbGlobalro;
-        sqlite3         *dbUserrw;
-        sqlite3         *dbGlobalrw;
+       sqlite3 *dbUserro;
+        sqlite3 *dbGlobalro;
+        sqlite3 *dbUserrw;
+        sqlite3 *dbGlobalrw;
 } db_info = {
-        .dbUserro = NULL,
-        .dbGlobalro = NULL,
-        .dbUserrw = NULL,
-        .dbGlobalrw = NULL
+       .dbUserro = NULL,
+       .dbGlobalro = NULL,
+       .dbUserrw = NULL,
+       .dbGlobalrw = NULL
 };
-  static __thread      sqlite3         *dbInit = NULL;
+
+static __thread sqlite3 *dbInit = NULL;
 
 static int ail_db_change_perm(const char *db_file, uid_t uid)
 {
        char buf[BUFSIZE];
        char journal_file[BUFSIZE];
        char *files[3];
-       int ret, i;
+       int ret;
+       int i;
+       struct passwd pwd;
        struct passwd *userinfo = NULL;
+       char *pwd_buf = NULL;
+       int pwd_bufsize;
+
        files[0] = (char *)db_file;
        files[1] = journal_file;
        files[2] = NULL;
 
        retv_if(!db_file, AIL_ERROR_FAIL);
-       if(getuid() != OWNER_ROOT) //At this time we should be root to apply this
-                       return AIL_ERROR_OK;
-    userinfo = getpwuid(uid);
-    if (!userinfo) {
+       if (getuid() != OWNER_ROOT) /* At this time we should be root to apply this */
+               return AIL_ERROR_OK;
+
+       pwd_bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+       if (pwd_bufsize <= 0)
+               pwd_bufsize = 16384;
+
+       pwd_buf = (char *)malloc(pwd_bufsize);
+       if (pwd_buf == NULL) {
+               _E("out of memory");
+               return AIL_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (getpwuid_r(uid, &pwd, pwd_buf, pwd_bufsize, &userinfo) != 0) {
                _E("FAIL: user %d doesn't exist", uid);
+               free(pwd_buf);
                return AIL_ERROR_FAIL;
        }
        snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
 
        for (i = 0; files[i]; i++) {
-               // Compare git_t type and not group name
+               /* Compare git_t type and not group name */
                ret = chown(files[i], uid, userinfo->pw_gid);
-               SET_SMACK_LABEL(files[i],uid)
+               SET_SMACK_LABEL(files[i],uid);
                if (ret == -1) {
                        strerror_r(errno, buf, sizeof(buf));
                        _E("FAIL : chown %s %d.%d, because %s", db_file, uid, userinfo->pw_gid, buf);
+                       free(pwd_buf);
                        return AIL_ERROR_FAIL;
                }
 
@@ -109,164 +146,307 @@ static int ail_db_change_perm(const char *db_file, uid_t uid)
                if (ret == -1) {
                        strerror_r(errno, buf, sizeof(buf));
                        _E("FAIL : chmod %s 0664, because %s", db_file, buf);
+                       free(pwd_buf);
                        return AIL_ERROR_FAIL;
                }
        }
 
+       free(pwd_buf);
+
        return AIL_ERROR_OK;
 }
 
-charail_get_icon_path(uid_t uid)
+char *ail_get_icon_path(uid_t uid)
 {
        char *result = NULL;
+       struct group grpbuf;
        struct group *grpinfo = NULL;
        char *dir = NULL;
-       struct passwd *userinfo = getpwuid(uid);
+       struct passwd pwd;
+       struct passwd *userinfo = NULL;
+       int buflen;
+       char *buf = NULL;
+       char *pwd_buf;
 
        if (uid == 0) {
                _E("FAIL : Root is not allowed user! please fix it replacing with DEFAULT_USER");
                return NULL;
        }
+
        if (uid != GLOBAL_USER) {
-               if (userinfo == NULL) {
+               buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+               if (buflen <= 0)
+                       buflen = 16384;
+
+               pwd_buf = (char *)malloc(buflen);
+               if (pwd_buf == NULL) {
+                       _E("out of memory");
+                       return NULL;
+               }
+
+               if (getpwuid_r(uid, &pwd, pwd_buf, buflen, &userinfo) != 0) {
                        _E("getpwuid(%d) returns NULL !", uid);
+                       free(pwd_buf);
                        return NULL;
                }
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       _E("getgrnam(users) returns NULL !");
+
+               buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+               if (buflen <= 0)
+                       buflen = 1024;
+
+               buf = (char *)malloc(buflen);
+               if (buf == NULL) {
+                       _E("out of memory");
+                       free(pwd_buf);
                        return NULL;
                }
-               // Compare git_t type and not group name
+
+               if (getgrnam_r("users", &grpbuf, buf, buflen, &grpinfo) != 0) {
+                       _E("getgrnam_r(users) returns NULL !");
+                       free(pwd_buf);
+                       free(buf);
+                       return NULL;
+               }
+
+               /* Compare git_t type and not group name */
                if (grpinfo->gr_gid != userinfo->pw_gid) {
                        _E("UID [%d] does not belong to 'users' group!", uid);
+                       free(pwd_buf);
+                       free(buf);
                        return NULL;
                }
+
                asprintf(&result, "%s/.applications/icons/", userinfo->pw_dir);
+               free(pwd_buf);
        } else {
-               result = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
+               result = strdup(tzplatform_mkpath(TZ_SYS_RW_ICONS, "/"));
+               if (result == NULL) {
+                       _E("out of memory");
+                       return NULL;
+               }
        }
+
        int ret = mkdir(result, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
        if (ret == -1 && errno != EEXIST) {
                _E("FAIL : to create directory %s %d", result, errno);
        } else if (getuid() == OWNER_ROOT) {
-               ret = chown(result, uid, ((grpinfo)?grpinfo->gr_gid:0));
-               SET_SMACK_LABEL(result,uid)
+               ret = chown(result, uid, grpinfo ? grpinfo->gr_gid : 0);
+               SET_SMACK_LABEL(result, uid);
                if (ret == -1) {
                        char buf[BUFSIZE];
                        strerror_r(errno, buf, sizeof(buf));
-                       _E("FAIL : chown %s %d.%d, because %s", result, uid, ((grpinfo)?grpinfo->gr_gid:0), buf);
+                       _E("FAIL : chown %s %d.%d, because %s", result, uid,
+                                       grpinfo ? grpinfo->gr_gid : 0, buf);
                }
        }
+
+       free(buf);
+
        return result;
 }
 
-charail_get_app_DB_journal(uid_t uid)
+char *ail_get_app_DB_journal(uid_t uid)
 {
-
        char *app_path = ail_get_app_DB(uid);
-       charresult = NULL;
+       char *result = NULL;
 
        asprintf(&result, "%s-journal", app_path);
+       free(app_path);
+
        return  result;
 }
 
-charail_get_app_DB(uid_t uid)
+char *ail_get_app_DB(uid_t uid)
 {
        char *result = NULL;
+       struct group grpbuf;
        struct group *grpinfo = NULL;
        char *dir = NULL;
-       struct passwd *userinfo = getpwuid(uid);
+       char *temp = NULL;
+       struct passwd pwd;
+       struct passwd *userinfo;
+       int buflen;
+       char *buf = NULL;
+       char *pwd_buf;
 
        if (uid == 0) {
                _E("FAIL : Root is not allowed! switch to DEFAULT_USER");
                return NULL;
        }
+
        if (uid != GLOBAL_USER) {
-               if (userinfo == NULL) {
+               buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+               if (buflen <= 0)
+                       buflen = 16384;
+
+               pwd_buf = (char *)malloc(buflen);
+               if (pwd_buf == NULL) {
+                       _E("out of memory");
+                       return NULL;
+               }
+
+               if (getpwuid_r(uid, &pwd, pwd_buf, buflen, &userinfo) != 0) {
                        _E("getpwuid(%d) returns NULL !", uid);
+                       free(pwd_buf);
                        return NULL;
                }
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
+
+               buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+               if (buflen <= 0)
+                       buflen = 1024;
+
+               buf = (char *)malloc(buflen);
+               if (buf == NULL) {
+                       _E("out of memory");
+                       free(pwd_buf);
+                       return NULL;
+               }
+
+               if (getgrnam_r("users", &grpbuf, buf, buflen, &grpinfo) != 0) {
                        _E("getgrnam(users) returns NULL !");
+                       free(pwd_buf);
+                       free(buf);
                        return NULL;
                }
-               // Compare git_t type and not group name
+
+               /* Compare git_t type and not group name */
                if (grpinfo->gr_gid != userinfo->pw_gid) {
                        _E("UID [%d] does not belong to 'users' group!", uid);
+                       free(pwd_buf);
+                       free(buf);
                        return NULL;
                }
+
                asprintf(&result, "%s/.applications/dbspace/.app_info.db", userinfo->pw_dir);
+               free(pwd_buf);
        } else {
-                       result = strdup(APP_INFO_DB_FILE);
+               result = strdup(APP_INFO_DB_FILE);
+               if (result == NULL) {
+                       _E("ouf of memory");
+                       return NULL;
+               }
+       }
+
+       temp = strdup(result);
+       if (temp == NULL) {
+               free(buf);
+               return result;
        }
-       char *temp = strdup(result);
+
        dir = strrchr(temp, '/');
-       if(!dir)
-       {
+       if (!dir) {
                free(temp);
+               free(buf);
                return result;
        }
        *dir = 0;
-               int ret = mkdir(temp, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-               if (ret == -1 && errno != EEXIST) {
-                       _E("FAIL : to create directory %s %d", temp, errno);
-               } else if (getuid() == OWNER_ROOT) {
-                       ret = chown(temp, uid, ((grpinfo)?grpinfo->gr_gid:0));
-                       SET_SMACK_LABEL(temp,uid)
-                       if (ret == -1) {
-                               char buf[BUFSIZE];
-                               strerror_r(errno, buf, sizeof(buf));
-                               _E("FAIL : chown %s %d.%d, because %s", temp, uid, ((grpinfo)?grpinfo->gr_gid:0), buf);
-                       }
+
+       int ret = mkdir(temp, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+       if (ret == -1 && errno != EEXIST) {
+               _E("FAIL : to create directory %s %d", temp, errno);
+       } else if (getuid() == OWNER_ROOT) {
+               ret = chown(temp, uid, grpinfo ? grpinfo->gr_gid : 0);
+               SET_SMACK_LABEL(temp,uid);
+               if (ret == -1) {
+                       char buf[BUFSIZE];
+                       strerror_r(errno, buf, sizeof(buf));
+                       _E("FAIL : chown %s %d.%d, because %s", temp, uid,
+                                       grpinfo ? grpinfo->gr_gid : 0, buf);
                }
+       }
+
        free(temp);
+       free(buf);
+
        return result;
 }
 
-charail_get_desktop_path(uid_t uid)
+char *ail_get_desktop_path(uid_t uid)
 {
        char *result = NULL;
+       struct group grpbuf;
        struct group *grpinfo = NULL;
        char *dir = NULL;
-       struct passwd *userinfo = getpwuid(uid);
+       struct passwd pwd;
+       struct passwd *userinfo = NULL;
+       int buflen;
+       char *buf = NULL;
+       char *pwd_buf;
 
        if (uid == 0) {
                _E("FAIL : Root is not allowed user! please fix it replacing with DEFAULT_USER");
                return NULL;
        }
+
        if (uid != GLOBAL_USER) {
-               if (userinfo == NULL) {
+               buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+               if (buflen <= 0)
+                       buflen = 16384;
+
+               pwd_buf = (char *)malloc(buflen);
+               if (pwd_buf == NULL) {
+                       _E("out of memory");
+                       return NULL;
+               }
+
+               if (getpwuid_r(uid, &pwd, pwd_buf, buflen, &userinfo) != 0) {
                        _E("getpwuid(%d) returns NULL !", uid);
+                       free(pwd_buf);
                        return NULL;
                }
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
+
+               buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+               if (buflen <= 0)
+                       buflen = 1024;
+
+               buf = (char *)malloc(buflen);
+               if (buf == NULL) {
+                       _E("out of memory");
+                       free(pwd_buf);
+                       return NULL;
+               }
+
+               if (getgrnam_r("users", &grpbuf, buf, buflen, &grpinfo) != 0) {
                        _E("getgrnam(users) returns NULL !");
+                       free(pwd_buf);
+                       free(buf);
                        return NULL;
                }
-               // Compare git_t type and not group name
+
+               /* Compare git_t type and not group name */
                if (grpinfo->gr_gid != userinfo->pw_gid) {
                        _E("UID [%d] does not belong to 'users' group!", uid);
+                       free(pwd_buf);
+                       free(buf);
                        return NULL;
                }
+
                asprintf(&result, "%s/.applications/desktop/", userinfo->pw_dir);
+               free(pwd_buf);
        } else {
-               result = tzplatform_mkpath(TZ_SYS_RW_DESKTOP_APP, "/");
+               result = strdup(tzplatform_mkpath(TZ_SYS_RW_DESKTOP_APP, "/"));
+               if (result == NULL) {
+                       _E("out of memory");
+                       return NULL;
+               }
        }
-               int ret = mkdir(result, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-               if (ret == -1 && errno != EEXIST) {
-                       _E("FAIL : to create directory %s %d", result, errno);
-               } else if (getuid() == OWNER_ROOT) {
-                       ret = chown(result, uid, ((grpinfo)?grpinfo->gr_gid:0));
-                       SET_SMACK_LABEL(result,uid)
-                       if (ret == -1) {
-                               char buf[BUFSIZE];
-                               strerror_r(errno, buf, sizeof(buf));
-                               _E("FAIL : chown %s %d.%d, because %s", result, uid, ((grpinfo)?grpinfo->gr_gid:0), buf);
-                       }
+
+       int ret = mkdir(result, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+       if (ret == -1 && errno != EEXIST) {
+               _E("FAIL : to create directory %s %d", result, errno);
+       } else if (getuid() == OWNER_ROOT) {
+               ret = chown(result, uid, grpinfo ? grpinfo->gr_gid : 0);
+               SET_SMACK_LABEL(result,uid);
+               if (ret == -1) {
+                       char buf[BUFSIZE];
+                       strerror_r(errno, buf, sizeof(buf));
+                       _E("FAIL : chown %s %d.%d, because %s", result, uid,
+                                       grpinfo ? grpinfo->gr_gid : 0, buf);
                }
+       }
+
+       free(buf);
+
        return result;
 }
 
@@ -292,6 +472,8 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
        int ret;
        int changed = 0;
        int i;
+       char *db;
+       char *global_db;
        const char *tbls[3] = {
                "CREATE TABLE app_info "
                "(package TEXT PRIMARY KEY, "
@@ -332,15 +514,26 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
                NULL
        };
 
-       char *db = ail_get_app_DB(uid);
-       char *global_db = ail_get_app_DB(GLOBAL_USER);
+       db = ail_get_app_DB(uid);
+       if (db == NULL) {
+               _E("Failed to get app DB");
+               return AIL_ERROR_DB_FAILED;
+       }
+
+       global_db = ail_get_app_DB(GLOBAL_USER);
+       if (global_db == NULL) {
+               _E("Failed to get global app DB");
+               free(db);
+               return AIL_ERROR_DB_FAILED;
+       }
 
        if (access(db, F_OK)) {
                if (AIL_ERROR_OK == db_util_open_with_options(db, &dbInit, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))
                {
                        for (i = 0; tbls[i] != NULL; i++) {
                                ret = do_db_exec(tbls[i], dbInit);
-                               retv_if(ret != AIL_ERROR_OK, AIL_ERROR_DB_FAILED);
+                               if (ret != AIL_ERROR_OK)
+                                       goto catch;
                        }
                        if(getuid() == OWNER_ROOT && AIL_ERROR_OK != ail_db_change_perm(db, uid)) {
                                _E("Failed to change permission\n");
@@ -350,9 +543,10 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
                        _E("Failed to create table %s\n", db);
                }
        }
-       if(dbInit) {
+
+       if (dbInit) {
                ret = sqlite3_close(dbInit);
-               retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
+               tryvm_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
                dbInit = NULL;
        }
        if(mode & DB_OPEN_RO) {
@@ -364,39 +558,41 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
                                char query_view_local[AIL_SQL_QUERY_MAX_LEN];
                                snprintf(query_attach, AIL_SQL_QUERY_MAX_LEN, QUERY_ATTACH, global_db);
                                if (db_exec_usr_ro(query_attach) < 0) {
-                                       _D("executing query_attach : %s", query_attach );
-                                       goto error;
+                                       _D("executing query_attach : %s", query_attach);
+                                       goto catch;
                                }
                                snprintf(query_view_app, AIL_SQL_QUERY_MAX_LEN, QUERY_CREATE_VIEW_APP);
                                if (db_exec_usr_ro(query_view_app) < 0) {
-                                       _D("executing query_attach : %s", query_view_app );
-                                       goto error;
+                                       _D("executing query_attach : %s", query_view_app);
+                                       goto catch;
                                }
 
                                snprintf(query_view_local, AIL_SQL_QUERY_MAX_LEN, QUERY_CREATE_VIEW_LOCAL);
                                if (db_exec_usr_ro(query_view_local) < 0) {
-                                       _D("executing query_attach : %s", query_view_local );
-                                       goto error;
+                                       _D("executing query_attach : %s", query_view_local);
+                                       goto catch;
                                }
                        }
                } else {
                        if (!db_info.dbGlobalro) {
                                ret = db_util_open_with_options(global_db, &db_info.dbGlobalro, SQLITE_OPEN_READONLY, NULL);
-                               retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
+                               tryvm_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
                        }
                }
        }
-       if(mode & DB_OPEN_RW) {
-               if(uid != GLOBAL_USER) {
-                       if(!db_info.dbUserrw){
+
+       if (mode & DB_OPEN_RW) {
+               if (uid != GLOBAL_USER) {
+                       if (!db_info.dbUserrw) {
                                ret = db_util_open(db, &db_info.dbUserrw, 0);
+                               tryvm_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
                        }
                } else {
-                       if(!db_info.dbGlobalrw){
+                       if (!db_info.dbGlobalrw) {
                                ret = db_util_open(global_db, &db_info.dbGlobalrw, 0);
+                               tryvm_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
                        }
                }
-               retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
        }
 
        free(global_db);
@@ -404,14 +600,13 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
 
        return AIL_ERROR_OK;
 
-error:
+catch:
        free(global_db);
        free(db);
 
        return AIL_ERROR_DB_FAILED;
 }
 
-
 ail_error_e db_prepare(const char *query, sqlite3_stmt **stmt)
 {
        return db_do_prepare(db_info.dbUserro, query, stmt);
@@ -427,13 +622,11 @@ ail_error_e db_prepare_rw(const char *query, sqlite3_stmt **stmt)
        return db_do_prepare(db_info.dbUserrw, query, stmt);
 }
 
-
 ail_error_e db_prepare_globalrw(const char *query, sqlite3_stmt **stmt)
 {
        return db_do_prepare(db_info.dbGlobalrw, query, stmt);
 }
 
-
 ail_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value)
 {
        int ret;
@@ -446,8 +639,6 @@ ail_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value)
        return AIL_ERROR_OK;
 }
 
-
-
 ail_error_e db_bind_int(sqlite3_stmt *stmt, int idx, int value)
 {
        int ret;
@@ -472,7 +663,6 @@ ail_error_e db_bind_text(sqlite3_stmt *stmt, int idx, char* value)
        return AIL_ERROR_OK;
 }
 
-
 ail_error_e db_step(sqlite3_stmt *stmt)
 {
        int ret;
@@ -490,8 +680,6 @@ ail_error_e db_step(sqlite3_stmt *stmt)
        retv_with_dbmsg_if(1, AIL_ERROR_DB_FAILED);
 }
 
-
-
 ail_error_e db_column_bool(sqlite3_stmt *stmt, int index, bool *value)
 {
        int out_val;
@@ -505,8 +693,6 @@ ail_error_e db_column_bool(sqlite3_stmt *stmt, int index, bool *value)
        return AIL_ERROR_OK;
 }
 
-
-
 ail_error_e db_column_int(sqlite3_stmt *stmt, int index, int *value)
 {
        retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
@@ -517,8 +703,6 @@ ail_error_e db_column_int(sqlite3_stmt *stmt, int index, int *value)
        return AIL_ERROR_OK;
 }
 
-
-
 ail_error_e db_column_str(sqlite3_stmt *stmt, int index, char **str)
 {
        retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
@@ -529,8 +713,6 @@ ail_error_e db_column_str(sqlite3_stmt *stmt, int index, char **str)
        return AIL_ERROR_OK;
 }
 
-
-
 ail_error_e db_reset(sqlite3_stmt *stmt)
 {
        int ret;
@@ -545,8 +727,6 @@ ail_error_e db_reset(sqlite3_stmt *stmt)
        return AIL_ERROR_OK;
 }
 
-
-
 ail_error_e db_finalize(sqlite3_stmt *stmt)
 {
        int ret;
@@ -559,8 +739,6 @@ ail_error_e db_finalize(sqlite3_stmt *stmt)
        return AIL_ERROR_OK;
 }
 
-
-
 ail_error_e do_db_exec(const char *query, sqlite3 * fileSQL)
 {
        int ret;
@@ -581,14 +759,11 @@ ail_error_e do_db_exec(const char *query, sqlite3 * fileSQL)
        return AIL_ERROR_OK;
 }
 
-
-
 ail_error_e db_exec_usr_rw(const char *query)
 {
        return do_db_exec(query, db_info.dbUserrw);
 }
 
-
 ail_error_e db_exec_usr_ro(const char *query)
 {
        return do_db_exec(query, db_info.dbUserro);
@@ -604,7 +779,6 @@ ail_error_e db_exec_glo_rw(const char *query)
        return do_db_exec(query, db_info.dbGlobalrw);
 }
 
-
 ail_error_e db_close(void)
 {
        int ret;
@@ -660,5 +834,3 @@ int db_exec_sqlite_query(char *query, sqlite_query_callback callback, void *data
        sqlite3_free(error_message);
        return 0;
 }
-
-// End of file.
index 4b80bab..b2a9bf1 100755 (executable)
@@ -270,6 +270,12 @@ _get_icon_with_path(char* icon, uid_t uid)
                }
 #else
                theme = strdup("default");
+               if (theme == NULL) {
+                       _E("out of memory");
+                       free(package);
+                       return NULL;
+               }
+
 #endif
 
                len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
@@ -286,7 +292,7 @@ _get_icon_with_path(char* icon, uid_t uid)
                        sqlite3_snprintf( len, icon_with_path, "%s%q", ail_get_icon_path(uid), icon);
                else
                        sqlite3_snprintf( len, icon_with_path, "%s/%q/small/%q", ail_get_icon_path(GLOBAL_USER), theme, icon);
-                       
+
                if (access (icon_with_path, F_OK)) {
                        app_path = tzplatform_getenv(TZ_SYS_RW_APP);
                        if (app_path)
@@ -770,14 +776,13 @@ static int _count_all(uid_t uid)
        if (uid != GLOBAL_USER)
                ret = ail_filter_count_usr_appinfo(NULL, &count, uid);
        else
-               ret = ail_filter_count_appinfo(NULL, &count);   
-       if(ret != AIL_ERROR_OK) {
+               ret = ail_filter_count_appinfo(NULL, &count);
+
+       if (ret != AIL_ERROR_OK) {
                _E("cannot count appinfo");
                count = -1;
        }
 
-       retv_if(ret != AIL_ERROR_OK, -1);
-
        return count;
 }
 
@@ -789,15 +794,24 @@ char *_pkgname_to_desktop(const char *package, uid_t uid)
 
        retv_if(!package, NULL);
 
-  desktop_path = ail_get_desktop_path(uid);
+       desktop_path = ail_get_desktop_path(uid);
+       if (desktop_path == NULL) {
+               _E("Failed to get desktop path");
+               return NULL;
+       }
 
        size = strlen(desktop_path) + strlen(package) + 10;
        desktop = malloc(size);
-       retv_if(!desktop, NULL);
+       if (desktop == NULL) {
+               _E("out of memory");
+               free(desktop_path);
+               return NULL;
+       }
 
-  snprintf(desktop, size, "%s/%s.desktop", desktop_path, package);
+       snprintf(desktop, size, "%s/%s.desktop", desktop_path, package);
+       _D("uid: %d / desktop: [%s]\n",  uid, desktop);
 
-  _D("uid: %d / desktop: [%s]\n",  uid, desktop);
+       free(desktop_path);
 
        return desktop;
 }
@@ -826,7 +840,6 @@ static inline int _bind_local_info(desktop_info_s* info, sqlite3_stmt * stmt)
        return AIL_ERROR_OK;
 }
 
-
 static inline int _len_local_info(desktop_info_s* info)
 {
        int len = 0;
@@ -845,7 +858,6 @@ static inline int _len_local_info(desktop_info_s* info)
        return len;
 }
 
-
 static inline int _insert_local_info(desktop_info_s* info, uid_t uid)
 {
        int len_query = SQL_INSERT_LOCALNAME_STR_LEN;
@@ -854,27 +866,30 @@ static inline int _insert_local_info(desktop_info_s* info, uid_t uid)
        int ret = AIL_ERROR_OK;
        sqlite3_stmt *stmt = NULL;
        int i = 0;
+
        retv_if(!info, AIL_ERROR_INVALID_PARAMETER);
        retv_if(!info->localname, AIL_ERROR_INVALID_PARAMETER);
 
        nb_locale_args = _len_local_info(info);
-
        retv_if(!nb_locale_args, AIL_ERROR_INVALID_PARAMETER);
 
-       len_query += SQL_LOCALNAME_TRIPLET_STR_LEN*nb_locale_args +1;
+       len_query += SQL_LOCALNAME_TRIPLET_STR_LEN*nb_locale_args + 1;
 
-       query = (char *) malloc(len_query);
+       query = (char *)malloc(len_query);
        retv_if(!query, AIL_ERROR_OUT_OF_MEMORY);
+
        stpncpy(query, SQL_INSERT_LOCALNAME_INIT_STR, len_query);
-       for (i = 0; i <  nb_locale_args - 1; i++)
-               strcat(query, SQL_LOCALNAME_TRIPLET_STR);
+       for (i = 0; i < nb_locale_args - 1; i++)
+               strncat(query, SQL_LOCALNAME_TRIPLET_STR, len_query - strlen(query) - 1);
 
        do {
-               if(uid != GLOBAL_USER)
+               if (uid != GLOBAL_USER)
                        ret = db_prepare_rw(query, &stmt);
-               else 
+               else
                        ret = db_prepare_globalrw(query, &stmt);
-               if (ret < 0) break;
+
+               if (ret < 0)
+                       break;
 
                ret = _bind_local_info(info, stmt);
                if (ret < 0) {
@@ -882,6 +897,7 @@ static inline int _insert_local_info(desktop_info_s* info, uid_t uid)
                        db_finalize(stmt);
                        break;
                }
+
                ret = db_step(stmt);
                if (ret != AIL_ERROR_NO_DATA) {
                        /* Insert Request doesn't return any data.
@@ -890,10 +906,12 @@ static inline int _insert_local_info(desktop_info_s* info, uid_t uid)
                        db_finalize(stmt);
                        break;
                }
+
                ret = db_finalize(stmt);
-       } while(0);
+       } while (0);
 
        free(query);
+
        return ret;
 }
 
@@ -940,9 +958,9 @@ static ail_error_e _init_desktop_info(desktop_info_s *info, const char *package,
 {
        static int is_initdb = -1;
 
-  _D("package - [%s].", package);
+       _D("package - [%s].", package);
 
-       if(is_initdb == -1)
+       if (is_initdb == -1)
                is_initdb = __is_ail_initdb();
 
        retv_if(!info, AIL_ERROR_INVALID_PARAMETER);
@@ -955,7 +973,7 @@ static ail_error_e _init_desktop_info(desktop_info_s *info, const char *package,
        info->x_slp_removable = 1;
        info->x_slp_submode = 0;
 
-       if(is_initdb)
+       if (is_initdb)
                info->x_slp_installedtime = 0;
        else
                info->x_slp_installedtime = time(NULL);
@@ -969,6 +987,7 @@ static ail_error_e _init_desktop_info(desktop_info_s *info, const char *package,
 
        info->x_slp_packageid = strdup(package);
        retv_if(!info->x_slp_packageid, AIL_ERROR_OUT_OF_MEMORY);
+
        info->x_slp_appid = strdup(package);
        retv_if(!info->x_slp_appid, AIL_ERROR_OUT_OF_MEMORY);
 
@@ -977,13 +996,11 @@ static ail_error_e _init_desktop_info(desktop_info_s *info, const char *package,
        info->desktop = _pkgname_to_desktop(package, uid);
        retv_if(!info->desktop, AIL_ERROR_FAIL);
 
-  _D("desktop - [%s].", info->desktop);
+       _D("desktop - [%s].", info->desktop);
 
        return AIL_ERROR_OK;
 }
 
-
-
 static ail_error_e _read_desktop_info(desktop_info_s* info,uid_t uid)
 {
        char *line = NULL;
@@ -1124,11 +1141,15 @@ static ail_error_e _load_desktop_info(desktop_info_s* info, uid_t uid)
        char query[AIL_SQL_QUERY_MAX_LEN];
        sqlite3_stmt *stmt = NULL;
        char w[AIL_SQL_QUERY_MAX_LEN];
+       const char *filter;
 
        retv_if(!info, AIL_ERROR_INVALID_PARAMETER);
 
-       snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), info->package);
+       filter = sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR);
+       if (filter == NULL)
+               return AIL_ERROR_FAIL;
 
+       snprintf(w, sizeof(w), filter, info->package);
        snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
 
        do {
@@ -1337,20 +1358,18 @@ static ail_error_e _insert_desktop_info(desktop_info_s *info, uid_t uid)
                ret = db_exec_usr_rw(query);
        else
                ret = db_exec_glo_rw(query);
-       
+
+       _D("Add (%s).", query);
        free(query);
+
        retv_if(ret != AIL_ERROR_OK, AIL_ERROR_DB_FAILED);
 
        if (info->localname)
                _insert_local_info(info, uid);
 
-       _D("Add (%s).", query);
-
        return AIL_ERROR_OK;
 }
 
-
-
 static ail_error_e _update_desktop_info(desktop_info_s *info, uid_t uid)
 {
        char *query;
@@ -1493,7 +1512,7 @@ static ail_error_e _remove_package(const char* package, uid_t uid)
        }
        snprintf(query, size, "delete from localname where package = '%s'", package);
        _D("query=%s",query);
-       
+
        if(uid != GLOBAL_USER) {
                if (db_exec_usr_rw(query) < 0) {
                        free(query);
@@ -1551,7 +1570,7 @@ static ail_error_e _clean_pkgid_data(const char* pkgid, uid_t uid)
                if (db_exec_glo_rw(query) < 0) {
                        free(query);
                        return AIL_ERROR_DB_FAILED;
-               }       
+               }
        }
        _D("Clean pkgid data (%s).", pkgid);
        free(query);
@@ -1644,7 +1663,6 @@ static int __is_authorized()
                return 0;
 }
 
-
 /* Public functions */
 EXPORT_API ail_error_e ail_usr_desktop_add(const char *appid, uid_t uid)
 {
@@ -1654,25 +1672,28 @@ EXPORT_API ail_error_e ail_usr_desktop_add(const char *appid, uid_t uid)
        retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
 
        ret = _init_desktop_info(&info, appid, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
-       ret = _read_desktop_info(&info,uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       ret = _read_desktop_info(&info, uid);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _insert_desktop_info(&info, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _send_db_done_noti(NOTI_ADD, appid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
+end:
        _fini_desktop_info(&info);
 
-       return AIL_ERROR_OK;
+       return ret;
 }
 
 EXPORT_API ail_error_e ail_desktop_add(const char *appid)
 {
-       return ail_usr_desktop_add(appid,GLOBAL_USER);
+       return ail_usr_desktop_add(appid, GLOBAL_USER);
 }
 
 EXPORT_API ail_error_e ail_usr_desktop_update(const char *appid, uid_t uid)
@@ -1683,20 +1704,23 @@ EXPORT_API ail_error_e ail_usr_desktop_update(const char *appid, uid_t uid)
        retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
 
        ret = _init_desktop_info(&info, appid, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
-       ret = _read_desktop_info(&info,uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       ret = _read_desktop_info(&info, uid);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _update_desktop_info(&info, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _send_db_done_noti(NOTI_UPDATE, appid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
+end:
        _fini_desktop_info(&info);
 
-       return AIL_ERROR_OK;
+       return ret;
 }
 
 EXPORT_API ail_error_e ail_desktop_update(const char *appid)
@@ -1753,17 +1777,19 @@ EXPORT_API ail_error_e ail_usr_desktop_fota(const char *appid, uid_t uid)
        retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
 
        ret = _init_desktop_info(&info, appid, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _read_desktop_info(&info,uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _insert_desktop_info(&info, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
+end:
        _fini_desktop_info(&info);
 
-       return AIL_ERROR_OK;
+       return ret;
 }
 
 EXPORT_API ail_error_e ail_desktop_fota(const char *appid)
@@ -1773,9 +1799,7 @@ EXPORT_API ail_error_e ail_desktop_fota(const char *appid)
 
 
 EXPORT_API ail_error_e ail_desktop_appinfo_modify_usr_bool(const char *appid,
-                                                            const char *property,
-                                                            bool value,
-                                                            bool broadcast, uid_t uid)
+               const char *property, bool value, bool broadcast, uid_t uid)
 {
        desktop_info_s info = {0,};
        ail_error_e ret;
@@ -1783,44 +1807,43 @@ EXPORT_API ail_error_e ail_desktop_appinfo_modify_usr_bool(const char *appid,
        retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
 
        retv_if(strcmp(property, AIL_PROP_X_SLP_ENABLED_BOOL),
-               AIL_ERROR_INVALID_PARAMETER);
+                               AIL_ERROR_INVALID_PARAMETER);
 
        ret = _init_desktop_info(&info, appid, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _load_desktop_info(&info, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _modify_desktop_info_bool(&info, property, value);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _update_desktop_info(&info, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
-       if (broadcast) {
+       if (broadcast)
                ret = _send_db_done_noti(NOTI_UPDATE, appid);
-               retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
-       }
 
+end:
        _fini_desktop_info(&info);
 
-       return AIL_ERROR_OK;
+       return ret;
 }
 
 EXPORT_API ail_error_e ail_desktop_appinfo_modify_bool(const char *appid,
-                                                            const char *property,
-                                                            bool value,
-                                                            bool broadcast)
+                       const char *property, bool value, bool broadcast)
 {
-       return ail_desktop_appinfo_modify_usr_bool(appid, property, value, broadcast,
-                       GLOBAL_USER);
+       return ail_desktop_appinfo_modify_usr_bool(appid, property, value,
+                                               broadcast, GLOBAL_USER);
 }
 
 
-EXPORT_API ail_error_e ail_desktop_appinfo_modify_usr_str(const char *appid, uid_t uid,
-                                                            const char *property,
-                                                            const char *value,
-                                                            bool broadcast)
+EXPORT_API ail_error_e ail_desktop_appinfo_modify_usr_str(const char *appid,
+       uid_t uid, const char *property, const char *value, bool broadcast)
 {
        desktop_info_s info = {0,};
        ail_error_e ret;
@@ -1828,36 +1851,36 @@ EXPORT_API ail_error_e ail_desktop_appinfo_modify_usr_str(const char *appid, uid
        retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
 
        ret = _init_desktop_info(&info, appid, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        ret = _load_desktop_info(&info, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
        _D("info.name [%s], value [%s]", info.name, value);
        ret = _modify_desktop_info_str(&info, property, value);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
+
        _D("info.name [%s], value [%s]", info.name, value);
 
        ret = _update_desktop_info(&info, uid);
-       retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
+       if (ret != AIL_ERROR_OK)
+               goto end;
 
-       if (broadcast) {
+       if (broadcast)
                ret = _send_db_done_noti(NOTI_UPDATE, appid);
-               retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
-       }
 
+end:
        _fini_desktop_info(&info);
 
-       return AIL_ERROR_OK;
+       return ret;
 }
 
 EXPORT_API ail_error_e ail_desktop_appinfo_modify_str(const char *appid,
-                                                            const char *property,
-                                                            const char *value,
-                                                            bool broadcast)
+                       const char *property, const char *value, bool broadcast)
 {
-       return ail_desktop_appinfo_modify_usr_str(appid, GLOBAL_USER, property, value,
-                               broadcast);
+       return ail_desktop_appinfo_modify_usr_str(appid, GLOBAL_USER, property,
+                                               value, broadcast);
 }
-
-// End of File
index ffed705..096b28a 100755 (executable)
@@ -176,9 +176,12 @@ static void _get_condition(gpointer data, char **condition)
        struct element *e = (struct element *)data;
        const char *f;
        char buf[AIL_SQL_QUERY_MAX_LEN];
+       int t;
 
        f =  sql_get_filter(e->prop);
-       int t;
+       if (f == NULL)
+               return;
+
        ELEMENT_TYPE(e, t);
 
        switch (t) {
@@ -213,7 +216,7 @@ char *_get_where_clause(ail_filter_h filter)
        c = NULL;
 
        GSList *l;
-       
+
        snprintf(w, AIL_SQL_QUERY_MAX_LEN, " WHERE ");
 
        for (l = filter->list; l; l = g_slist_next(l)) {
@@ -285,6 +288,11 @@ EXPORT_API ail_error_e ail_filter_count_appinfo(ail_filter_h filter, int *cnt)
                return AIL_ERROR_DB_FAILED;
        }
        ai = appinfo_create();
+       if (ai == NULL) {
+               _E("out of memory");
+               db_finalize(stmt);
+               return AIL_ERROR_FAIL;
+       }
 
        appinfo_set_stmt(ai, stmt);
        while (db_step(stmt) == AIL_ERROR_OK) {
@@ -351,7 +359,13 @@ EXPORT_API ail_error_e ail_filter_count_usr_appinfo(ail_filter_h filter, int *cn
                _E("db_prepare fail for query = %s",q);
                return AIL_ERROR_DB_FAILED;
        }
+
        ai = appinfo_create();
+       if (ai == NULL) {
+               _E("Failed to create appinfo");
+               db_finalize(stmt);
+               return AIL_ERROR_OUT_OF_MEMORY;
+       }
 
        appinfo_set_stmt(ai, stmt);
        while (db_step(stmt) == AIL_ERROR_OK) {
@@ -423,6 +437,11 @@ EXPORT_API ail_error_e ail_filter_list_appinfo_foreach(ail_filter_h filter, ail_
                return AIL_ERROR_DB_FAILED;
        }*/
        ai = appinfo_create();
+       if (ai == NULL) {
+               _E("out of memory");
+               db_finalize(stmt);
+               return AIL_ERROR_FAIL;
+       }
 
        appinfo_set_stmt(ai, stmt);
        uint i = 0;
@@ -487,7 +506,14 @@ EXPORT_API ail_error_e ail_filter_list_usr_appinfo_foreach(ail_filter_h filter,
                _E("db_prepare fail for query = %s",q);
                return AIL_ERROR_DB_FAILED;
        }
+
        ai = appinfo_create();
+       if (ai == NULL) {
+               _E("Failed to create appinfo");
+               db_finalize(stmt);
+               return AIL_ERROR_DB_FAILED;
+       }
+
        appinfo_set_stmt(ai, stmt);
        uint i = 0;
        while (i = db_step(stmt) == AIL_ERROR_OK) {
@@ -498,8 +524,9 @@ EXPORT_API ail_error_e ail_filter_list_usr_appinfo_foreach(ail_filter_h filter,
                if (AIL_CB_RET_CANCEL == r)
                        break;
        }
-       appinfo_destroy(ai);
 
+       appinfo_destroy(ai);
        db_finalize(stmt);
+
        return AIL_ERROR_OK;
 }
index d8d7dcc..6657e61 100755 (executable)
@@ -128,7 +128,7 @@ static char* __convert_syslocale_to_manifest_locale(char *syslocale)
                return NULL;
        }
 
-       sprintf(locale, "%c%c_%c%c", syslocale[0], syslocale[1], toupper(syslocale[3]), toupper(syslocale[4]));
+       snprintf(locale, 6, "%c%c_%c%c", syslocale[0], syslocale[1], toupper(syslocale[3]), toupper(syslocale[4]));
        return locale;
 }
 
@@ -296,7 +296,6 @@ EXPORT_API ail_error_e ail_destroy_appinfo(ail_appinfo_h ai)
        return AIL_ERROR_OK;
 }
 
-
 EXPORT_API ail_error_e ail_package_get_appinfo(const char *package, ail_appinfo_h *ai)
 {
        return ail_get_appinfo(package, ai);
@@ -307,14 +306,13 @@ EXPORT_API ail_error_e ail_package_get_usr_appinfo(const char *package, uid_t ui
        return ail_get_usr_appinfo(package, uid, ai);
 }
 
-
-
 EXPORT_API ail_error_e ail_get_appinfo(const char *appid, ail_appinfo_h *ai)
 {
        ail_error_e ret;
        char query[AIL_SQL_QUERY_MAX_LEN];
        sqlite3_stmt *stmt = NULL;
        char w[AIL_SQL_QUERY_MAX_LEN];
+       const char *filter;
 
        retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
        retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
@@ -322,19 +320,25 @@ EXPORT_API ail_error_e ail_get_appinfo(const char *appid, ail_appinfo_h *ai)
        *ai = appinfo_create();
        retv_if(!*ai, AIL_ERROR_OUT_OF_MEMORY);
 
-       snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), appid);
+       filter = sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR);
+       if (filter == NULL) {
+               appinfo_destroy(*ai);
+               return AIL_ERROR_FAIL;
+       }
 
-       snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
+       snprintf(w, sizeof(w), filter, appid);
+       snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",
+                                       SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
 
        do {
                ret = db_open(DB_OPEN_RO, GLOBAL_USER);
-               if (ret < 0) break;
-//is_admin
-               ret = db_prepare_globalro(query, &stmt);
-               if (ret < 0) break;
-//             ret = db_prepare(query, &stmt);
-//             if (ret < 0) break;
+               if (ret < 0)
+                       break;
 
+               /* is_admin */
+               ret = db_prepare_globalro(query, &stmt);
+               if (ret < 0)
+                       break;
 
                ret = db_step(stmt);
                if (ret < 0) {
@@ -357,11 +361,13 @@ EXPORT_API ail_error_e ail_get_appinfo(const char *appid, ail_appinfo_h *ai)
                }
 
                ret = db_finalize((*ai)->stmt);
-               if (ret < 0) break;
+               if (ret < 0)
+                       break;
+
                (*ai)->stmt = NULL;
 
                return AIL_ERROR_OK;
-       } while(0);
+       } while (0);
 
        appinfo_destroy(*ai);
 
@@ -374,6 +380,7 @@ EXPORT_API ail_error_e ail_get_usr_appinfo(const char *appid, uid_t uid, ail_app
        char query[AIL_SQL_QUERY_MAX_LEN];
        sqlite3_stmt *stmt = NULL;
        char w[AIL_SQL_QUERY_MAX_LEN];
+       const char *filter;
 
        retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
        retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
@@ -381,19 +388,25 @@ EXPORT_API ail_error_e ail_get_usr_appinfo(const char *appid, uid_t uid, ail_app
        *ai = appinfo_create();
        retv_if(!*ai, AIL_ERROR_OUT_OF_MEMORY);
 
-       snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), appid);
+       filter = sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR);
+       if (filter == NULL) {
+               appinfo_destroy(*ai);
+               return AIL_ERROR_FAIL;
+       }
 
-       snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
+       snprintf(w, sizeof(w), filter, appid);
+       snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",
+                                       SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
 
        do {
                ret = db_open(DB_OPEN_RO, uid);
-               if (ret < 0) break;
-//is_admin
-               ret = db_prepare(query, &stmt);
-               if (ret < 0) break;
-//             ret = db_prepare(query, &stmt);
-//             if (ret < 0) break;
+               if (ret < 0)
+                       break;
 
+               /* is_admin */
+               ret = db_prepare(query, &stmt);
+               if (ret < 0)
+                       break;
 
                ret = db_step(stmt);
                if (ret < 0) {
@@ -416,16 +429,19 @@ EXPORT_API ail_error_e ail_get_usr_appinfo(const char *appid, uid_t uid, ail_app
                }
 
                ret = db_finalize((*ai)->stmt);
-               if (ret < 0) break;
+               if (ret < 0)
+                       break;
+
                (*ai)->stmt = NULL;
 
                return AIL_ERROR_OK;
-       } while(0);
+       } while (0);
 
        appinfo_destroy(*ai);
 
        return ret;
 }
+
 EXPORT_API ail_error_e ail_appinfo_get_bool(const ail_appinfo_h ai, const char *property, bool *value)
 {
        ail_prop_bool_e prop;
@@ -439,7 +455,7 @@ EXPORT_API ail_error_e ail_appinfo_get_bool(const ail_appinfo_h ai, const char *
 
        if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
                return AIL_ERROR_INVALID_PARAMETER;
-       
+
        if (ai->stmt) {
                int index;
                index = sql_get_app_info_idx(prop);
@@ -466,7 +482,7 @@ EXPORT_API ail_error_e ail_appinfo_get_int(const ail_appinfo_h ai, const char *p
                return AIL_ERROR_INVALID_PARAMETER;
 
        if (ai->stmt) {
-               int index; 
+               int index;
                index = sql_get_app_info_idx(prop);
                if (db_column_int(ai->stmt, index, value) < 0)
                        return AIL_ERROR_DB_FAILED;
index efa2f66..f46c133 100755 (executable)
@@ -138,7 +138,6 @@ typedef enum {
        E_AIL_PROP_STR_MAX = E_AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR,
 } ail_prop_str_e;
 
-
 /**
  * @brief integer type properties
  */
index fe2c024..8304a26 100644 (file)
 #include "ail.h"
 #include "ail_private.h"
 
-
 static void usage(const char *name)
 {
        fprintf(stderr, "\n");
-       fprintf(stderr, "  Usage: %s\n", name);
+       fprintf(stderr, "Usage: %s\n", name);
        fprintf(stderr, "\n");
 }
 
-
-ail_cb_ret_e appinfo_list_appid_namefunc(const ail_appinfo_h appinfo,  void *user_data)
+ail_cb_ret_e appinfo_list_appid_namefunc(const ail_appinfo_h appinfo, void *user_data)
 {
-       char* package_str_name = NULL;
-    char* package_str_appid = NULL;
-    char* package_str_x_slp_exe = NULL;
+       char *package_str_name = NULL;
+       char *package_str_appid = NULL;
+       char *package_str_x_slp_exe = NULL;
+
        ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_APPID_STR, &package_str_appid);
        ail_appinfo_get_str(appinfo, AIL_PROP_NAME_STR, &package_str_name);
        ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_EXE_PATH, &package_str_x_slp_exe);
-               
+
        printf("'%s' '%s' '%s'\n",package_str_appid, package_str_name, package_str_x_slp_exe);
-       return AIL_CB_RET_CONTINUE;
-}
 
+       free(package_str_appid);
+       free(package_str_name);
+       free(package_str_x_slp_exe);
 
+       return AIL_CB_RET_CONTINUE;
+}
 
 int main(int argc, char *argv[])
 {
        int o;
        bool err;
-       if(getuid() == 0) {
+
+       if (getuid() == 0) {
                printf("Please use it as non root user\n");
                return;
-       } 
-       
+       }
+
        printf("Application List for user %lu\n", (long)getuid());
        printf("User's Application \n");
        printf("APPID    NAME   EXEPATH \n");
+
        ail_filter_list_usr_appinfo_foreach(NULL, appinfo_list_appid_namefunc, NULL, getuid());
+
        printf("Global's / Common Applications \n");
        printf("APPID    NAME   EXEPATH \n");
+
        ail_filter_list_appinfo_foreach(NULL, appinfo_list_appid_namefunc, NULL);
+
        printf("=================================================\n");
+
        return 0;
 }
index a73aadd..fd3a490 100644 (file)
@@ -108,20 +108,19 @@ static ail_error_e _get_appinfo(const char *package, const char *property, uid_t
        ail_error_e ret;
        int prop, ival;
        bool bval;
-       char *str;
+       char *str = NULL;;
        struct element e;
        struct element *p;
        int t;
-    //__isadmin)
+
+       /* __is admin */
        ret = ail_package_get_appinfo(package, &handle);
-       if (ret != AIL_ERROR_OK) {
+       if (ret != AIL_ERROR_OK)
                return AIL_ERROR_FAIL;
-       }
 
        prop = _get_property(property);
-       if (prop < 0) {
+       if (prop < 0)
                goto END;
-       }
 
        e.prop = prop;
        p = &e;
@@ -129,29 +128,30 @@ static ail_error_e _get_appinfo(const char *package, const char *property, uid_t
 
        if (t == VAL_TYPE_STR) {
                ret = ail_appinfo_get_str(handle, property, &str);
-               if (ret != AIL_ERROR_OK) {
+               if (ret != AIL_ERROR_OK)
                        goto END;
-               }
+
                fprintf(stderr, "Package[%s], Property[%s] : %s\n", package, property, str);
        } else if (t == VAL_TYPE_INT) {
                ret = ail_appinfo_get_int(handle, property, &ival);
-               if (ret != AIL_ERROR_OK) {
+               if (ret != AIL_ERROR_OK)
                        goto END;
-               }
+
                fprintf(stderr, "Package[%s], Property[%s] : %d\n", package, property, ival);
        } else if (t == VAL_TYPE_BOOL) {
                ret = ail_appinfo_get_bool(handle, property, &bval);
-               if (ret != AIL_ERROR_OK) {
+               if (ret != AIL_ERROR_OK)
                        goto END;
-               }
+
                fprintf(stderr, "Package[%s], Property[%s] : %d\n", package, property, bval);
        }
 
 END:
+       free(str);
+
        ret = ail_package_destroy_appinfo(handle);
-       if (ret != AIL_ERROR_OK) {
+       if (ret != AIL_ERROR_OK)
                return AIL_ERROR_FAIL;
-       }
 
        return AIL_ERROR_OK;
 }
index 66c936a..daa06c7 100644 (file)
@@ -21,8 +21,6 @@
  *
  */
 
-
-
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include "ail_db.h"
 #include "ail_private.h"
 
-
 #ifdef _E
 #undef _E
 #endif
+
 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
 
 #ifdef _D
 #undef _D
 #endif
+
 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
 
 #define SET_DEFAULT_LABEL(x) \
-       if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
-       else _D("chsmack -a \"*\" %s", x)
-
-
-static int __is_authorized()
+       do { \
+               if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
+                       _E("failed chsmack -a \"*\" %s", x); \
+               else \
+                       _D("chsmack -a \"*\" %s", x); \
+       } while (0)
+
+static int __is_authorized(void)
 {
        /* ail_init db should be called by an user. */
-
        uid_t uid = getuid();
-       if ((uid_t) OWNER_ROOT != uid)
+       if ((uid_t)OWNER_ROOT != uid)
                return 1;
        else
                return 0;
 }
 
-
 int main(int argc, char *argv[])
 {
        int ret;
+       char *db;
 
        if (!__is_authorized()) {
                fprintf(stderr, "You are not an authorized user!\n");
-               _D("You are root user! Please switch to a regular user\n");
+               _E("You are root user! Please switch to a regular user");
                return -1;
        }
-       else {
-               if(remove(ail_get_app_DB(getuid())))
-                       _E(" %s is not removed", ail_get_app_DB(getuid()));
-               if(remove(ail_get_app_DB_journal(getuid())))
-                       _E(" %s is not removed", ail_get_app_DB_journal(getuid()));
+
+       db = ail_get_app_DB(getuid());
+       if (db) {
+               if (remove(db))
+                       _E("%s is not removed", db);
+
+               free(db);
+       }
+
+       db = ail_get_app_DB_journal(getuid());
+       if (db) {
+               if (remove(db))
+                       _E("%s is not removed", db);
+
+               free(db);
        }
 
        ret = setenv("AIL_INITDB", "1", 1);
        _D("AIL_INITDB : %d", ret);
+
        if (db_open(DB_OPEN_RW, getuid()) != AIL_ERROR_OK) {
                _E("Fail to create system databases");
                return AIL_ERROR_DB_FAILED;
@@ -89,7 +101,3 @@ int main(int argc, char *argv[])
 
        return AIL_ERROR_OK;
 }
-
-
-
-// END
index b00baf7..4aae135 100755 (executable)
@@ -34,7 +34,6 @@
 #include "ail_db.h"
 #include "ail_private.h"
 
-
 #ifdef _E
 #undef _E
 #endif
@@ -157,45 +156,59 @@ int initdb_user_load_directory(const char *directory)
 static int __is_authorized()
 {
        /* ail_init db should be called by an user. */
-
        uid_t uid = getuid();
-       if ((uid_t) OWNER_ROOT != uid)
+       if ((uid_t)OWNER_ROOT != uid)
                return 1;
        else
                return 0;
 }
 
-
 int main(int argc, char *argv[])
 {
        int ret;
+       char *db;
+       char *path;
 
        if (!__is_authorized()) {
                fprintf(stderr, "You are not an authorized user!\n");
                _D("You are root user! Please switch to a regular user\n");
                return -1;
        }
-       else {
-               if(remove(ail_get_app_DB(getuid())))
-                       _E(" %s is not removed", ail_get_app_DB(getuid()));
-               if(remove(ail_get_app_DB_journal(getuid())))
-                       _E(" %s is not removed", ail_get_app_DB_journal(getuid()));
+
+       db = ail_get_app_DB(getuid());
+       if (db) {
+               if (remove(db))
+                       _E(" %s is not removed", db);
+
+               free(db);
        }
+
+       db = ail_get_app_DB_journal(getuid());
+       if (db) {
+               if (remove(db))
+                       _E(" %s is not removed", db);
+
+               free(db);
+       }
+
        ret = setenv("AIL_INITDB", "1", 1);
        _D("AIL_INITDB : %d", ret);
+
        ret = initdb_user_count_app();
-       if (ret > 0) {
+       if (ret > 0)
                _D("Some Apps in the App Info DB.");
+
+       path = ail_get_desktop_path(getuid());
+       if (path == NULL) {
+               _E("Failed to get desktop path");
+               return -1;
        }
 
-       ret = initdb_user_load_directory(ail_get_desktop_path(getuid()));
-       if (ret == AIL_ERROR_FAIL) {
+       ret = initdb_user_load_directory(path);
+       if (ret == AIL_ERROR_FAIL)
                _E("cannot load usr desktop directory.");
-       }
+
+       free(path);
 
        return AIL_ERROR_OK;
 }
-
-
-
-// END
index 8a346a2..23efdc8 100644 (file)
@@ -21,8 +21,6 @@
  *
  */
 
-
-
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -36,7 +34,6 @@
 #include "ail_db.h"
 #include "ail_private.h"
 
-
 #ifdef _E
 #undef _E
 #endif
@@ -78,11 +75,10 @@ static int syncdb_user_count_app(void)
        return total;
 }
 
-
-
 char* _desktop_to_package(const char* desktop)
 {
-       char *package, *tmp;
+       char *package;
+       char *tmp;
 
        retv_if(!desktop, NULL);
 
@@ -118,7 +114,7 @@ int syncdb_user_load_directory(const char *directory)
        int total_cnt = 0;
        int ok_cnt = 0;
 
-       // desktop file
+       /*  desktop file */
        dir = opendir(directory);
        if (!dir) {
                if (strerror_r(errno, buf, sizeof(buf)) == 0)
@@ -159,7 +155,6 @@ int syncdb_user_load_directory(const char *directory)
 static int __is_authorized()
 {
        /* ail_init db should be called by an user. */
-
        uid_t uid = getuid();
        if ((uid_t) OWNER_ROOT != uid)
                return 1;
@@ -167,35 +162,51 @@ static int __is_authorized()
                return 0;
 }
 
-
 int main(int argc, char *argv[])
 {
        int ret;
+       char *db;
+       char *path;
 
        if (!__is_authorized()) {
                fprintf(stderr, "You are not an authorized user!\n");
                _D("You are root user! Please switch to a regular user\n");
                return -1;
        }
-       if (access(ail_get_app_DB(getuid()), F_OK)) {
+
+       db = ail_get_app_DB(getuid());
+       if (db == NULL) {
+               fprintf(stderr, "Failed to get DB");
+               _E("Failed to get DB");
+               return -1;
+       }
+
+       if (access(db, F_OK)) {
                fprintf(stderr, "Application database is missing, please use ail_createdb to create one before\n");
+               free(db);
                return AIL_ERROR_FAIL;
        }
+
        ret = setenv("AIL_INITDB", "1", 1);
        _D("AIL_INITDB : %d", ret);
+
        ret = syncdb_user_count_app();
-       if (ret > 0) {
+       if (ret > 0)
                _D("Some Apps in the App Info DB.");
+
+       path = ail_get_desktop_path(getuid());
+       if (path == NULL) {
+               _E("Failed to get desktop path");
+               free(db);
+               return AIL_ERROR_FAIL;
        }
 
-       ret = syncdb_user_load_directory(ail_get_desktop_path(getuid()));
-       if (ret == AIL_ERROR_FAIL) {
+       ret = syncdb_user_load_directory(path);
+       if (ret == AIL_ERROR_FAIL)
                _E("cannot load usr desktop directory.");
-       }
+
+       free(path);
+       free(db);
 
        return AIL_ERROR_OK;
 }
-
-
-
-// END