From b5f1c0d88be57d10db5b06b07d0b0ea4aa220bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 09:17:46 +0100 Subject: [PATCH 1/7] udev: add assert to make coverity happy Coverity says: CID 1446387 (#1 of 1): Bad bit shift operation (BAD_SHIFT) 8. negative_shift: In expression 1U << (int)cmd, shifting by a negative amount has undefined behavior. The shift amount, cmd, is -22. I don't think there's any issue, unless we forget to set token->data appropriately. Let's add an assert. --- src/udev/udev-rules.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index e12225ca39..96a391c07c 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -1773,6 +1773,7 @@ static int udev_rule_apply_token_to_event( } case TK_M_IMPORT_BUILTIN: { UdevBuiltinCommand cmd = PTR_TO_UDEV_BUILTIN_CMD(token->data); + assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX); unsigned mask = 1U << (int) cmd; if (udev_builtin_run_once(cmd)) { From 47237e0ed47e018ce5c32bab618a5dfa5d1aa2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 09:23:22 +0100 Subject: [PATCH 2/7] shared/generator: add missing initializer Coverity CID#1444710. --- src/shared/generator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/generator.c b/src/shared/generator.c index b8616cf1f2..8b95c772db 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -652,7 +652,7 @@ int generator_write_veritysetup_service_section( const char *roothash, const char *options) { - _cleanup_free_ char *name_escaped = NULL, *data_what_escaped = NULL, *hash_what_escaped, + _cleanup_free_ char *name_escaped = NULL, *data_what_escaped = NULL, *hash_what_escaped = NULL, *roothash_escaped = NULL, *options_escaped = NULL; assert(f); From 363729c4704b9838f351eaba3acb8b1ed16fae0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 09:35:52 +0100 Subject: [PATCH 3/7] sd-journal: add forgotten unmap in error path Bug introduced in 4b5bc5396c090ee41c45cab9052372d296c4a2f4 :( Coverity CID#1444709. --- src/libsystemd/sd-journal/compress.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libsystemd/sd-journal/compress.c b/src/libsystemd/sd-journal/compress.c index aaf186ba69..429dbadb6c 100644 --- a/src/libsystemd/sd-journal/compress.c +++ b/src/libsystemd/sd-journal/compress.c @@ -664,10 +664,11 @@ int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes) { offset += n; total_out += n; - if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) - return log_debug_errno(SYNTHETIC_ERRNO(EFBIG), - "Compressed stream longer than %" PRIu64 " bytes", - max_bytes); + if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) { + r = log_debug_errno(SYNTHETIC_ERRNO(EFBIG), + "Compressed stream longer than %" PRIu64 " bytes", max_bytes); + goto cleanup; + } if (size - offset < frame_size + 4) { k = loop_write(fdt, buf, offset, false); From 333ab199a12c7b060d3a3f4d50a8f73ee4fd5ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 09:50:49 +0100 Subject: [PATCH 4/7] fsck: make sure we don't read an unitialized variable This use on %n was completely unnecessary: fprintf returns the number of characters written. And the issue was that if fprintf failed for whatever reason, it would not process the %n and m would be unitialized. Rework the code a bit to simplify it. Coverity CID#1444708. --- src/fsck/fsck.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index cd012f0f3a..94aa31e71a 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -172,7 +172,7 @@ static int process_progress(int fd, FILE* console) { } for (;;) { - int pass, m; + int pass; unsigned long cur, max; _cleanup_free_ char *device = NULL; double p; @@ -206,18 +206,17 @@ static int process_progress(int fd, FILE* console) { last = t; p = percent(pass, cur, max); - fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m); - fflush(console); + r = fprintf(console, "\r%s: fsck %3.1f%% complete...\r", device, p); + if (r < 0) + return -EIO; /* No point in continuing if something happend to our output stream */ - if (m > clear) - clear = m; + fflush(console); + clear = MAX(clear, r); } if (clear > 0) { - unsigned j; - fputc('\r', console); - for (j = 0; j < (unsigned) clear; j++) + for (int j = 0; j < clear; j++) fputc(' ', console); fputc('\r', console); fflush(console); From 0eacd1852abc1b17bef29f0ba3b72bbb1b6f8a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 09:55:01 +0100 Subject: [PATCH 5/7] basic/locale-util: reduce variable scope --- src/basic/locale-util.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c index f58e0348ba..93261e675e 100644 --- a/src/basic/locale-util.c +++ b/src/basic/locale-util.c @@ -98,7 +98,6 @@ static int add_locales_from_archive(Set *locales) { _cleanup_close_ int fd = -1; size_t sz = 0; struct stat st; - size_t i; int r; fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC); @@ -129,7 +128,7 @@ static int add_locales_from_archive(Set *locales) { } e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset); - for (i = 0; i < h->namehash_size; i++) { + for (size_t i = 0; i < h->namehash_size; i++) { char *z; if (e[i].locrec_offset == 0) @@ -434,12 +433,10 @@ const char *special_glyph(SpecialGlyph code) { } void locale_variables_free(char *l[_VARIABLE_LC_MAX]) { - LocaleVariable i; - if (!l) return; - for (i = 0; i < _VARIABLE_LC_MAX; i++) + for (LocaleVariable i = 0; i < _VARIABLE_LC_MAX; i++) l[i] = mfree(l[i]); } From 78b4e9ed173a5cbe2becb7cfd238c62aa4ccdfad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 11:59:22 +0100 Subject: [PATCH 6/7] homework: reduce scope of iterator variables --- src/home/homework-luks.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index b0b2d8029f..a2d7c78005 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -518,7 +518,7 @@ static int luks_validate( blkid_loff_t offset = 0, size = 0; blkid_partlist pl; bool found = false; - int r, i, n; + int r, n; assert(fd >= 0); assert(label); @@ -570,7 +570,7 @@ static int luks_validate( if (n < 0) return errno > 0 ? -errno : -EIO; - for (i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { blkid_partition pp; sd_id128_t id; const char *sid; @@ -681,12 +681,12 @@ static int luks_validate_home_record( PasswordCache *cache, UserRecord **ret_luks_home_record) { - int r, token; + int r; assert(cd); assert(h); - for (token = 0;; token++) { + for (int token = 0;; token++) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *rr = NULL; _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL; _cleanup_(user_record_unrefp) UserRecord *lhr = NULL; @@ -2423,7 +2423,7 @@ static int prepare_resize_partition( _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; _cleanup_free_ char *path = NULL, *disk_uuid_as_string = NULL; - size_t n_partitions, i; + size_t n_partitions; sd_id128_t disk_uuid; bool found = false; int r; @@ -2473,7 +2473,7 @@ static int prepare_resize_partition( return log_error_errno(r, "Failed to acquire partition table: %m"); n_partitions = fdisk_table_get_nents(t); - for (i = 0; i < n_partitions; i++) { + for (size_t i = 0; i < n_partitions; i++) { struct fdisk_partition *p; p = fdisk_table_get_partition(t, i); @@ -2898,7 +2898,7 @@ int home_passwd_luks( PasswordCache *cache, /* the passwords acquired via PKCS#11/FIDO2 security tokens */ char **effective_passwords /* new passwords */) { - size_t volume_key_size, i, max_key_slots, n_effective; + size_t volume_key_size, max_key_slots, n_effective; _cleanup_(erase_and_freep) void *volume_key = NULL; struct crypt_pbkdf_type good_pbkdf, minimal_pbkdf; const char *type; @@ -2943,7 +2943,7 @@ int home_passwd_luks( build_good_pbkdf(&good_pbkdf, h); build_minimal_pbkdf(&minimal_pbkdf, h); - for (i = 0; i < max_key_slots; i++) { + for (size_t i = 0; i < max_key_slots; i++) { r = crypt_keyslot_destroy(setup->crypt_device, i); if (r < 0 && !IN_SET(r, -ENOENT, -EINVAL)) /* Returns EINVAL or ENOENT if there's no key in this slot already */ return log_error_errno(r, "Failed to destroy LUKS password: %m"); From a4aa5742f75f0753c4dfa812059af608c9bab7b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Feb 2021 12:00:09 +0100 Subject: [PATCH 7/7] homework: fix unitialized variable Coverity CID#1444703. --- src/home/homework-luks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index a2d7c78005..c6b34d28d4 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -572,7 +572,7 @@ static int luks_validate( for (int i = 0; i < n; i++) { blkid_partition pp; - sd_id128_t id; + sd_id128_t id = SD_ID128_NULL; const char *sid; errno = 0;