summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2014-12-01 15:06:53 -0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-12-03 21:29:22 +0000
commit45562fb05a63e5f5a373d38e068a7e1100c92e7e (patch)
tree2d7caaf8fc9ee33c2bd91e51669d358fcb316ad2
parent42a850059ca07f5e14fdf31990a35ad63e3f1bd8 (diff)
downloadvboot-45562fb05a63e5f5a373d38e068a7e1100c92e7e.tar.gz
vboot2: Use calloc() in host lib and unit tests
This is cleaner than malloc() immediately followed by memset(). BUG=chromium:423882 BRANCH=none TEST=VBOOT2=1 make runtests Change-Id: Iac32ca54ba18ef6722c6e9c58d521d5337a816b2 Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/232770 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
-rw-r--r--host/lib/host_fw_preamble2.c2
-rw-r--r--host/lib/host_key2.c17
-rw-r--r--host/lib/host_keyblock2.c2
-rw-r--r--host/lib/host_signature2.c3
-rw-r--r--tests/vb2_common_tests.c3
-rw-r--r--tests/vb2_host_key_tests.c3
-rw-r--r--tests/vb2_host_sig_tests.c6
7 files changed, 12 insertions, 24 deletions
diff --git a/host/lib/host_fw_preamble2.c b/host/lib/host_fw_preamble2.c
index 759af1b4..87c4ecdd 100644
--- a/host/lib/host_fw_preamble2.c
+++ b/host/lib/host_fw_preamble2.c
@@ -55,7 +55,7 @@ int vb2_fw_preamble_create(struct vb2_fw_preamble2 **fp_ptr,
fp.c.total_size = fp.sig_offset + sig_size;
/* Allocate buffer and copy components */
- buf = malloc(fp.c.total_size);
+ buf = calloc(fp.c.total_size, 1);
if (!buf)
return VB2_FW_PREAMBLE_CREATE_ALLOC;
diff --git a/host/lib/host_key2.c b/host/lib/host_key2.c
index 065be234..adda9da0 100644
--- a/host/lib/host_key2.c
+++ b/host/lib/host_key2.c
@@ -71,12 +71,10 @@ int vb2_private_key_unpack(struct vb2_private_key **key_ptr,
return VB2_ERROR_UNPACK_PRIVATE_KEY_STRUCT_VERSION;
/* Allocate the new key */
- key = malloc(sizeof(*key));
+ key = calloc(1, sizeof(*key));
if (!key)
return VB2_ERROR_UNPACK_PRIVATE_KEY_ALLOC;
- memset(key, 0, sizeof(*key));
-
/* Copy key algorithms and guid */
key->sig_alg = pkey->sig_alg;
key->hash_alg = pkey->hash_alg;
@@ -140,12 +138,10 @@ int vb2_private_key_read_pem(struct vb2_private_key **key_ptr,
*key_ptr = NULL;
/* Allocate the new key */
- key = malloc(sizeof(*key));
+ key = calloc(1, sizeof(*key));
if (!key)
return VB2_ERROR_READ_PEM_ALLOC;
- memset(key, 0, sizeof(*key));
-
/* Read private key */
f = fopen(filename, "r");
if (!f) {
@@ -212,13 +208,12 @@ int vb2_private_key_write(const struct vb2_private_key *key,
pkey.c.total_size = pkey.key_offset + pkey.key_size;
/* Pack private key */
- buf = malloc(pkey.c.total_size);
+ buf = calloc(1, pkey.c.total_size);
if (!buf) {
free(rsabuf);
return VB2_ERROR_PRIVATE_KEY_WRITE_ALLOC;
}
- memset(buf, 0, pkey.c.total_size);
memcpy(buf, &pkey, sizeof(pkey));
/* strcpy() is ok here because we checked the length above */
@@ -310,11 +305,10 @@ static int vb2_public_key_alloc(struct vb2_public_key **key_ptr,
if (!key_data_size)
return VB2_ERROR_PUBLIC_KEY_ALLOC_SIZE;
- key = malloc(buf_size);
+ key = calloc(1, buf_size);
if (!key)
return VB2_ERROR_PUBLIC_KEY_ALLOC;
- memset(key, 0, buf_size);
key->guid = (struct vb2_guid *)(key + 1);
key->sig_alg = sig_alg;
@@ -454,8 +448,7 @@ int vb2_public_key_pack(struct vb2_packed_key2 **key_ptr,
key.guid = *pubk->guid;
/* Allocate the new buffer */
- buf = malloc(key.c.total_size);
- memset(buf, 0, key.c.total_size);
+ buf = calloc(1, key.c.total_size);
/* Copy data into the buffer */
memcpy(buf, &key, sizeof(key));
diff --git a/host/lib/host_keyblock2.c b/host/lib/host_keyblock2.c
index 9394cf6a..570d3c8a 100644
--- a/host/lib/host_keyblock2.c
+++ b/host/lib/host_keyblock2.c
@@ -52,7 +52,7 @@ int vb2_keyblock_create(struct vb2_keyblock2 **kb_ptr,
kb.c.total_size = kb.sig_offset + sig_size;
/* Allocate buffer and copy header and data key */
- buf = malloc(kb.c.total_size);
+ buf = calloc(1, kb.c.total_size);
if (!buf) {
free(key);
return VB2_KEYBLOCK_CREATE_ALLOC;
diff --git a/host/lib/host_signature2.c b/host/lib/host_signature2.c
index c5b2c466..b81428d0 100644
--- a/host/lib/host_signature2.c
+++ b/host/lib/host_signature2.c
@@ -154,8 +154,7 @@ int vb2_sign_data(struct vb2_signature2 **sig_ptr,
}
/* Allocate signature buffer and copy header */
- buf = malloc(s.c.total_size);
- memset(buf, 0, s.c.total_size);
+ buf = calloc(1, s.c.total_size);
memcpy(buf, &s, sizeof(s));
/* strcpy() is ok because we allocated buffer based on desc length */
diff --git a/tests/vb2_common_tests.c b/tests/vb2_common_tests.c
index 199b1e05..89c8307d 100644
--- a/tests/vb2_common_tests.c
+++ b/tests/vb2_common_tests.c
@@ -665,8 +665,7 @@ static void test_verify_fw_preamble(void)
/* Now that the total size is known, create the real preamble */
buf_size = fp.c.total_size;
- buf = malloc(buf_size);
- memset(buf, 0, buf_size);
+ buf = calloc(1, buf_size);
memcpy(buf, &fp, sizeof(fp));
memcpy(buf + fp.c.fixed_size, desc, sizeof(desc));
diff --git a/tests/vb2_host_key_tests.c b/tests/vb2_host_key_tests.c
index 5780691a..451366f7 100644
--- a/tests/vb2_host_key_tests.c
+++ b/tests/vb2_host_key_tests.c
@@ -187,8 +187,7 @@ static void public_key_tests(const struct alg_combo *combo,
vb2_public_key_free(key);
bufsize = vb2_packed_key_size(combo->sig_alg);
- buf = malloc(bufsize);
- memset(buf, 0, bufsize);
+ buf = calloc(1, bufsize);
vb2_write_file(testfile, buf, bufsize - 1);
TEST_EQ(vb2_public_key_read_keyb(&key, testfile),
diff --git a/tests/vb2_host_sig_tests.c b/tests/vb2_host_sig_tests.c
index 76e128c9..809c7b04 100644
--- a/tests/vb2_host_sig_tests.c
+++ b/tests/vb2_host_sig_tests.c
@@ -117,8 +117,7 @@ static void sig_tests(const struct alg_combo *combo,
c_sig_offs = sizeof(*c) + 24;
TEST_SUCC(vb2_sig_size_for_key(&size, prik, NULL), "Sig size");
bufsize = c_sig_offs + size;
- buf = malloc(bufsize);
- memset(buf, 0, bufsize);
+ buf = calloc(1, bufsize);
memset(buf + sizeof(*c), 0x12, 24);
c = (struct vb2_struct_common *)buf;
c->total_size = bufsize;
@@ -135,8 +134,7 @@ static void sig_tests(const struct alg_combo *combo,
/* Multiply sign an object */
TEST_SUCC(vb2_sig_size_for_keys(&size, priks, 2), "Sigs size");
bufsize = c_sig_offs + size;
- buf = malloc(bufsize);
- memset(buf, 0, bufsize);
+ buf = calloc(1, bufsize);
memset(buf + sizeof(*c), 0x12, 24);
c = (struct vb2_struct_common *)buf;
c->total_size = bufsize;