memory leak and missing error check issues fix 74/226174/1 accepted/tizen/unified/20200228.123704 submit/tizen/20200227.113842 submit/tizen/20200227.113923
authorsrinivasa.m <srinivasa.m@samsung.com>
Thu, 27 Feb 2020 09:17:10 +0000 (14:47 +0530)
committersrinivasa.m <srinivasa.m@samsung.com>
Thu, 27 Feb 2020 09:19:59 +0000 (14:49 +0530)
Change-Id: Ifa675fc9640f5a5ac3e48ad9a53b9ba9c3ac5811
Signed-off-by: srinivasa.m <srinivasa.m@samsung.com>
common/cryptoutil/src/AsmCrypto.cpp
common/uiutil/src/AcUiAdaptor.cpp [changed mode: 0755->0644]
common/uiutil/src/PinAuthUiAdaptor.cpp [changed mode: 0755->0644]
common/uiutil/src/TCUiAdaptor.cpp [changed mode: 0755->0644]
server/auth_discovery/src/BAuthStub.cpp [changed mode: 0755->0644]
server/auth_discovery/src/RAuthStub.cpp [changed mode: 0755->0644]
server/auth_discovery/src/RoamingUtil.cpp [changed mode: 0755->0644]
server/src/AsmStorage.cpp [changed mode: 0755->0644]

index 5281cbf..c8bb565 100644 (file)
@@ -183,18 +183,20 @@ AsmCrypto::getAsmToken(void)
                time_t t;
                srand((unsigned) time(&t));
                mac = (char*)calloc(128, sizeof(char));
-               snprintf(mac, 128 - 1, "%d", (AsmCrypto::genRandomInt()%1000 + 1));
-
-               char *macClone = _SAFE_DUP(mac);
-               if(macClone)
-               _INFO("%s", macClone);
-               std::string macStr(macClone);
-               _INFO("%s", macStr.c_str());
-               asmTok = strdup(macClone);
-               SAFE_DELETE(macClone);
-               SAFE_DELETE(mac);
-               bt_deinitialize();
-               return macStr;
+               if(mac != NULL  ) {
+                       snprintf(mac, 128 - 1, "%d", (AsmCrypto::genRandomInt()%1000 + 1));
+
+                       char *macClone = _SAFE_DUP(mac);
+                       std::string macStr(macClone);
+
+                       _INFO("%s", macClone);
+                       _INFO("%s", macStr.c_str());
+                       asmTok = strdup(macClone);
+                       SAFE_DELETE(macClone);
+                       SAFE_DELETE(mac);
+                       bt_deinitialize();
+                       return macStr;
+               }
        }
 
        char *realpath_res = NULL;
@@ -487,12 +489,14 @@ AsmCrypto::aesEncrypt(const unsigned char *plaintext, int plaintext_len, const u
        if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
        {
          _ERR("EVP_EncryptInit_ex() failed");
+         EVP_CIPHER_CTX_free(ctx);
          return -1;
        }
 
        if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
        {
          _ERR("EVP_EncryptUpdate() failed");
+         EVP_CIPHER_CTX_free(ctx);
          return -1;
        }
 
@@ -501,6 +505,7 @@ AsmCrypto::aesEncrypt(const unsigned char *plaintext, int plaintext_len, const u
        if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
        {
          _ERR("EVP_EncryptFinal_ex() failed");
+         EVP_CIPHER_CTX_free(ctx);
          return -1;
        }
 
@@ -532,12 +537,14 @@ AsmCrypto::aesDecrypt(const unsigned char *ciphertext, int ciphertext_len, const
        if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
        {
          _ERR("EVP_DecryptInit_ex() failed");
+         EVP_CIPHER_CTX_free(ctx);
          return -1;
        }
 
        if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
        {
          _ERR("EVP_DecryptUpdate() failed");
+         EVP_CIPHER_CTX_free(ctx);
          return -1;
        }
 
@@ -546,6 +553,7 @@ AsmCrypto::aesDecrypt(const unsigned char *ciphertext, int ciphertext_len, const
        if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
        {
          _ERR("EVP_DecryptFinal_ex() failed");
+         EVP_CIPHER_CTX_free(ctx);
          return -1;
        }
 
@@ -837,11 +845,16 @@ __b64_encode(unsigned char *input, int ip_len)
        RET_IF_FAIL(input != NULL, NULL);
        RET_IF_FAIL(ip_len > 0, NULL);
 
-       unsigned char *output = (unsigned char*)calloc(ip_len * 1.5, sizeof(char));
-
+       unsigned char *output = NULL;
        BIO *bmem = NULL;
        BIO *b64 = NULL;
        BUF_MEM *bptr = NULL;
+
+       output = (unsigned char*)calloc(ip_len * 1.5, sizeof(char));
+       if(output == NULL) {
+               _ERR("memory allocation failed");
+               return NULL;
+       }
        b64 = BIO_new(BIO_f_base64());
        if (b64 == NULL) {
                _ERR("BIO_new failed \n");
old mode 100755 (executable)
new mode 100644 (file)
index a1663f4..a79ab68
@@ -47,10 +47,15 @@ char *
 AcUiAdaptor::genNonce(void)
 {
        struct timeval t1;
+       char *nonce = NULL;
        gettimeofday(&t1, NULL);
        srand(t1.tv_usec * t1.tv_sec);
 
-       char *nonce = NALLOC(NONCE_LEN + 1, char);
+       nonce = NALLOC(NONCE_LEN + 1, char);
+       if(nonce == NULL) {
+               _ERR("memory allocation failed");
+               return NULL;
+       }
        for (int i = 0; i < NONCE_LEN; i++) {
                int randNum = AsmCrypto::genRandomInt() % ('z' - '0');
                nonce[i] = '0' + randNum;
old mode 100755 (executable)
new mode 100644 (file)
index 41a079c..7e8f17b
@@ -54,10 +54,15 @@ char *
 PinAuthUiAdaptor::genNonce(void)
 {
        struct timeval t1;
+       char *nonce = NULL;
        gettimeofday(&t1, NULL);
        srand(t1.tv_usec * t1.tv_sec);
 
-       char *nonce = NALLOC(NONCE_LEN + 1, char);
+       nonce = NALLOC(NONCE_LEN + 1, char);
+       if(nonce == NULL) {
+               _ERR("memory allocation failed");
+               return NULL;
+       }
        for (int i = 0; i < NONCE_LEN; i++) {
                int randNum = AsmCrypto::genRandomInt() % ('z' - '0');
                nonce[i] = '0' + randNum;
old mode 100755 (executable)
new mode 100644 (file)
index f557bdf..3bed736
@@ -46,10 +46,15 @@ char *
 TcUiAdaptor::genNonce(void)
 {
        struct timeval t1;
+       char * nonce = NULL;
        gettimeofday(&t1, NULL);
        srand(t1.tv_usec * t1.tv_sec);
 
-       char *nonce = NALLOC(NONCE_LEN + 1, char);
+       nonce = NALLOC(NONCE_LEN + 1, char);
+       if(nonce == NULL) {
+               _ERR("memory allocation failed");
+               return NULL;
+       }
        for (int i = 0; i < NONCE_LEN; i++) {
                int randNum = AsmCrypto::genRandomInt() % ('z' - '0');
                nonce[i] = '0' + randNum;
old mode 100755 (executable)
new mode 100644 (file)
index 1722b6f..31c6875
@@ -443,9 +443,18 @@ BAuthStub::hash(const std::string& message)
                _INFO("AuthenrStub::hash::digest generated is [%s]", digest);
 
                Buffer *hashInfo = ALLOC(Buffer);
+               if(hashInfo == NULL) {
+                       _ERR("memory allocation failed");
+                       return NULL;
+               }
                hashInfo->len = SHA256_DIGEST_LENGTH;
                hashInfo->data = NALLOC(BIG_SIZE, uint8_t);
                memcpy(hashInfo->data, digest, SHA256_DIGEST_LENGTH);
+               if(hashInfo->data == NULL) {
+                       _ERR("memory allocation failed");
+                       SAFE_DELETE(hashInfo);
+                       return NULL;
+               }
                //std::string digestStr((char*)digest, 5000);
                _END;
                return hashInfo;
@@ -459,8 +468,17 @@ BAuthStub::hash(const std::string& message)
 
        //std::string digestStr(digest, strlen(digest));
        Buffer *hashInfo = ALLOC(Buffer);
+       if(hashInfo == NULL) {
+               _ERR("memory allocation failed");
+               return NULL;
+       }
        hashInfo->len = SHA256_DIGEST_LENGTH;
        hashInfo->data = NALLOC(BIG_SIZE, uint8_t);
+       if(hashInfo->data == NULL) {
+               _ERR("memory allocation failed");
+               SAFE_DELETE(hashInfo);
+               return NULL;
+       }
        memcpy(hashInfo->data, digest, SHA256_DIGEST_LENGTH);
 
        _END;
old mode 100755 (executable)
new mode 100644 (file)
index f3f1e5c..9e7866c
@@ -254,8 +254,17 @@ RAuthStub::hash(const std::string& message)
        _INFO("AuthenrStub::hash::digest generated is [%s]", digest);
 
        Buffer *hashInfo = ALLOC(Buffer);
+       if(hashInfo == NULL) {
+               _ERR("memory allocation failed");
+               return NULL;
+       }
        hashInfo->len = SHA256_DIGEST_LENGTH;
        hashInfo->data = NALLOC(BIG_SIZE, uint8_t);
+       if(hashInfo->data == NULL) {
+               _ERR("memory allocation failed");
+               SAFE_DELETE(hashInfo);
+               return NULL;
+       }
        memcpy(hashInfo->data, digest, SHA256_DIGEST_LENGTH);
        //std::string digestStr((char*)digest, 5000);
        _END;
old mode 100755 (executable)
new mode 100644 (file)
index 80e49de..7f7495c
@@ -72,11 +72,16 @@ RoamingUtil::b64Encode(unsigned char *input, int ip_len)
        RET_IF_FAIL(input != NULL, NULL);
        RET_IF_FAIL(ip_len > 0, NULL);
 
-       unsigned char *output = (unsigned char*)calloc(ip_len * 1.5, sizeof(char));
-
+       unsigned char *output = NULL;
        BIO *bmem = NULL;
        BIO *b64 = NULL;
        BUF_MEM *bptr = NULL;
+
+       output = (unsigned char*)calloc(ip_len * 1.5, sizeof(char));
+       if(output == NULL) {
+               _ERR(" calloc failed \n");
+               return NULL;
+       }
        b64 = BIO_new(BIO_f_base64());
        if (b64 == NULL) {
                _ERR("BIO_new failed \n");
old mode 100755 (executable)
new mode 100644 (file)
index 7eef790..9fb0468
@@ -388,6 +388,7 @@ AsmStorage::createIfNotExists(const std::string& dbName)
        ret = sqlite3_exec(dbH, query, NULL, 0, &errMsg);
        if (errMsg != NULL) {
                _INFO("createtable error = [%s]", errMsg);
+               sqlite3_free(errMsg);
                sqlite3_close(dbH);
 
                SAFE_DELETE(dbPath);