env-util: replace unsetenv_erase() by new getenv_steal_erase() helper

The new helper combines a bunch of steps every invocation of
unsetenv_erase() did so far: getenv() + strdup() + unsetenv_erase().
Let's unify this into one helper that is harder to use incorrectly. It's
in inspired by TAKE_PTR() in a way: get the env var out and invalidate
where it was before.
This commit is contained in:
Lennart Poettering
2022-02-20 12:38:06 +09:00
committed by Yu Watanabe
parent 5cf84d2545
commit e99ca14741
9 changed files with 91 additions and 85 deletions
+7 -8
View File
@@ -818,20 +818,19 @@ static bool libcryptsetup_plugins_support(void) {
#if HAVE_LIBCRYPTSETUP_PLUGINS
static int acquire_pins_from_env_variable(char ***ret_pins) {
char *e;
_cleanup_(erase_and_freep) char *envpin = NULL;
_cleanup_strv_free_erase_ char **pins = NULL;
int r;
assert(ret_pins);
e = getenv("PIN");
if (e) {
pins = strv_new(e);
r = getenv_steal_erase("PIN", &envpin);
if (r < 0)
return log_error_errno(r, "Failed to acquire PIN from environment: %m");
if (r > 0) {
pins = strv_new(envpin);
if (!pins)
return log_oom();
string_erase(e);
if (unsetenv("PIN") < 0)
return log_error_errno(errno, "Failed to unset $PIN: %m");
}
*ret_pins = TAKE_PTR(pins);