From e914edce4c6067470fc19d828c10930fbef61c6c Mon Sep 17 00:00:00 2001 From: Sungbae Yoo Date: Wed, 17 Aug 2016 21:21:37 +0900 Subject: [PATCH] Add error handlings when *alloc returns NULL Signed-off-by: Sungbae Yoo Change-Id: I1a820dc052f447fe5ad41dd81230f6cdd9801e49 --- lib/krate/app-proxy.cpp | 39 +++++++++++++++++++++++----------- lib/krate/package-proxy.cpp | 50 +++++++++++++++++++++++++++++++------------- tools/apps/kaskit/src/main.c | 4 ++++ volume/main.cpp | 16 +++++++++----- 4 files changed, 78 insertions(+), 31 deletions(-) diff --git a/lib/krate/app-proxy.cpp b/lib/krate/app-proxy.cpp index 449520d..137cc50 100644 --- a/lib/krate/app-proxy.cpp +++ b/lib/krate/app-proxy.cpp @@ -43,35 +43,50 @@ static app_info_h make_app_info_handle(const Krate::AppProxy::AppInfo& info) return NULL; } - application_x* app = (application_x*)::malloc(sizeof(application_x)); - pkgmgr_appinfo_x* pkgappinfo = (pkgmgr_appinfo_x*)::malloc(sizeof(pkgmgr_appinfo_x)); - app_info_s* appinfo = (app_info_s*)::malloc(sizeof(struct app_info_s)); + application_x* app = (application_x*)::calloc(1, sizeof(application_x)); + if (app == NULL) { + return NULL; + } + + pkgmgr_appinfo_x* pkgappinfo = (pkgmgr_appinfo_x*)::calloc(1, sizeof(pkgmgr_appinfo_x)); + if (pkgappinfo == NULL) { + free(app); + return NULL; + } + + app_info_s* appinfo = (app_info_s*)::calloc(1, sizeof(struct app_info_s)); + if (appinfo == NULL) { + free(app); + free(pkgappinfo); + return NULL; + } - ::memset(appinfo, 0, sizeof(app_info_s)); appinfo->app_id = ::strdup(info.id.c_str()); appinfo->pkg_app_info = pkgappinfo; - ::memset(pkgappinfo, 0, sizeof(pkgmgr_appinfo_x)); pkgappinfo->package = ::strdup(info.package.c_str()); pkgappinfo->locale = ::strdup(info.locale.c_str()); pkgappinfo->app_component = info.componentType; pkgappinfo->app_info = app; - ::memset(app, 0, sizeof(application_x)); app->appid = ::strdup(info.id.c_str()); app->nodisplay = ::strdup(info.isNoDisplayed? "true":"false"); app->taskmanage = ::strdup(info.isTaskManaged? "true":"false"); app->type = ::strdup(info.type.c_str()); icon_x* icon = (icon_x*)::calloc(1, sizeof(icon_x)); - icon->text = ::strdup(info.icon.c_str()); - icon->lang = ::strdup(info.locale.c_str()); - app->icon = ::g_list_append(NULL, icon); + if (icon != NULL) { + icon->text = ::strdup(info.icon.c_str()); + icon->lang = ::strdup(info.locale.c_str()); + app->icon = ::g_list_append(NULL, icon); + } label_x* label = (label_x*)::calloc(1, sizeof(label_x)); - label->text = ::strdup(info.label.c_str()); - label->lang = ::strdup(info.locale.c_str()); - app->label = ::g_list_append(NULL, label); + if (label != NULL) { + label->text = ::strdup(info.label.c_str()); + label->lang = ::strdup(info.locale.c_str()); + app->label = ::g_list_append(NULL, label); + } return reinterpret_cast(appinfo); } diff --git a/lib/krate/package-proxy.cpp b/lib/krate/package-proxy.cpp index 690397e..a78db06 100644 --- a/lib/krate/package-proxy.cpp +++ b/lib/krate/package-proxy.cpp @@ -125,8 +125,22 @@ static package_info_h make_package_info_handle(const PackageProxy::PackageInfo& } package_x* package = (package_x*)::calloc(1, sizeof(package_x)); + if (package == NULL) { + return NULL; + } + pkgmgr_pkginfo_x* pkginfo = (pkgmgr_pkginfo_x*)::calloc(1, sizeof(pkgmgr_pkginfo_x)); + if (pkginfo == NULL) { + free(package); + return NULL; + } + package_info_s* packageinfo = (package_info_s*)::calloc(1, sizeof(package_info_s)); + if (packageinfo == NULL) { + free(package); + free(pkginfo); + return NULL; + } packageinfo->package = ::strdup(info.id.c_str()); packageinfo->pkgmgr_pkginfo = pkginfo; @@ -146,26 +160,34 @@ static package_info_h make_package_info_handle(const PackageProxy::PackageInfo& package->api_version = ::strdup(info.apiVersion.c_str()); icon_x* icon = (icon_x*)::calloc(1, sizeof(icon_x)); - icon->text = ::strdup(info.icon.c_str()); - icon->lang = ::strdup(info.locale.c_str()); - package->icon = ::g_list_append(NULL, icon); + if (icon != NULL) { + icon->text = ::strdup(info.icon.c_str()); + icon->lang = ::strdup(info.locale.c_str()); + package->icon = ::g_list_append(NULL, icon); + } label_x* label = (label_x*)::calloc(1, sizeof(label_x)); - label->text = ::strdup(info.label.c_str()); - label->lang = ::strdup(info.locale.c_str()); - package->label = ::g_list_append(NULL, label); + if (label != NULL) { + label->text = ::strdup(info.label.c_str()); + label->lang = ::strdup(info.locale.c_str()); + package->label = ::g_list_append(NULL, label); + } description_x* desc = (description_x*)::calloc(1, sizeof(description_x)); - desc->text = ::strdup(info.description.c_str()); - desc->lang = ::strdup(info.locale.c_str()); - package->description = ::g_list_append(NULL, desc); + if (desc != NULL) { + desc->text = ::strdup(info.description.c_str()); + desc->lang = ::strdup(info.locale.c_str()); + package->description = ::g_list_append(NULL, desc); + } author_x* author = (author_x*)::calloc(1, sizeof(author_x)); - author->text = ::strdup(info.author.name.c_str()); - author->email = ::strdup(info.author.email.c_str()); - author->href = ::strdup(info.author.href.c_str()); - author->lang = ::strdup(info.locale.c_str()); - package->author = ::g_list_append(NULL, author); + if (author != NULL) { + author->text = ::strdup(info.author.name.c_str()); + author->email = ::strdup(info.author.email.c_str()); + author->href = ::strdup(info.author.href.c_str()); + author->lang = ::strdup(info.locale.c_str()); + package->author = ::g_list_append(NULL, author); + } return reinterpret_cast(packageinfo); } diff --git a/tools/apps/kaskit/src/main.c b/tools/apps/kaskit/src/main.c index 42ca637..172fb7a 100644 --- a/tools/apps/kaskit/src/main.c +++ b/tools/apps/kaskit/src/main.c @@ -161,6 +161,10 @@ static char* __get_current_krate_name() { char* ret, *buf = malloc(bufsize * sizeof(char)); + if (buf == NULL) { + return NULL; + } + getpwuid_r(getuid(), &pwd, buf, bufsize, &result); if (result == NULL) { ret = NULL; diff --git a/volume/main.cpp b/volume/main.cpp index 4de5260..d277009 100755 --- a/volume/main.cpp +++ b/volume/main.cpp @@ -62,7 +62,7 @@ ecryptfs_payload* generateToken(char* key) unsigned char keyBuffer[ECRYPTFS_MAX_KEY_SIZE+1]; - ecryptfs_payload* authToken = (ecryptfs_payload *)::malloc(sizeof(ecryptfs_payload)); + ecryptfs_payload* authToken = new ecryptfs_payload(); if (authToken == NULL) { return NULL; } @@ -112,16 +112,20 @@ int mountEcryptfs(const std::string& src, const std::string& keyName) "user", signature, 0); - if (rc == -1 && errno != ENOKEY) { - ERROR("Failed to find key"); - return -1; - } + if (rc == -1) { + if (errno != ENOKEY) { + ERROR("Failed to find key"); + delete authTok; + return -1; + } + rc = KernelKeyRing::add("user", signature, (void*)authTok, sizeof(ecryptfs_payload), KEY_SPEC_USER_KEYRING); + if (rc == -1) { ERROR("Failed to add key"); return -1; @@ -136,6 +140,8 @@ int mountEcryptfs(const std::string& src, const std::string& keyName) "smackfsroot=*,smackfsdef=*", ECRYPTFS_MAX_KEY_SIZE, signature); + delete authTok; + rc = ::mount(src.c_str(), src.c_str(), "ecryptfs", MS_NODEV, ecryptfsOpts); if (rc != 0) { ERROR(runtime::GetSystemErrorMessage()); -- 2.7.4