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

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Jun 9 01:52:21 PDT 2026


================
@@ -221,5 +230,32 @@ int EnvironmentManager::set(cpp::string_view name, cpp::string_view value,
   return 0;
 }
 
+int EnvironmentManager::unset(cpp::string_view name) {
+  cpp::optional<size_t> idx = find_var(name);
+  if (!idx)
+    return 0; // Variable not found; POSIX defines this as success.
+
+  // Transition to managed storage so we can modify the array and track
+  // ownership correctly.
+  if (!ensure_capacity(count))
+    return -1;
+
+  char **env_array = get_array();
+
+  // Free the string if we allocated it.
+  if (ownership[*idx].can_free())
+    delete[] env_array[*idx];
+
+  // Compact: shift remaining entries left to fill the gap.
----------------
labath wrote:

An obvious alternative is to just move the last element in place of the one we've just deleted.

I can see how that might not be desirable (to preserve the relative order in the `environ` pointer), but if that's the goal, it would good to mention that.

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


More information about the libc-commits mailing list