diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-10-22 16:11:41 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-10-22 16:15:46 -0400 |
commit | fb4650aa346a57da3d20525ca2a3124099a1a307 (patch) | |
tree | ce7365ba81a89f676a3435138340fe24b6ea546c /src/cryptsetup/cryptsetup.c | |
parent | d7f69e16f1a5b84e9acf1771a9b53da3787ae79d (diff) |
tree-wide: use startswith return value to avoid hardcoded offset
I think it's an antipattern to have to count the number of bytes in
the prefix by hand. We should do this automatically to avoid wasting
programmer time, and possible errors. I didn't any offsets that were
wrong, so this change is mostly to make future development easier.
Diffstat (limited to 'src/cryptsetup/cryptsetup.c')
-rw-r--r-- | src/cryptsetup/cryptsetup.c | 105 |
1 files changed, 51 insertions, 54 deletions
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 9927621ea0..1e17fbbb03 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -68,26 +68,25 @@ static usec_t arg_timeout = 0; */ static int parse_one_option(const char *option) { + const char *val; + int r; + assert(option); /* Handled outside of this tool */ if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail")) return 0; - if (startswith(option, "cipher=")) { - char *t; - - t = strdup(option+7); - if (!t) + if ((val = startswith(option, "cipher="))) { + r = free_and_strdup(&arg_cipher, val); + if (r < 0) return log_oom(); - free(arg_cipher); - arg_cipher = t; - - } else if (startswith(option, "size=")) { + } else if ((val = startswith(option, "size="))) { - if (safe_atou(option+5, &arg_key_size) < 0) { - log_error("size= parse failure, ignoring."); + r = safe_atou(val, &arg_key_size); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } @@ -98,68 +97,67 @@ static int parse_one_option(const char *option) { arg_key_size /= 8; - } else if (startswith(option, "key-slot=")) { + } else if ((val = startswith(option, "key-slot="))) { arg_type = CRYPT_LUKS1; - if (safe_atoi(option+9, &arg_key_slot) < 0) { - log_error("key-slot= parse failure, ignoring."); + r = safe_atoi(val, &arg_key_slot); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "tcrypt-keyfile=")) { + } else if ((val = startswith(option, "tcrypt-keyfile="))) { arg_type = CRYPT_TCRYPT; - if (path_is_absolute(option+15)) { - if (strv_extend(&arg_tcrypt_keyfiles, option + 15) < 0) + if (path_is_absolute(val)) { + if (strv_extend(&arg_tcrypt_keyfiles, val) < 0) return log_oom(); } else - log_error("Key file path '%s' is not absolute. Ignoring.", option+15); + log_error("Key file path \"%s\" is not absolute. Ignoring.", val); - } else if (startswith(option, "keyfile-size=")) { + } else if ((val = startswith(option, "keyfile-size="))) { - if (safe_atou(option+13, &arg_keyfile_size) < 0) { - log_error("keyfile-size= parse failure, ignoring."); + r = safe_atou(val, &arg_keyfile_size); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "keyfile-offset=")) { + } else if ((val = startswith(option, "keyfile-offset="))) { - if (safe_atou(option+15, &arg_keyfile_offset) < 0) { - log_error("keyfile-offset= parse failure, ignoring."); + r = safe_atou(val, &arg_keyfile_offset); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "hash=")) { - char *t; - - t = strdup(option+5); - if (!t) + } else if ((val = startswith(option, "hash="))) { + r = free_and_strdup(&arg_hash, val); + if (r < 0) return log_oom(); - free(arg_hash); - arg_hash = t; - - } else if (startswith(option, "header=")) { + } else if ((val = startswith(option, "header="))) { arg_type = CRYPT_LUKS1; - if (!path_is_absolute(option+7)) { - log_error("Header path '%s' is not absolute, refusing.", option+7); + if (!path_is_absolute(val)) { + log_error("Header path \"%s\" is not absolute, refusing.", val); return -EINVAL; } if (arg_header) { - log_error("Duplicate header= options, refusing."); + log_error("Duplicate header= option, refusing."); return -EINVAL; } - arg_header = strdup(option+7); + arg_header = strdup(val); if (!arg_header) return log_oom(); - } else if (startswith(option, "tries=")) { + } else if ((val = startswith(option, "tries="))) { - if (safe_atou(option+6, &arg_tries) < 0) { - log_error("tries= parse failure, ignoring."); + r = safe_atou(val, &arg_tries); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } @@ -181,29 +179,28 @@ static int parse_one_option(const char *option) { arg_tcrypt_system = true; } else if (STR_IN_SET(option, "plain", "swap", "tmp")) arg_type = CRYPT_PLAIN; - else if (startswith(option, "timeout=")) { + else if ((val = startswith(option, "timeout="))) { - if (parse_sec(option+8, &arg_timeout) < 0) { - log_error("timeout= parse failure, ignoring."); + r = parse_sec(val, &arg_timeout); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "offset=")) { + } else if ((val = startswith(option, "offset="))) { - if (safe_atou64(option+7, &arg_offset) < 0) { - log_error("offset= parse failure, refusing."); - return -EINVAL; - } + r = safe_atou64(val, &arg_offset); + if (r < 0) + return log_error_errno(r, "Failed to parse %s: %m", option); - } else if (startswith(option, "skip=")) { + } else if ((val = startswith(option, "skip="))) { - if (safe_atou64(option+5, &arg_skip) < 0) { - log_error("skip= parse failure, refusing."); - return -EINVAL; - } + r = safe_atou64(val, &arg_skip); + if (r < 0) + return log_error_errno(r, "Failed to parse %s: %m", option); } else if (!streq(option, "none")) - log_error("Encountered unknown /etc/crypttab option '%s', ignoring.", option); + log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option); return 0; } |