[libc-commits] [libc] [libc][stdlib] Add unsetenv (PR #202422)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jun 11 10:54:51 PDT 2026


================
@@ -0,0 +1,128 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+/// \file
+/// Integration tests for unsetenv.
+
+#include "src/stdlib/getenv.h"
+#include "src/stdlib/setenv.h"
+#include "src/stdlib/unsetenv.h"
+#include "src/string/strcmp.h"
+#include "src/unistd/environ.h"
+
+#include "test/IntegrationTest/test.h"
+
+#include <errno.h>
+
+namespace LIBC_NAMESPACE {
+
+TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
+          [[maybe_unused]] char **envp) {
+  // Test: Remove a variable set by setenv
+  {
+    ASSERT_EQ(setenv("UNSET_VAR", "value", 1), 0);
+    ASSERT_TRUE(getenv("UNSET_VAR") != nullptr);
+
+    ASSERT_EQ(unsetenv("UNSET_VAR"), 0);
+    ASSERT_TRUE(getenv("UNSET_VAR") == nullptr);
+  }
+
+  // Test: Unset non-existent variable succeeds
+  {
+    ASSERT_EQ(unsetenv("DOES_NOT_EXIST"), 0);
+  }
+
+  // Test: Empty name returns EINVAL
----------------
michaelrj-google wrote:

pedantic nit: Technically it doesn't return EINVAL, it sets errno to EINVAL.

https://github.com/llvm/llvm-project/pull/202422


More information about the libc-commits mailing list