[libc-commits] [libc] [libc][stdlib] Add putenv (PR #208339)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Thu Jul 9 00:00:32 PDT 2026


================
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Implementation of the POSIX putenv function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/putenv.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/stdlib/environ_internal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, putenv, (char *string)) {
+  LIBC_CRASH_ON_NULLPTR(string);
+
+  cpp::string_view sv(string);
+  size_t eq_pos = sv.find_first_of('=');
+
+  // Defense in depth: Reject empty variable names.
+  // - eq_pos == 0: e.g. "=value" (empty name)
+  // - eq_pos == cpp::string_view::npos && sv.empty(): e.g. "" (attempting to
+  // unset empty name)
+  if (eq_pos == 0 || (eq_pos == cpp::string_view::npos && sv.empty())) {
+    libc_errno = EINVAL;
+    return -1;
+  }
----------------
labath wrote:

I think this can be (avoids scanning the string twice)
```
  if (sv.empty() /*see the big comment on that */ || sv[0] == '=') {
    libc_errno = EINVAL;
    return -1;
  }
```

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


More information about the libc-commits mailing list