summaryrefslogtreecommitdiff
path: root/verity
diff options
context:
space:
mode:
authorYunlian Jiang <yunlian@google.com>2017-08-30 09:50:05 -0700
committerYunlian Jiang <yunlian@google.com>2017-08-30 10:13:39 -0700
commit6eb049322c26431961fb69168a113bfd27016ab3 (patch)
treeb2a53e99b7916ac1588b80580e3c7aa349c25977 /verity
parent5c4dcdb68dccf48c701d8daa4e8b88a566f6eb1e (diff)
downloadextras-6eb049322c26431961fb69168a113bfd27016ab3.tar.gz
fix memory leak in unittest.
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
Diffstat (limited to 'verity')
-rw-r--r--verity/build_verity_tree.cpp36
1 files changed, 13 insertions, 23 deletions
diff --git a/verity/build_verity_tree.cpp b/verity/build_verity_tree.cpp
index c50e4494..19e4a2a7 100644
--- a/verity/build_verity_tree.cpp
+++ b/verity/build_verity_tree.cpp
@@ -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;
}