From ecb49ed5ea8c2e257aff6d5d8f76cec97033e043 Mon Sep 17 00:00:00 2001 From: Priyesh Padmavilasom Date: Thu, 31 Mar 2016 01:10:31 +0000 Subject: [PATCH] fix code scan reported errors - dead code, unreachable code, unnecessary free etc --- client/api.c | 18 +++++----- client/clean.c | 2 +- client/client.c | 14 ++++++-- client/config.c | 5 --- client/defines.h | 78 ++++++++++++++++++++++++++----------------- client/goal.c | 4 --- client/packageutils.c | 20 ----------- client/remoterepo.c | 1 + client/repoutils.c | 19 +++++++---- client/updateinfo.c | 5 +++ tools/cli/defines.h | 29 ++++++++++------ 11 files changed, 106 insertions(+), 89 deletions(-) diff --git a/client/api.c b/client/api.c index 47fe0c55..5620d4d9 100644 --- a/client/api.c +++ b/client/api.c @@ -101,9 +101,6 @@ TDNFCheckLocalPackages( pszRPMPath = g_build_filename(pszLocalPath, pszFile, NULL); hPkg = hy_sack_add_cmdline_package(hSack, pszRPMPath); - g_free(pszRPMPath); - pszRPMPath = NULL; - if(!hPkg) { dwError = ERROR_TDNF_INVALID_PARAMETER; @@ -111,6 +108,9 @@ TDNFCheckLocalPackages( } hy_packagelist_push(hPkgList, hPkg); hPkg = NULL; + + g_free(pszRPMPath); + pszRPMPath = NULL; } fprintf(stdout, "Found %d packages\n", hy_packagelist_count(hPkgList)); @@ -1023,13 +1023,13 @@ TDNFFreeCmdArgs( TDNF_SAFE_FREE_MEMORY(pCmdArgs->ppszCmds[nIndex]); } TDNF_SAFE_FREE_MEMORY(pCmdArgs->ppszCmds); - } - TDNF_SAFE_FREE_MEMORY(pCmdArgs->pszInstallRoot); - TDNF_SAFE_FREE_MEMORY(pCmdArgs->pszConfFile); - TDNF_SAFE_FREE_MEMORY(pCmdArgs->pszReleaseVer); + TDNF_SAFE_FREE_MEMORY(pCmdArgs->pszInstallRoot); + TDNF_SAFE_FREE_MEMORY(pCmdArgs->pszConfFile); + TDNF_SAFE_FREE_MEMORY(pCmdArgs->pszReleaseVer); - TDNF_SAFE_FREE_MEMORY(pCmdArgs->pSetOpt); - TDNF_SAFE_FREE_MEMORY(pCmdArgs); + TDNF_SAFE_FREE_MEMORY(pCmdArgs->pSetOpt); + TDNF_SAFE_FREE_MEMORY(pCmdArgs); + } } const char* diff --git a/client/clean.c b/client/clean.c index 091aec32..9d13c3f2 100644 --- a/client/clean.c +++ b/client/clean.c @@ -76,7 +76,7 @@ TDNFCopyEnabledRepos( return dwError; error: - if(!pppszReposUsed) + if(pppszReposUsed) { *pppszReposUsed = NULL; } diff --git a/client/client.c b/client/client.c index c748cb8f..8c8b2b7d 100644 --- a/client/client.c +++ b/client/client.c @@ -83,6 +83,8 @@ TDNFApplyPackageFilter( ) { uint32_t dwError = 0; + int nCmpType = HY_GLOB; + if(!hQuery || !ppszPackageNameSpecs) { dwError = ERROR_TDNF_INVALID_PARAMETER; @@ -92,12 +94,20 @@ TDNFApplyPackageFilter( { if(TDNFIsGlob(*ppszPackageNameSpecs)) { - hy_query_filter(hQuery, HY_PKG_NAME, HY_GLOB, *ppszPackageNameSpecs); + nCmpType = HY_GLOB; } else { - hy_query_filter(hQuery, HY_PKG_NAME, HY_EQ, *ppszPackageNameSpecs); + nCmpType = HY_EQ; } + + dwError = hy_query_filter( + hQuery, + HY_PKG_NAME, + nCmpType, + *ppszPackageNameSpecs); + BAIL_ON_TDNF_HAWKEY_ERROR(dwError); + ++ppszPackageNameSpecs; } diff --git a/client/config.c b/client/config.c index 7c055ff0..6cabb8c6 100644 --- a/client/config.c +++ b/client/config.c @@ -43,7 +43,6 @@ TDNFReadConfig( uint32_t dwError = 0; GKeyFile* pKeyFile = NULL; - char* pszValue = NULL; PTDNF_CONF pConf = NULL; @@ -174,10 +173,6 @@ TDNFReadConfig( { g_key_file_free(pKeyFile); } - if(pszValue) - { - g_free(pszValue); - } return dwError; error: diff --git a/client/defines.h b/client/defines.h index 82071445..e2d6021b 100644 --- a/client/defines.h +++ b/client/defines.h @@ -37,51 +37,67 @@ typedef enum #define IsNullOrEmptyString(str) (!(str) || !(*str)) #define BAIL_ON_TDNF_ERROR(dwError) \ - if (dwError) \ - { \ - goto error; \ - } + do { \ + if (dwError) \ + { \ + goto error; \ + } \ + } while(0) #define BAIL_ON_TDNF_SYSTEM_ERROR(dwError) \ - if (dwError) \ - { \ - dwError = ERROR_TDNF_SYSTEM_BASE + dwError; \ - goto error; \ - } + do { \ + if (dwError) \ + { \ + dwError = ERROR_TDNF_SYSTEM_BASE + dwError; \ + goto error; \ + } \ + } while(0) #define BAIL_ON_TDNF_HAWKEY_ERROR(dwError) \ - if (dwError) \ - { \ - dwError = ERROR_TDNF_HAWKEY_BASE + dwError; \ - goto error; \ - } + do { \ + if (dwError) \ + { \ + dwError = ERROR_TDNF_HAWKEY_BASE + dwError; \ + goto error; \ + } \ + } while(0) #define BAIL_ON_TDNF_RPM_ERROR(dwError) \ - if (dwError) \ - { \ - dwError = ERROR_TDNF_RPM_BASE + dwError; \ - goto error; \ - } + do { \ + if (dwError) \ + { \ + dwError = ERROR_TDNF_RPM_BASE + dwError; \ + goto error; \ + } \ + } while(0) #define TDNF_SAFE_FREE_MEMORY(pMemory) \ - if (pMemory) { \ - TDNFFreeMemory(pMemory); \ - } + do { \ + if (pMemory) { \ + TDNFFreeMemory(pMemory); \ + } \ + } while(0) #define TDNF_SAFE_FREE_PKGLIST(hPkgList) \ - if (hPkgList) { \ - hy_packagelist_free(hPkgList); \ - } + do { \ + if (hPkgList) { \ + hy_packagelist_free(hPkgList); \ + } \ + } while(0) #define TDNF_SAFE_FREE_STRINGARRAY(ppArray) \ - if (ppArray) { \ - TDNFFreeStringArray(ppArray); \ - } + do { \ + if (ppArray) { \ + TDNFFreeStringArray(ppArray); \ + } \ + } while(0) #define TDNF_SAFE_FREE_PKGINFO(pPkgInfo) \ - if (pPkgInfo) { \ - TDNFFreePackageInfo(pPkgInfo); \ - } + do { \ + if (pPkgInfo) { \ + TDNFFreePackageInfo(pPkgInfo); \ + } \ + } while(0) //Misc #define TDNF_RPM_EXT ".rpm" diff --git a/client/goal.c b/client/goal.c index 662535b2..5c004389 100644 --- a/client/goal.c +++ b/client/goal.c @@ -331,10 +331,6 @@ TDNFGoalReportProblems( } } cleanup: - if(pszProblem) - { - hy_free(pszProblem); - } return dwError; error: diff --git a/client/packageutils.c b/client/packageutils.c index 70b6256f..67080f0f 100644 --- a/client/packageutils.c +++ b/client/packageutils.c @@ -118,10 +118,6 @@ TDNFFindAvailablePkgByPkg( { *phPkg = NULL; } - if(hPkg) - { - hy_package_free(hPkg); - } goto cleanup; } @@ -205,10 +201,6 @@ TDNFFindInstalledPkgByName( { *phPkg = NULL; } - if(hPkg) - { - hy_package_free(hPkg); - } goto cleanup; } @@ -258,10 +250,6 @@ TDNFGetInstalled( { *phPkgList = NULL; } - if(hPkgList) - { - hy_packagelist_free(hPkgList); - } if(hQuery) { hy_query_free(hQuery); @@ -315,10 +303,6 @@ TDNFMatchForReinstall( { *phPkgList = NULL; } - if(hPkgList) - { - hy_packagelist_free(hPkgList); - } if(hPkgAvailable) { hy_package_free(hPkgAvailable); @@ -879,10 +863,6 @@ TDNFFilterPackages( { *phPkgList = NULL; } - if(hPkgList) - { - hy_packagelist_free(hPkgList); - } goto cleanup; } diff --git a/client/remoterepo.c b/client/remoterepo.c index cf06feaa..46c7980e 100644 --- a/client/remoterepo.c +++ b/client/remoterepo.c @@ -134,6 +134,7 @@ TDNFDownloadPackage( fprintf(stdout, "\n"); cleanup: TDNF_SAFE_FREE_MEMORY(pszBaseUrl); + TDNF_SAFE_FREE_MEMORY(pszUserPass); if(pRepoHandle) { lr_handle_free(pRepoHandle); diff --git a/client/repoutils.c b/client/repoutils.c index 2f8e6538..a1877b53 100644 --- a/client/repoutils.c +++ b/client/repoutils.c @@ -205,7 +205,7 @@ TDNFRepoRemoveCache( char* pszFilePath = NULL; GDir* pDir = NULL; - if(!pTdnf || IsNullOrEmptyString(pszRepoId)) + if(!pTdnf || !pTdnf->pConf || IsNullOrEmptyString(pszRepoId)) { dwError = ERROR_TDNF_INVALID_PARAMETER; BAIL_ON_TDNF_ERROR(dwError); @@ -233,17 +233,22 @@ TDNFRepoRemoveCache( while ((pszFile = g_dir_read_name (pDir)) != NULL) { pszFilePath = g_build_filename(pszRepoCacheDir, pszFile, NULL); - - if(unlink(pszFilePath)) - { - dwError = errno; - BAIL_ON_TDNF_SYSTEM_ERROR(dwError); - } if(pszFilePath) { + if(unlink(pszFilePath)) + { + dwError = errno; + BAIL_ON_TDNF_SYSTEM_ERROR(dwError); + } + g_free(pszFilePath); pszFilePath = NULL; } + else + { + dwError = ERROR_TDNF_INVALID_PARAMETER; + BAIL_ON_TDNF_ERROR(dwError); + } } if(rmdir(pszRepoCacheDir)) { diff --git a/client/updateinfo.c b/client/updateinfo.c index f6042775..8da1eefa 100644 --- a/client/updateinfo.c +++ b/client/updateinfo.c @@ -141,6 +141,7 @@ TDNFFreeUpdateInfo( TDNFFreeUpdateInfoReferences(pUpdateInfo->pReferences); TDNFFreeUpdateInfoPackages(pUpdateInfo->pPackages); + TDNF_SAFE_FREE_MEMORY(pUpdateInfo); } } @@ -255,6 +256,10 @@ TDNFGetUpdateInfoPackages( { *ppPkgs = NULL; } + if(pPkg) + { + TDNFFreeUpdateInfoPackages(pPkg); + } if(pPkgs) { TDNFFreeUpdateInfoPackages(pPkgs); diff --git a/tools/cli/defines.h b/tools/cli/defines.h index 6b614527..13ecc227 100644 --- a/tools/cli/defines.h +++ b/tools/cli/defines.h @@ -38,20 +38,29 @@ #define IsNullOrEmptyString(str) (!(str) || !(*str)) #define BAIL_ON_CLI_ERROR(unError) \ - if (unError) \ - { \ - goto error; \ - } + do { \ + if (unError) \ + { \ + goto error; \ + } \ + } \ + while(0) #define TDNF_CLI_SAFE_FREE_MEMORY(pMemory) \ - if (pMemory) { \ - TDNFFreeMemory(pMemory); \ - } + do { \ + if (pMemory) { \ + TDNFFreeMemory(pMemory); \ + } \ + } \ + while(0) #define TDNF_CLI_SAFE_FREE_STRINGARRAY(ppArray) \ - if (ppArray) { \ - TDNFFreeStringArray(ppArray); \ - } + do { \ + if (ppArray) { \ + TDNFFreeStringArray(ppArray); \ + } \ + } \ + while(0) typedef uint32_t (*PFN_CMD)(PTDNF, PTDNF_CMD_ARGS);