[libc-commits] [libc] [libc] Copy the path in posix_spawn_file_actions_addopen (PR #239224)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Thu Nov 5 20:56:37 PST 2026
================
@@ -42,14 +65,41 @@ struct BaseSpawnFileAction {
};
struct SpawnFileOpenAction : public BaseSpawnFileAction {
- const char *path;
int fd;
int oflag;
mode_t mode;
+ // A copy of the path passed to posix_spawn_file_actions_addopen. It is
+ // allocated together with the action (see create()).
+ char path[];
+
+ /// Allocates an open action, together with a copy of `p`. Returns a null
+ /// CheckedPtr if the allocation fails.
+ LIBC_INLINE static CheckedPtr<SpawnFileOpenAction>
+ create(const char *p, int fdesc, int flags, mode_t m) {
+ size_t path_size = internal::string_length(p) + 1;
+ // As in make_unique, there is no need to inspect `ac`: operator new is
+ // noexcept, so if it fails, no object is constructed and the
+ // new-expression evaluates to nullptr.
+ AllocChecker ac;
+ return CheckedPtr<SpawnFileOpenAction>(
+ new (ac, path_size) SpawnFileOpenAction(p, path_size, fdesc, flags, m));
+ }
- SpawnFileOpenAction(const char *p, int fdesc, int flags, mode_t m)
- : BaseSpawnFileAction(BaseSpawnFileAction::OPEN), path(p), fd(fdesc),
- oflag(flags), mode(m) {}
+ /// Allocates storage for the object, followed by `path_size` bytes for the
+ /// path. Declaring this also hides the global operator new, which would not
+ /// leave room for the path.
+ LIBC_INLINE static void *operator new(size_t size, AllocChecker &ac,
+ size_t path_size) noexcept {
+ return ::operator new(size + path_size, ac);
+ }
----------------
SchrodingerZhu wrote:
I guess the purpose is to allow syntax like `new (ac, path_size) SpawnFileOpenAction`
https://github.com/llvm/llvm-project/pull/239224
More information about the libc-commits
mailing list