OSDN Git Service

fix memory leak in unittest.
authorYunlian Jiang <yunlian@google.com>
Wed, 30 Aug 2017 16:50:05 +0000 (09:50 -0700)
committerYunlian Jiang <yunlian@google.com>
Wed, 30 Aug 2017 17:13:39 +0000 (10:13 -0700)
This fixes the warning
warning: Potential leak of memory pointed to by 'salt' [clang-analyzer-cplusplus.NewDeleteLeaks]

Bug: None
Test: The warning is gone.
Change-Id: I1a31188775c2a099949bf8b8e2613b71af5081cd

verity/build_verity_tree.cpp

index c50e449..19e4a2a 100644 (file)
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <vector>
 
 #include <android-base/file.h>
 
@@ -126,7 +127,7 @@ int main(int argc, char **argv)
 {
     char *data_filename;
     char *verity_filename;
-    unsigned char *salt = NULL;
+    std::vector<unsigned char> salt;
     size_t salt_size = 0;
     bool sparse = false;
     size_t block_size = 4096;
@@ -150,12 +151,8 @@ int main(int argc, char **argv)
 
         switch (c) {
         case 'a':
-            salt_size = strlen(optarg);
-            salt = new unsigned char[salt_size]();
-            if (salt == NULL) {
-                FATAL("failed to allocate memory for salt\n");
-            }
-            memcpy(salt, optarg, salt_size);
+            salt.clear();
+            salt.insert(salt.end(), optarg, &optarg[strlen(optarg)]);
             break;
         case 'A': {
                 BIGNUM *bn = NULL;
@@ -163,11 +160,8 @@ int main(int argc, char **argv)
                     FATAL("failed to convert salt from hex\n");
                 }
                 salt_size = BN_num_bytes(bn);
-                salt = new unsigned char[salt_size]();
-                if (salt == NULL) {
-                    FATAL("failed to allocate memory for salt\n");
-                }
-                if((size_t)BN_bn2bin(bn, salt) != salt_size) {
+                salt.resize(salt_size);
+                if((size_t)BN_bn2bin(bn, salt.data()) != salt_size) {
                     FATAL("failed to convert salt to bytes\n");
                 }
             }
@@ -214,19 +208,16 @@ int main(int argc, char **argv)
     size_t hash_size = EVP_MD_size(md);
     assert(hash_size * 2 < block_size);
 
-    if (!salt || !salt_size) {
+    if (salt.data() || !salt_size) {
         salt_size = hash_size;
-        salt = new unsigned char[salt_size];
-        if (salt == NULL) {
-            FATAL("failed to allocate memory for salt\n");
-        }
+        salt.resize(salt_size);
 
         int random_fd = open("/dev/urandom", O_RDONLY);
         if (random_fd < 0) {
             FATAL("failed to open /dev/urandom\n");
         }
 
-        ssize_t ret = read(random_fd, salt, salt_size);
+        ssize_t ret = read(random_fd, salt.data(), salt_size);
         if (ret != (ssize_t)salt_size) {
             FATAL("failed to read %zu bytes from /dev/urandom: %zd %d\n", salt_size, ret, errno);
         }
@@ -310,14 +301,14 @@ int main(int argc, char **argv)
     unsigned char zero_block_hash[hash_size];
     unsigned char zero_block[block_size];
     memset(zero_block, 0, block_size);
-    hash_block(md, zero_block, block_size, salt, salt_size, zero_block_hash, NULL);
+    hash_block(md, zero_block, block_size, salt.data(), salt_size, zero_block_hash, NULL);
 
     unsigned char root_hash[hash_size];
     verity_tree_levels[levels] = root_hash;
 
     struct sparse_hash_ctx ctx;
     ctx.hashes = verity_tree_levels[0];
-    ctx.salt = salt;
+    ctx.salt = salt.data();
     ctx.salt_size = salt_size;
     ctx.hash_size = hash_size;
     ctx.block_size = block_size;
@@ -334,7 +325,7 @@ int main(int argc, char **argv)
         hash_blocks(md,
                 verity_tree_levels[i], verity_tree_level_blocks[i] * block_size,
                 verity_tree_levels[i + 1], &out_size,
-                salt, salt_size, block_size);
+                salt.data(), salt_size, block_size);
           if (i < levels - 1) {
               assert(div_round_up(out_size, block_size) == verity_tree_level_blocks[i + 1]);
           } else {
@@ -347,7 +338,7 @@ int main(int argc, char **argv)
     }
     printf(" ");
     for (size_t i = 0; i < salt_size; i++) {
-        printf("%02x", salt[i]);
+        printf("%02x", salt.data()[i]);
     }
     printf("\n");
 
@@ -363,5 +354,4 @@ int main(int argc, char **argv)
     delete[] verity_tree_levels;
     delete[] verity_tree_level_blocks;
     delete[] verity_tree;
-    delete[] salt;
 }