X Tutup
Skip to content

Commit 44ee03d

Browse files
committed
tree-wide: unsetenv cannot fail
... when called with a valid environment variable name. This means that any time we call it with a fixed string, it is guaranteed to return 0. (Also when the variable is not present in the environment block.)
1 parent 063f9f0 commit 44ee03d

File tree

8 files changed

+22
-32
lines changed

8 files changed

+22
-32
lines changed

src/core/main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,9 +1418,8 @@ static int fixup_environment(void) {
14181418
return -errno;
14191419

14201420
/* The kernels sets HOME=/ for init. Let's undo this. */
1421-
if (path_equal_ptr(getenv("HOME"), "/") &&
1422-
unsetenv("HOME") < 0)
1423-
log_warning_errno(errno, "Failed to unset $HOME: %m");
1421+
if (path_equal_ptr(getenv("HOME"), "/"))
1422+
assert_se(unsetenv("HOME") == 0);
14241423

14251424
return 0;
14261425
}

src/home/homectl.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ static int acquire_existing_password(const char *user_name, UserRecord *hr, bool
215215
return log_error_errno(r, "Failed to store password: %m");
216216

217217
string_erase(e);
218-
219-
if (unsetenv("PASSWORD") < 0)
220-
return log_error_errno(errno, "Failed to unset $PASSWORD: %m");
218+
assert_se(unsetenv("PASSWORD") == 0);
221219

222220
return 0;
223221
}
@@ -255,9 +253,7 @@ static int acquire_token_pin(const char *user_name, UserRecord *hr) {
255253
return log_error_errno(r, "Failed to store token PIN: %m");
256254

257255
string_erase(e);
258-
259-
if (unsetenv("PIN") < 0)
260-
return log_error_errno(errno, "Failed to unset $PIN: %m");
256+
assert_se(unsetenv("PIN") == 0);
261257

262258
return 0;
263259
}
@@ -997,9 +993,7 @@ static int acquire_new_password(
997993
return log_error_errno(r, "Failed to store password: %m");
998994

999995
string_erase(e);
1000-
1001-
if (unsetenv("NEWPASSWORD") < 0)
1002-
return log_error_errno(errno, "Failed to unset $NEWPASSWORD: %m");
996+
assert_se(unsetenv("NEWPASSWORD") == 0);
1003997

1004998
if (ret)
1005999
*ret = TAKE_PTR(copy);

src/libsystemd/sd-daemon/sd-daemon.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@
3030
#define SNDBUF_SIZE (8*1024*1024)
3131

3232
static void unsetenv_all(bool unset_environment) {
33-
3433
if (!unset_environment)
3534
return;
3635

37-
unsetenv("LISTEN_PID");
38-
unsetenv("LISTEN_FDS");
39-
unsetenv("LISTEN_FDNAMES");
36+
assert_se(unsetenv("LISTEN_PID") == 0);
37+
assert_se(unsetenv("LISTEN_FDS") == 0);
38+
assert_se(unsetenv("LISTEN_FDNAMES") == 0);
4039
}
4140

4241
_public_ int sd_listen_fds(int unset_environment) {
@@ -548,7 +547,7 @@ _public_ int sd_pid_notify_with_fds(
548547

549548
finish:
550549
if (unset_environment)
551-
unsetenv("NOTIFY_SOCKET");
550+
assert_se(unsetenv("NOTIFY_SOCKET") == 0);
552551

553552
return r;
554553
}
@@ -672,9 +671,9 @@ _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
672671

673672
finish:
674673
if (unset_environment && s)
675-
unsetenv("WATCHDOG_USEC");
674+
assert_se(unsetenv("WATCHDOG_USEC") == 0);
676675
if (unset_environment && p)
677-
unsetenv("WATCHDOG_PID");
676+
assert_se(unsetenv("WATCHDOG_PID") == 0);
678677

679678
return r;
680679
}

src/test/test-execute.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -898,11 +898,11 @@ int main(int argc, char *argv[]) {
898898
}
899899
#endif
900900

901-
(void) unsetenv("USER");
902-
(void) unsetenv("LOGNAME");
903-
(void) unsetenv("SHELL");
904-
(void) unsetenv("HOME");
905-
(void) unsetenv("TMPDIR");
901+
assert_se(unsetenv("USER") == 0);
902+
assert_se(unsetenv("LOGNAME") == 0);
903+
assert_se(unsetenv("SHELL") == 0);
904+
assert_se(unsetenv("HOME") == 0);
905+
assert_se(unsetenv("TMPDIR") == 0);
906906

907907
can_unshare = have_namespaces();
908908

src/test/test-path-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static void test_find_executable_full(void) {
184184
if (p)
185185
assert_se(oldpath = strdup(p));
186186

187-
assert_se(unsetenv("PATH") >= 0);
187+
assert_se(unsetenv("PATH") == 0);
188188

189189
assert_se(find_executable_full("sh", true, &p) == 0);
190190
puts(p);
@@ -347,7 +347,7 @@ static void test_fsck_exists(void) {
347347
log_info("/* %s */", __func__);
348348

349349
/* Ensure we use a sane default for PATH. */
350-
unsetenv("PATH");
350+
assert_se(unsetenv("PATH") == 0);
351351

352352
/* fsck.minix is provided by util-linux and will probably exist. */
353353
assert_se(fsck_exists("minix") == 1);

src/test/test-time-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ static void test_in_utc_timezone(void) {
480480
assert_se(streq(tzname[0], "CET"));
481481
assert_se(streq(tzname[1], "CEST"));
482482

483-
assert_se(unsetenv("TZ") >= 0);
483+
assert_se(unsetenv("TZ") == 0);
484484
}
485485

486486
static void test_map_clock_usec(void) {

src/udev/udevd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device
565565
assert(monitor);
566566
assert(dev);
567567

568-
unsetenv("NOTIFY_SOCKET");
568+
assert_se(unsetenv("NOTIFY_SOCKET") == 0);
569569

570570
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0);
571571

src/userdb/userdbctl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,8 @@ static int run(int argc, char *argv[]) {
780780
return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m");
781781

782782
log_info("Enabled services: %s", e);
783-
} else {
784-
if (unsetenv("SYSTEMD_ONLY_USERDB") < 0)
785-
return log_error_errno(r, "Failed to unset $SYSTEMD_ONLY_USERDB: %m");
786-
}
783+
} else
784+
assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0);
787785

788786
return dispatch_verb(argc, argv, verbs, NULL);
789787
}

0 commit comments

Comments
 (0)
X Tutup