diff options
author | Randall Spangler <rspangler@chromium.org> | 2016-09-28 11:54:20 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-10-01 00:04:39 -0700 |
commit | 770202f0c745d68f2635dbb8d0f0765aae75a087 (patch) | |
tree | 46d29849be392aed1a168e0f291ab98a210dca8d | |
parent | fefc682bb70d7fd997f4ef0079e8fec0a4937cf7 (diff) | |
download | vboot-770202f0c745d68f2635dbb8d0f0765aae75a087.tar.gz |
Fix more coverity warnings
Assorted minor code issues, which we should fix so any new errors stand
out more.
BUG=chromium:643769
BRANCH=none
TEST=make runtests
Change-Id: I82ece2de948ef224115c408bdfc09445d3da119b
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/390337
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r-- | futility/cmd_create.c | 5 | ||||
-rw-r--r-- | futility/cmd_vbutil_key.c | 1 | ||||
-rw-r--r-- | host/lib/crossystem.c | 32 | ||||
-rw-r--r-- | host/lib/host_key2.c | 4 | ||||
-rw-r--r-- | host/lib/host_misc.c | 4 | ||||
-rw-r--r-- | tests/vb20_common3_tests.c | 10 | ||||
-rw-r--r-- | tests/vboot_common2_tests.c | 6 | ||||
-rw-r--r-- | tests/vboot_common3_tests.c | 18 |
8 files changed, 52 insertions, 28 deletions
diff --git a/futility/cmd_create.c b/futility/cmd_create.c index 578e3be5..bdec20b5 100644 --- a/futility/cmd_create.c +++ b/futility/cmd_create.c @@ -184,7 +184,10 @@ static int vb2_make_keypair() if (!rsa_key) { /* Check if the PEM contains only a public key */ - fseek(fp, 0, SEEK_SET); + if (0 != fseek(fp, 0, SEEK_SET)) { + fprintf(stderr, "Error seeking in %s\n", infile); + goto done; + } rsa_key = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL); } fclose(fp); diff --git a/futility/cmd_vbutil_key.c b/futility/cmd_vbutil_key.c index 4362255d..bc013127 100644 --- a/futility/cmd_vbutil_key.c +++ b/futility/cmd_vbutil_key.c @@ -86,6 +86,7 @@ static int do_pack(const char *infile, const char *outfile, uint32_t algorithm, if (pubkey) { if (0 != vb2_write_packed_key(outfile, pubkey)) { fprintf(stderr, "vbutil_key: Error writing key.\n"); + free(pubkey); return 1; } free(pubkey); diff --git a/host/lib/crossystem.c b/host/lib/crossystem.c index 53ec6897..6d70d6aa 100644 --- a/host/lib/crossystem.c +++ b/host/lib/crossystem.c @@ -747,20 +747,24 @@ int VbSetSystemPropertyString(const char* name, const char* value) { } -static int InAndroid() { - int fd; - struct stat s; - - /* In Android, mosys utility located in /system/bin - check if file exists. Using fstat because for some - reason, stat() was seg faulting in Android */ - fd = open(MOSYS_ANDROID_PATH, O_RDONLY); - if (fd != -1 && fstat(fd, &s) == 0) { - close(fd); - return 1; - } - close(fd); - return 0; +static int InAndroid(void) +{ + int fd; + struct stat s; + int retval = 0; + + /* + * In Android, mosys utility located in /system/bin check if file + * exists. Using fstat because for some reason, stat() was seg + * faulting in Android + */ + fd = open(MOSYS_ANDROID_PATH, O_RDONLY); + if (fd != -1) { + if (fstat(fd, &s) == 0) + retval = 1; + close(fd); + } + return retval; } diff --git a/host/lib/host_key2.c b/host/lib/host_key2.c index ff6c1c35..0f4168ed 100644 --- a/host/lib/host_key2.c +++ b/host/lib/host_key2.c @@ -202,8 +202,8 @@ int vb2_copy_packed_key(struct vb2_packed_key *dest, struct vb2_packed_key *vb2_read_packed_key(const char *filename) { - struct vb2_packed_key *key; - uint32_t file_size; + struct vb2_packed_key *key = NULL; + uint32_t file_size = 0; if (VB2_SUCCESS != vb2_read_file(filename, (uint8_t **)&key, &file_size)) { diff --git a/host/lib/host_misc.c b/host/lib/host_misc.c index 89a6c2f1..dde05d07 100644 --- a/host/lib/host_misc.c +++ b/host/lib/host_misc.c @@ -37,8 +37,10 @@ uint8_t* ReadFile(const char* filename, uint64_t* sizeptr) { fseek(f, 0, SEEK_END); size = ftell(f); - if (size < 0) + if (size < 0) { + fclose(f); return NULL; + } rewind(f); buf = malloc(size); diff --git a/tests/vb20_common3_tests.c b/tests/vb20_common3_tests.c index fd15892a..24029293 100644 --- a/tests/vb20_common3_tests.c +++ b/tests/vb20_common3_tests.c @@ -224,8 +224,11 @@ static void test_verify_fw_preamble(struct vb2_packed_key *public_key, private_key, 0x5678); TEST_PTR_NEQ(hdr, NULL, "vb2_verify_fw_preamble() prereq test preamble"); - if (!hdr) + if (!hdr) { + free(body_sig); return; + } + hsize = (uint32_t) hdr->preamble_size; h = (struct vb2_fw_preamble *)malloc(hsize + 16384); @@ -366,8 +369,11 @@ static void test_verify_kernel_preamble( private_key); TEST_PTR_NEQ(hdr, NULL, "vb2_verify_kernel_preamble() prereq test preamble"); - if (!hdr) + if (!hdr) { + free(body_sig); return; + } + hsize = (uint32_t) hdr->preamble_size; struct vb2_kernel_preamble *h = (struct vb2_kernel_preamble *)malloc(hsize + 16384); diff --git a/tests/vboot_common2_tests.c b/tests/vboot_common2_tests.c index adbb2b14..b5b4b5bf 100644 --- a/tests/vboot_common2_tests.c +++ b/tests/vboot_common2_tests.c @@ -142,8 +142,12 @@ static void VerifyKernelPreambleTest(const VbPublicKey *public_key, vb2_create_kernel_preamble(0x1234, 0x100000, 0x300000, 0x4000, body_sig, 0, 0, 0, 0, private_key); TEST_NEQ(hdr && rsa, 0, "VerifyKernelPreamble() prerequisites"); - if (!hdr) + if (!hdr) { + free(body_sig); + RSAPublicKeyFree(rsa); return; + } + hsize = (unsigned) hdr->preamble_size; h = (VbKernelPreambleHeader *)malloc(hsize + 16384); diff --git a/tests/vboot_common3_tests.c b/tests/vboot_common3_tests.c index 1365631d..d146e9e7 100644 --- a/tests/vboot_common3_tests.c +++ b/tests/vboot_common3_tests.c @@ -165,7 +165,10 @@ int test_permutation(int signing_key_algorithm, int data_key_algorithm, int signing_rsa_len = siglen_map[signing_key_algorithm] * 8; int data_rsa_len = siglen_map[data_key_algorithm] * 8; + struct vb2_private_key *signing_private_key = NULL; + struct vb2_packed_key *data_public_key = NULL; VbPublicKey *signing_public_key = NULL; + int retval = 1; printf("***Testing signing algorithm: %s\n", algo_strings[signing_key_algorithm]); @@ -174,12 +177,12 @@ int test_permutation(int signing_key_algorithm, int data_key_algorithm, snprintf(filename, sizeof(filename), "%s/key_rsa%d.pem", keys_dir, signing_rsa_len); - struct vb2_private_key *signing_private_key = + signing_private_key = vb2_read_private_key_pem(filename, signing_key_algorithm); if (!signing_private_key) { fprintf(stderr, "Error reading signing_private_key: %s\n", filename); - return 1; + goto cleanup; } snprintf(filename, sizeof(filename), @@ -189,22 +192,23 @@ int test_permutation(int signing_key_algorithm, int data_key_algorithm, if (!signing_public_key) { fprintf(stderr, "Error reading signing_public_key: %s\n", filename); - return 1; + goto cleanup; } snprintf(filename, sizeof(filename), "%s/key_rsa%d.keyb", keys_dir, data_rsa_len); - struct vb2_packed_key *data_public_key = - vb2_read_packed_keyb(filename, data_key_algorithm, 1); + data_public_key = vb2_read_packed_keyb(filename, data_key_algorithm, 1); if (!data_public_key) { fprintf(stderr, "Error reading data_public_key: %s\n", filename); - return 1; + goto cleanup; } KeyBlockVerifyTest(signing_public_key, signing_private_key, data_public_key); + retval = 0; +cleanup: if (signing_public_key) free(signing_public_key); if (signing_private_key) @@ -212,7 +216,7 @@ int test_permutation(int signing_key_algorithm, int data_key_algorithm, if (data_public_key) free(data_public_key); - return 0; + return retval; } struct test_perm |