[libc-commits] [libc] [llvm] [libc] Use CheckedPtr in environ_internal (PR #241777)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Thu Nov 12 02:46:18 PST 2026
https://github.com/labath created https://github.com/llvm/llvm-project/pull/241777
Replace the manual AllocChecker allocations with make_unique and friends. The allocations are only held in CheckedPtrs locally, and released into the raw member pointers once nothing can fail anymore. I'm deliberately not making the members themselves unique_ptrs, as that would make the EnvironmentManager singleton non-trivially destructible.
Assisted-by: Claude
>From 5a2ca85be5f0f1deada187aee5a01c27d4c7ca1a Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Thu, 12 Nov 2026 10:40:53 +0000
Subject: [PATCH] [libc] Use CheckedPtr in environ_internal
Replace the manual AllocChecker allocations with make_unique and
friends. The allocations are only held in CheckedPtrs locally, and
released into the raw member pointers once nothing can fail anymore.
I'm deliberately not making the members themselves unique_ptrs, as that
would make the EnvironmentManager singleton non-trivially destructible.
Assisted-by: Claude
---
libc/src/stdlib/CMakeLists.txt | 2 +-
libc/src/stdlib/environ_internal.cpp | 118 ++++++------------
libc/src/stdlib/environ_internal.h | 15 ---
.../llvm-project-overlay/libc/BUILD.bazel | 2 +-
4 files changed, 40 insertions(+), 97 deletions(-)
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 98e356244e87a5..f98912368200c4 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -91,7 +91,7 @@ endif()
set(environ_internal_deps
libc.config.app_h
libc.hdr.types.size_t
- libc.src.__support.alloc_checker
+ libc.src.__support.checked_ptr
libc.src.__support.CPP.new
libc.src.__support.CPP.optional
libc.src.__support.CPP.string_view
diff --git a/libc/src/stdlib/environ_internal.cpp b/libc/src/stdlib/environ_internal.cpp
index 43159cada19042..a55b2f1f67c379 100644
--- a/libc/src/stdlib/environ_internal.cpp
+++ b/libc/src/stdlib/environ_internal.cpp
@@ -15,7 +15,7 @@
#include "config/app.h"
#include "src/__support/CPP/new.h"
#include "src/__support/CPP/string_view.h"
-#include "src/__support/alloc-checker.h"
+#include "src/__support/checked_ptr.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/memory_utils/inline_strcmp.h"
@@ -108,85 +108,44 @@ cpp::optional<size_t> EnvironmentManager::find_var(cpp::string_view name) {
return cpp::nullopt;
}
-// Helper: allocate new storage and ownership arrays of the given capacity,
-// copy the first `copy_count` entries from old_storage/old_ownership, and
-// initialize the remaining ownership slots to default (not-owned).
-// Returns nullopt on allocation failure; the old arrays are untouched.
-cpp::optional<EnvironmentManager::AllocResult>
-EnvironmentManager::alloc_and_copy(size_t new_capacity, char **old_storage,
- EnvStringOwnership *old_ownership,
- size_t copy_count) {
- AllocChecker ac;
- char **new_storage = new (ac) char *[new_capacity + 1];
- if (!ac)
- return cpp::nullopt;
-
- EnvStringOwnership *new_ownership =
- new (ac) EnvStringOwnership[new_capacity + 1];
- if (!ac) {
- delete[] new_storage;
- return cpp::nullopt;
- }
-
- for (size_t i = 0; i < copy_count; i++) {
- new_storage[i] = old_storage ? old_storage[i] : nullptr;
- new_ownership[i] = old_ownership ? old_ownership[i] : EnvStringOwnership();
- }
- new_storage[copy_count] = nullptr;
-
- return AllocResult{new_storage, new_ownership};
-}
-
bool EnvironmentManager::ensure_capacity(size_t needed) {
- // If we're still using the startup environ (pointed to by app.env_ptr),
- // we must transition to our own managed storage. This allows us to
- // track ownership of strings and safely expand the array.
- if (!is_ours) {
- char **old_env = reinterpret_cast<char **>(app.env_ptr);
-
- // Allocate new array with room to grow.
- size_t new_capacity = needed < MIN_ENVIRON_CAPACITY
- ? MIN_ENVIRON_CAPACITY
- : needed * ENVIRON_GROWTH_FACTOR;
-
- auto result = alloc_and_copy(new_capacity, old_env, nullptr, count);
- if (!result)
- return false;
-
- auto [new_storage, new_ownership] = *result;
- storage = new_storage;
- ownership = new_ownership;
- capacity = new_capacity;
- is_ours = true;
-
- // Update the global environ pointer.
- app.env_ptr = reinterpret_cast<uintptr_t *>(storage);
-#ifdef LIBC_COPT_SUPPORT_ENVIRON
- environ = storage;
-#endif
-
+ // Nothing to do if we already own an array that is large enough.
+ if (is_ours && needed <= capacity)
return true;
- }
-
- // We already own the environment array. Check if it's large enough.
- if (needed <= capacity)
- return true;
-
- // Grow capacity. We avoid realloc to ensure that failures don't leave the
- // manager in an inconsistent state.
- size_t new_capacity = needed * ENVIRON_GROWTH_FACTOR;
- auto result = alloc_and_copy(new_capacity, storage, ownership, count);
- if (!result)
+ // Either transition from the startup environ (pointed to by app.env_ptr) to
+ // our own managed storage, which allows us to track ownership of strings and
+ // safely expand the array, or grow the array we already own. We avoid
+ // realloc to ensure that failures don't leave the manager in an inconsistent
+ // state.
+ size_t new_capacity = (!is_ours && needed < MIN_ENVIRON_CAPACITY)
+ ? MIN_ENVIRON_CAPACITY
+ : needed * ENVIRON_GROWTH_FACTOR;
+
+ auto new_storage = make_unique_for_overwrite<char *[]>(new_capacity + 1);
+ if (!new_storage)
+ return false;
+ // All ownership entries start out as not-owned.
+ auto new_ownership = make_unique<EnvStringOwnership[]>(new_capacity + 1);
+ if (!new_ownership)
return false;
- delete[] storage;
- delete[] ownership;
+ char **old_storage = get_array();
+ for (size_t i = 0; i < count; i++) {
+ new_storage[i] = old_storage[i];
+ if (is_ours)
+ new_ownership[i] = ownership[i];
+ }
+ new_storage[count] = nullptr;
- auto [new_storage, new_ownership] = *result;
- storage = new_storage;
- ownership = new_ownership;
+ if (is_ours) {
+ delete[] storage;
+ delete[] ownership;
+ }
+ storage = new_storage.release();
+ ownership = new_ownership.release();
capacity = new_capacity;
+ is_ours = true;
// Update the global environ pointer.
app.env_ptr = reinterpret_cast<uintptr_t *>(storage);
@@ -216,14 +175,13 @@ int EnvironmentManager::set(cpp::string_view name, cpp::string_view value,
size_t value_len = value.size();
size_t total_len = name_len + 1 + value_len + 1; // name + '=' + value + '\0'
- AllocChecker ac;
- char *new_string = new (ac) char[total_len];
- if (!ac)
+ auto new_string = make_unique_for_overwrite<char[]>(total_len);
+ if (!new_string)
return -1;
- inline_memcpy(new_string, name.data(), name_len);
+ inline_memcpy(new_string.get(), name.data(), name_len);
new_string[name_len] = '=';
- inline_memcpy(new_string + name_len + 1, value.data(), value_len);
+ inline_memcpy(new_string.get() + name_len + 1, value.data(), value_len);
new_string[name_len + 1 + value_len] = '\0';
char **env_array = get_array();
@@ -233,11 +191,11 @@ int EnvironmentManager::set(cpp::string_view name, cpp::string_view value,
if (ownership[*idx].can_free())
delete[] env_array[*idx];
- env_array[*idx] = new_string;
+ env_array[*idx] = new_string.release();
ownership[*idx].allocated_by_us = true;
} else {
// Add new variable at the end.
- env_array[count] = new_string;
+ env_array[count] = new_string.release();
ownership[count].allocated_by_us = true;
count++;
env_array[count] = nullptr; // Maintain null terminator.
diff --git a/libc/src/stdlib/environ_internal.h b/libc/src/stdlib/environ_internal.h
index f39726d5ba9c77..40d2bee9ddba6d 100644
--- a/libc/src/stdlib/environ_internal.h
+++ b/libc/src/stdlib/environ_internal.h
@@ -88,21 +88,6 @@ class EnvironmentManager {
// success, false on allocation failure.
bool ensure_capacity(size_t needed);
- // Result of a successful environment storage allocation.
- struct AllocResult {
- char **storage;
- EnvStringOwnership *ownership;
- };
-
- // Helper: allocate new storage and ownership arrays of the given capacity,
- // copy the first `copy_count` entries from old_storage/old_ownership, and
- // initialize the remaining ownership slots to default (not-owned).
- // Returns nullopt on allocation failure; the old arrays are untouched.
- cpp::optional<AllocResult> alloc_and_copy(size_t new_capacity,
- char **old_storage,
- EnvStringOwnership *old_ownership,
- size_t copy_count);
-
public:
// Get the singleton instance of the environment manager.
static EnvironmentManager &get_instance();
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 997e85b2fa20b4..2ff444c6b91790 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -17540,7 +17540,7 @@ libc_support_library(
srcs = ["src/stdlib/environ_internal.cpp"],
hdrs = ["src/stdlib/environ_internal.h"],
deps = [
- ":__support_alloc_checker",
+ ":__support_checked_ptr",
":__support_common",
":__support_cpp_new",
":__support_cpp_optional",
More information about the libc-commits
mailing list