From 23bada3c5ae36e159b00456a2537170f6ad3a57d Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Wed, 10 Apr 2019 12:30:09 +0200 Subject: [PATCH] Fix several issues found by Coverity scan. --- lib/crypto_backend/crypto_storage.c | 4 ++-- lib/utils_device_locking.c | 9 +++++---- lib/utils_storage_wrappers.c | 4 +++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/crypto_backend/crypto_storage.c b/lib/crypto_backend/crypto_storage.c index 25a9f56a..3ae4d2ef 100644 --- a/lib/crypto_backend/crypto_storage.c +++ b/lib/crypto_backend/crypto_storage.c @@ -246,7 +246,7 @@ int crypt_storage_decrypt(struct crypt_storage *ctx, uint64_t iv_offset, uint64_t length, char *buffer) { - unsigned int i; + uint64_t i; int r = 0; if (length & ((1 << ctx->sector_shift) - 1)) @@ -275,7 +275,7 @@ int crypt_storage_encrypt(struct crypt_storage *ctx, uint64_t iv_offset, uint64_t length, char *buffer) { - unsigned int i; + uint64_t i; int r = 0; if (length & ((1 << ctx->sector_shift) - 1)) diff --git a/lib/utils_device_locking.c b/lib/utils_device_locking.c index 1699a9e8..f9f1e6cb 100644 --- a/lib/utils_device_locking.c +++ b/lib/utils_device_locking.c @@ -301,12 +301,12 @@ static unsigned device_lock_dec(struct crypt_lock_handle *h) static int acquire_and_verify(struct crypt_device *cd, struct device *device, const char *resource, int flock_op, struct crypt_lock_handle **lock) { int r; - struct crypt_lock_handle *h = malloc(sizeof(*h)); + struct crypt_lock_handle *h; if (device && resource) return -EINVAL; - if (!h) + if (!(h = malloc(sizeof(*h)))) return -ENOMEM; do { @@ -329,7 +329,8 @@ static int acquire_and_verify(struct crypt_device *cd, struct device *device, co */ r = verify_lock_handle(device_path(device), h); if (r) { - flock(h->flock_fd, LOCK_UN); + if (flock(h->flock_fd, LOCK_UN)) + log_dbg(cd, "flock on fd %d failed.", h->flock_fd); release_lock_handle(cd, h); log_dbg(cd, "Lock handle verification failed."); } @@ -337,7 +338,7 @@ static int acquire_and_verify(struct crypt_device *cd, struct device *device, co if (r) { free(h); - return r; + return -EINVAL; } *lock = h; diff --git a/lib/utils_storage_wrappers.c b/lib/utils_storage_wrappers.c index b6383220..96be9cd9 100644 --- a/lib/utils_storage_wrappers.c +++ b/lib/utils_storage_wrappers.c @@ -185,6 +185,7 @@ int crypt_storage_wrapper_init(struct crypt_device *cd, return -ENOMEM; memset(w, 0, sizeof(*w)); + w->dev_fd = -1; w->data_offset = data_offset; w->mem_alignment = device_alignment(device); w->block_size = device_block_size(cd, device); @@ -376,7 +377,8 @@ void crypt_storage_wrapper_destroy(struct crypt_storage_wrapper *cw) dm_remove_device(NULL, cw->u.dm.name, CRYPT_DEACTIVATE_FORCE); } - close(cw->dev_fd); + if (cw->dev_fd >= 0) + close(cw->dev_fd); free(cw); }