diff options
author | Randall Spangler <rspangler@chromium.org> | 2016-09-02 12:25:27 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-09-14 13:05:02 -0700 |
commit | d8a9ede87c0a0b804ef17c60f3b2baac3498f6ae (patch) | |
tree | c88b1a975f5eccc27767cf2e53b224656eafe25f /host | |
parent | afa7350dccee079673831ef16a7c60a9a74ba77f (diff) | |
download | vboot-d8a9ede87c0a0b804ef17c60f3b2baac3498f6ae.tar.gz |
futility/host lib: Fix 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: Ib37b45dea54bd506b519b0304300b8d192e34339
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/382319
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'host')
-rw-r--r-- | host/arch/x86/lib/crossystem_arch.c | 12 | ||||
-rw-r--r-- | host/lib/crossystem.c | 2 | ||||
-rw-r--r-- | host/lib/file_keys.c | 71 | ||||
-rw-r--r-- | host/lib/host_keyblock.c | 5 | ||||
-rw-r--r-- | host/lib/host_misc.c | 4 | ||||
-rw-r--r-- | host/lib21/host_key.c | 6 | ||||
-rw-r--r-- | host/lib21/include/host_key2.h | 4 |
7 files changed, 59 insertions, 45 deletions
diff --git a/host/arch/x86/lib/crossystem_arch.c b/host/arch/x86/lib/crossystem_arch.c index d7e89fe1..2b5d00aa 100644 --- a/host/arch/x86/lib/crossystem_arch.c +++ b/host/arch/x86/lib/crossystem_arch.c @@ -170,9 +170,9 @@ int VbReadNvStorage(VbNvContext* vnc) { } -int VbWriteNvStorage(VbNvContext* vnc) { +int VbWriteNvStorage(VbNvContext* vnc) +{ unsigned offs, blksz; - VbSharedDataHeader *sh = VbSharedDataRead(); if (!vnc->raw_changed) return 0; /* Nothing changed, so no need to write */ @@ -189,8 +189,12 @@ int VbWriteNvStorage(VbNvContext* vnc) { return -1; /* Also attempt to write using mosys if using vboot2 */ - if (sh && (sh->flags & VBSD_BOOT_FIRMWARE_VBOOT2)) - VbWriteNvStorage_mosys(vnc); + VbSharedDataHeader *sh = VbSharedDataRead(); + if (sh) { + if (sh->flags & VBSD_BOOT_FIRMWARE_VBOOT2) + VbWriteNvStorage_mosys(vnc); + free(sh); + } return 0; } diff --git a/host/lib/crossystem.c b/host/lib/crossystem.c index 87c74169..53ec6897 100644 --- a/host/lib/crossystem.c +++ b/host/lib/crossystem.c @@ -755,7 +755,7 @@ static int InAndroid() { check if file exists. Using fstat because for some reason, stat() was seg faulting in Android */ fd = open(MOSYS_ANDROID_PATH, O_RDONLY); - if (fstat(fd, &s) == 0) { + if (fd != -1 && fstat(fd, &s) == 0) { close(fd); return 1; } diff --git a/host/lib/file_keys.c b/host/lib/file_keys.c index fd07752b..a774ba22 100644 --- a/host/lib/file_keys.c +++ b/host/lib/file_keys.c @@ -22,45 +22,52 @@ #include "host_common.h" #include "signature_digest.h" -uint8_t* BufferFromFile(const char* input_file, uint64_t* len) { - int fd; - struct stat stat_fd; - uint8_t* buf = NULL; +uint8_t *BufferFromFile(const char* input_file, uint64_t* len) +{ + int fd; + struct stat stat_fd; + uint8_t* buf = NULL; - if ((fd = open(input_file, O_RDONLY)) == -1) { - VBDEBUG(("Couldn't open file %s\n", input_file)); - return NULL; - } + if ((fd = open(input_file, O_RDONLY)) == -1) { + VBDEBUG(("Couldn't open file %s\n", input_file)); + return NULL; + } - if (-1 == fstat(fd, &stat_fd)) { - VBDEBUG(("Couldn't stat file %s\n", input_file)); - return NULL; - } - *len = stat_fd.st_size; + if (-1 == fstat(fd, &stat_fd)) { + VBDEBUG(("Couldn't stat file %s\n", input_file)); + close(fd); + return NULL; + } + *len = stat_fd.st_size; - buf = (uint8_t*)malloc(*len); - if (!buf) { - VbExError("Couldn't allocate %ld bytes for file %s\n", *len, input_file); - return NULL; - } + buf = (uint8_t *)malloc(*len); + if (!buf) { + VbExError("Couldn't allocate %ld bytes for file %s\n", + *len, input_file); + close(fd); + return NULL; + } - if (*len != read(fd, buf, *len)) { - VBDEBUG(("Couldn't read file %s into a buffer\n", input_file)); - return NULL; - } + if (*len != read(fd, buf, *len)) { + VBDEBUG(("Couldn't read file %s into a buffer\n", input_file)); + free(buf); + close(fd); + return NULL; + } - close(fd); - return buf; + close(fd); + return buf; } -RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) { - uint64_t len; - RSAPublicKey* key = NULL; - uint8_t* buf = BufferFromFile(input_file, &len); - if (buf) - key = RSAPublicKeyFromBuf(buf, len); - free(buf); - return key; +RSAPublicKey *RSAPublicKeyFromFile(const char *input_file) { + uint64_t len; + RSAPublicKey* key = NULL; + + uint8_t *buf = BufferFromFile(input_file, &len); + if (buf) + key = RSAPublicKeyFromBuf(buf, len); + free(buf); + return key; } int DigestFile(char *input_file, enum vb2_hash_algorithm alg, diff --git a/host/lib/host_keyblock.c b/host/lib/host_keyblock.c index 21fa0995..dde8fe6c 100644 --- a/host/lib/host_keyblock.c +++ b/host/lib/host_keyblock.c @@ -92,6 +92,9 @@ struct vb2_keyblock *vb2_create_keyblock_external( uint32_t flags, const char *external_signer) { + if (!signing_key_pem_file || !data_key || !external_signer) + return NULL; + uint32_t signed_size = sizeof(struct vb2_keyblock) + data_key->key_size; uint32_t sig_data_size = vb2_rsa_sig_size(algorithm); uint32_t block_size = @@ -101,8 +104,6 @@ struct vb2_keyblock *vb2_create_keyblock_external( struct vb2_keyblock *h = (struct vb2_keyblock *)calloc(block_size, 1); if (!h) return NULL; - if (!signing_key_pem_file || !data_key || !external_signer) - return NULL; uint8_t *data_key_dest = (uint8_t *)(h + 1); uint8_t *block_chk_dest = data_key_dest + data_key->key_size; diff --git a/host/lib/host_misc.c b/host/lib/host_misc.c index 3fb9b24e..89a6c2f1 100644 --- a/host/lib/host_misc.c +++ b/host/lib/host_misc.c @@ -27,7 +27,7 @@ char* StrCopy(char* dest, const char* src, int dest_size) { uint8_t* ReadFile(const char* filename, uint64_t* sizeptr) { FILE* f; uint8_t* buf; - uint64_t size; + long size; f = fopen(filename, "rb"); if (!f) { @@ -37,6 +37,8 @@ uint8_t* ReadFile(const char* filename, uint64_t* sizeptr) { fseek(f, 0, SEEK_END); size = ftell(f); + if (size < 0) + return NULL; rewind(f); buf = malloc(size); diff --git a/host/lib21/host_key.c b/host/lib21/host_key.c index 4ef18d88..2ebd08a1 100644 --- a/host/lib21/host_key.c +++ b/host/lib21/host_key.c @@ -18,7 +18,7 @@ #include "host_key2.h" #include "host_misc.h" -struct vb2_text_vs_enum vb2_text_vs_sig[] = { +const struct vb2_text_vs_enum vb2_text_vs_sig[] = { {"RSA1024", VB2_SIG_RSA1024}, {"RSA2048", VB2_SIG_RSA2048}, {"RSA4096", VB2_SIG_RSA4096}, @@ -26,7 +26,7 @@ struct vb2_text_vs_enum vb2_text_vs_sig[] = { {0, 0} }; -struct vb2_text_vs_enum vb2_text_vs_hash[] = { +const struct vb2_text_vs_enum vb2_text_vs_hash[] = { {"SHA1", VB2_HASH_SHA1}, {"SHA256", VB2_HASH_SHA256}, {"SHA512", VB2_HASH_SHA512}, @@ -155,7 +155,7 @@ int vb21_private_key_read(struct vb2_private_key **key_ptr, const char *filename) { uint32_t size = 0; - uint8_t *buf; + uint8_t *buf = NULL; int rv; *key_ptr = NULL; diff --git a/host/lib21/include/host_key2.h b/host/lib21/include/host_key2.h index 4681a5f4..5a2ec0ad 100644 --- a/host/lib21/include/host_key2.h +++ b/host/lib21/include/host_key2.h @@ -55,8 +55,8 @@ const struct vb2_text_vs_enum *vb2_lookup_by_name( const struct vb2_text_vs_enum *table, const char *name); -extern struct vb2_text_vs_enum vb2_text_vs_sig[]; -extern struct vb2_text_vs_enum vb2_text_vs_hash[]; +extern const struct vb2_text_vs_enum vb2_text_vs_sig[]; +extern const struct vb2_text_vs_enum vb2_text_vs_hash[]; /** * Return the name of a signature algorithm. |