From c1ab9a5a1a241e4021ef6bf611e580c46ebbb3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Wed, 10 Nov 2021 15:02:43 +0100 Subject: [PATCH] Fold print; return err; into return print, err; --- src/bin/zfs-tpm-list.cpp | 6 ++---- src/bin/zfs-tpm2-change-key.cpp | 3 +-- src/fd.cpp | 24 ++++++++---------------- src/main.hpp | 26 +++++++++++--------------- src/tpm1x.cpp | 6 ++---- src/tpm1x.hpp | 7 ++----- src/tpm2.cpp | 30 ++++++++++-------------------- src/zfs.cpp | 22 +++++++--------------- src/zfs.hpp | 3 +-- 9 files changed, 44 insertions(+), 83 deletions(-) diff --git a/src/bin/zfs-tpm-list.cpp b/src/bin/zfs-tpm-list.cpp index 21ed089..7088cdd 100644 --- a/src/bin/zfs-tpm-list.cpp +++ b/src/bin/zfs-tpm-list.cpp @@ -57,10 +57,8 @@ int main(int argc, char ** argv) { maxdepth = SIZE_MAX; break; case 'd': - if(parse_int(optarg, maxdepth)) { - fprintf(stderr, "%s is not an integer\n", optarg); - return __LINE__; - } + if(parse_int(optarg, maxdepth)) + return fprintf(stderr, "%s is not an integer\n", optarg), __LINE__; break; case 'a': print_nontzpfms = true; diff --git a/src/bin/zfs-tpm2-change-key.cpp b/src/bin/zfs-tpm2-change-key.cpp index f084d33..68f9fbd 100644 --- a/src/bin/zfs-tpm2-change-key.cpp +++ b/src/bin/zfs-tpm2-change-key.cpp @@ -73,8 +73,7 @@ int main(int argc, char ** argv) { char persistent_handle_s[2 + sizeof(persistent_handle) * 2 + 1]; if(auto written = snprintf(persistent_handle_s, sizeof(persistent_handle_s), "0x%" PRIX32, persistent_handle); written < 0 || written >= static_cast(sizeof(persistent_handle_s))) { - fprintf(stderr, "Truncated persistent_handle name? %d/%zu\n", written, sizeof(persistent_handle_s)); - return __LINE__; + return fprintf(stderr, "Truncated persistent_handle name? %d/%zu\n", written, sizeof(persistent_handle_s)), __LINE__; } TRY_MAIN(set_key_props(dataset, THIS_BACKEND, persistent_handle_s)); } diff --git a/src/fd.cpp b/src/fd.cpp index 9aa6f28..ffbbe36 100644 --- a/src/fd.cpp +++ b/src/fd.cpp @@ -41,10 +41,8 @@ int read_exact(const char * path, void * data, size_t len) { if(const auto rd = TRY("read input file", read(infd, data, len))) { len -= rd; data = static_cast(data) + rd; - } else { - fprintf(stderr, "Couldn't read %zu bytes from input file: too short\n", len); - return __LINE__; - } + } else + return fprintf(stderr, "Couldn't read %zu bytes from input file: too short\n", len), __LINE__; return 0; } @@ -159,24 +157,18 @@ int read_new_passphrase(const char * whom, uint8_t *& buf, size_t & len_out, siz TRY_MAIN(get_key_material_raw(whom, false, true, first_passphrase, first_passphrase_len)); quickscope_wrapper first_passphrase_deleter{[&] { free(first_passphrase); }}; - if(first_passphrase_len != 0 && first_passphrase_len < MIN_PASSPHRASE_LEN) { - fprintf(stderr, "Passphrase too short (min %u)\n", MIN_PASSPHRASE_LEN); - return __LINE__; - } - if(first_passphrase_len > max_len) { - fprintf(stderr, "Passphrase too long (max %zu)\n", max_len); - return __LINE__; - } + if(first_passphrase_len != 0 && first_passphrase_len < MIN_PASSPHRASE_LEN) + return fprintf(stderr, "Passphrase too short (min %u)\n", MIN_PASSPHRASE_LEN), __LINE__; + if(first_passphrase_len > max_len) + return fprintf(stderr, "Passphrase too long (max %zu)\n", max_len), __LINE__; uint8_t * second_passphrase{}; size_t second_passphrase_len{}; TRY_MAIN(get_key_material_raw(whom, true, true, second_passphrase, second_passphrase_len)); quickscope_wrapper second_passphrase_deleter{[&] { free(second_passphrase); }}; - if(second_passphrase_len != first_passphrase_len || memcmp(first_passphrase, second_passphrase, first_passphrase_len)) { - fprintf(stderr, "Provided keys do not match.\n"); - return __LINE__; - } + if(second_passphrase_len != first_passphrase_len || memcmp(first_passphrase, second_passphrase, first_passphrase_len)) + return fprintf(stderr, "Provided keys do not match.\n"), __LINE__; if(second_passphrase_len) { buf = second_passphrase; diff --git a/src/main.hpp b/src/main.hpp index a773515..a53a35a 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -44,10 +44,8 @@ int do_bare_main(int argc, char ** argv, const char * getoptions, const char * u if constexpr(std::is_same_v, void>) getoptfn(opt); else { - if(auto err = getoptfn(opt)) { - fprintf(stderr, "Usage: %s [-hV] %s%s%s\n", argv[0], usage, strlen(usage) ? " " : "", dataset_usage); - return err; - } + if(auto err = getoptfn(opt)) + return fprintf(stderr, "Usage: %s [-hV] %s%s%s\n", argv[0], usage, strlen(usage) ? " " : "", dataset_usage), err; } } @@ -57,13 +55,12 @@ int do_bare_main(int argc, char ** argv, const char * getoptions, const char * u template int do_main(int argc, char ** argv, const char * getoptions, const char * usage, G && getoptfn, M && main) { return do_bare_main(argc, argv, getoptions, usage, "", getoptfn, [&](auto libz) { - if(optind >= argc) { - fprintf(stderr, - "No dataset to act on?\n" - "Usage: %s [-hV] %s%s\n", - argv[0], usage, strlen(usage) ? " " : ""); - return __LINE__; - } + if(optind >= argc) + return fprintf(stderr, + "No dataset to act on?\n" + "Usage: %s [-hV] %s%s\n", + argv[0], usage, strlen(usage) ? " " : ""), + __LINE__; auto dataset = TRY_PTR(nullptr, zfs_open(libz, argv[optind], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)); quickscope_wrapper dataset_deleter{[&] { zfs_close(dataset); }}; @@ -72,10 +69,9 @@ int do_main(int argc, char ** argv, const char * getoptions, const char * usage, boolean_t dataset_is_root; TRY("get encryption root", zfs_crypto_get_encryption_root(dataset, &dataset_is_root, encryption_root)); - if(!dataset_is_root && !strlen(encryption_root)) { - fprintf(stderr, "Dataset %s not encrypted?\n", zfs_get_name(dataset)); - return __LINE__; - } else if(!dataset_is_root) { + if(!dataset_is_root && !strlen(encryption_root)) + return fprintf(stderr, "Dataset %s not encrypted?\n", zfs_get_name(dataset)), __LINE__; + else if(!dataset_is_root) { fprintf(stderr, "Using dataset %s's encryption root %s instead.\n", zfs_get_name(dataset), encryption_root); zfs_close(dataset); dataset = TRY_PTR(nullptr, zfs_open(libz, encryption_root, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)); diff --git a/src/tpm1x.cpp b/src/tpm1x.cpp index 1defbc0..07e30d2 100644 --- a/src/tpm1x.cpp +++ b/src/tpm1x.cpp @@ -23,10 +23,8 @@ tpm1x_handle::~tpm1x_handle() { int tpm1x_parse_handle(const char * dataset_name, char * handle_s, tpm1x_handle & handle) { auto midpoint = strchr(handle_s, ':'); - if(!midpoint) { - fprintf(stderr, "Dataset %s's handle %s not valid.\n", dataset_name, handle_s); - return __LINE__; - } + if(!midpoint) + return fprintf(stderr, "Dataset %s's handle %s not valid.\n", dataset_name, handle_s), __LINE__; *midpoint = '\0'; auto parent_key_wide_blob = handle_s; diff --git a/src/tpm1x.hpp b/src/tpm1x.hpp index 060bb81..4a20797 100644 --- a/src/tpm1x.hpp +++ b/src/tpm1x.hpp @@ -80,11 +80,8 @@ int try_policy_or_passphrase(const char * what, const char * what_for, TSS_HPOLI } // TRY_TPM1X() unrolled because no constexpr/string-literal-template arguments until C++20, which is not supported by GCC 8, which we need for Buster - if(err != TPM_SUCCESS) { - fprintf(stderr, "Couldn't %s: %s\n", what, Trspi_Error_String(err)); - return __LINE__; - } - + if(err != TPM_SUCCESS) + return fprintf(stderr, "Couldn't %s: %s\n", what, Trspi_Error_String(err)), __LINE__; return 0; } diff --git a/src/tpm2.cpp b/src/tpm2.cpp index fb6ba76..c5ec618 100644 --- a/src/tpm2.cpp +++ b/src/tpm2.cpp @@ -32,10 +32,8 @@ static int try_or_passphrase(const char * what, const char * what_for, ESYS_CONT } // TRY_TPM2() unrolled because no constexpr/string-literal-template arguments until C++20, which is not supported by GCC 8, which we need for Buster - if(err != TPM2_RC_SUCCESS) { - fprintf(stderr, "Couldn't %s: %s\n", what, Tss2_RC_Decode(err)); - return __LINE__; - } + if(err != TPM2_RC_SUCCESS) + return fprintf(stderr, "Couldn't %s: %s\n", what, Tss2_RC_Decode(err)), __LINE__; return 0; } @@ -58,10 +56,8 @@ TPM2B_DATA tpm2_creation_metadata(const char * dataset_name) { int tpm2_parse_handle(const char * dataset_name, const char * handle_s, TPMI_DH_PERSISTENT & handle) { - if(parse_int(handle_s, handle)) { - fprintf(stderr, "Dataset %s's handle %s not valid.\n", dataset_name, handle_s); - return __LINE__; - } + if(parse_int(handle_s, handle)) + return fprintf(stderr, "Dataset %s's handle %s not valid.\n", dataset_name, handle_s), __LINE__; return 0; } @@ -72,10 +68,8 @@ int tpm2_generate_rand(ESYS_CONTEXT * tpm2_ctx, void * into, size_t length) { TRY_TPM2("get random data from TPM", Esys_GetRandom(tpm2_ctx, ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE, length, &rand)); quickscope_wrapper rand_deleter{[=] { Esys_Free(rand); }}; - if(rand->size != length) { - fprintf(stderr, "Wrong random size: wanted %zu, got %" PRIu16 " bytes.\n", length, rand->size); - return __LINE__; - } + if(rand->size != length) + return fprintf(stderr, "Wrong random size: wanted %zu, got %" PRIu16 " bytes.\n", length, rand->size), __LINE__; memcpy(into, rand->buffer, length); return 0; @@ -103,10 +97,8 @@ static int tpm2_find_unused_persistent_non_platform(ESYS_CONTEXT * tpm2_ctx, TPM } } - if(!persistent_handle) { - fprintf(stderr, "All %zu persistent handles allocated! We're fucked!\n", TPM2_MAX_CAP_HANDLES); - return __LINE__; - } + if(!persistent_handle) + return fprintf(stderr, "All %zu persistent handles allocated! We're fucked!\n", TPM2_MAX_CAP_HANDLES), __LINE__; return 0; } @@ -232,10 +224,8 @@ int tpm2_unseal(ESYS_CONTEXT * tpm2_ctx, ESYS_TR tpm2_session, TPMI_DH_PERSISTEN TRY_MAIN(try_or_passphrase("unseal wrapping key", "wrapping key", tpm2_ctx, TPM2_RC_AUTH_FAIL, pandle, [&] { return Esys_Unseal(tpm2_ctx, pandle, tpm2_session, ESYS_TR_NONE, ESYS_TR_NONE, &unsealed); })); - if(unsealed->size != data_len) { - fprintf(stderr, "Unsealed data has wrong length %" PRIu16 ", expected %zu!\n", unsealed->size, data_len); - return __LINE__; - } + if(unsealed->size != data_len) + return fprintf(stderr, "Unsealed data has wrong length %" PRIu16 ", expected %zu!\n", unsealed->size, data_len), __LINE__; memcpy(data, unsealed->buffer, data_len); return 0; } diff --git a/src/zfs.cpp b/src/zfs.cpp index ffd8656..b84d4aa 100644 --- a/src/zfs.cpp +++ b/src/zfs.cpp @@ -104,9 +104,7 @@ int clear_key_props(zfs_handle_t * from) { TRY("delete tzpfms.backend", zfs_prop_inherit(from, PROPNAME_BACKEND, B_FALSE)); TRY("delete tzpfms.key", zfs_prop_inherit(from, PROPNAME_KEY, B_FALSE)); - - ok = true; - return 0; + return ok = true, 0; } @@ -114,20 +112,14 @@ int parse_key_props(zfs_handle_t * in, const char * our_backend, char *& handle) char * backend{}; TRY_MAIN(lookup_userprop(in, PROPNAME_BACKEND, backend)); - if(!backend) { - fprintf(stderr, "Dataset %s not encrypted with tzpfms!\n", zfs_get_name(in)); - return __LINE__; - } - if(strcmp(backend, our_backend)) { - fprintf(stderr, "Dataset %s encrypted with tzpfms back-end %s, but we are %s.\n", zfs_get_name(in), backend, our_backend); - return __LINE__; - } + if(!backend) + return fprintf(stderr, "Dataset %s not encrypted with tzpfms!\n", zfs_get_name(in)), __LINE__; + if(strcmp(backend, our_backend)) + return fprintf(stderr, "Dataset %s encrypted with tzpfms back-end %s, but we are %s.\n", zfs_get_name(in), backend, our_backend), __LINE__; TRY_MAIN(lookup_userprop(in, PROPNAME_KEY, handle)); - if(!handle) { - fprintf(stderr, "Dataset %s missing key data.\n", zfs_get_name(in)); - return __LINE__; - } + if(!handle) + return fprintf(stderr, "Dataset %s missing key data.\n", zfs_get_name(in)), __LINE__; return 0; } diff --git a/src/zfs.hpp b/src/zfs.hpp index 08b7bab..d935f5f 100644 --- a/src/zfs.hpp +++ b/src/zfs.hpp @@ -23,8 +23,7 @@ #define REQUIRE_KEY_LOADED(dataset) \ do { \ if(zfs_prop_get_int(dataset, ZFS_PROP_KEYSTATUS) == ZFS_KEYSTATUS_UNAVAILABLE) { \ - fprintf(stderr, "Key change error: Key must be loaded.\n"); \ - return __LINE__; \ + return fprintf(stderr, "Key change error: Key must be loaded.\n"), __LINE__; \ } \ } while(0)