Skip to content

Remove warning from docs for withUnsafeTemporaryAllocation() about overallocation.#89467

Open
grynspan wants to merge 1 commit into
mainfrom
jgrynspan/withunsafetemporaryallocation-scary-warning
Open

Remove warning from docs for withUnsafeTemporaryAllocation() about overallocation.#89467
grynspan wants to merge 1 commit into
mainfrom
jgrynspan/withunsafetemporaryallocation-scary-warning

Conversation

@grynspan
Copy link
Copy Markdown
Contributor

The markup for the two overloads of withUnsafeTemporaryAllocation() states:

The implementation may allocate a larger buffer pointer than is strictly
necessary to contain capacity values of type type. The behavior of a
program that attempts to access any such additional storage is undefined.

This text is meant to indicate that the compiler, runtime, OS, etc. are free to allocate a larger buffer, which may occur e.g. if you ask for 1 byte and the generated assembly pushes SP up by 4/8 instead, or if you ask for 1025 bytes and the OS's malloc() gives you a 4KB/16KB page back. It does not imply that the buffer created and passed to body has a bad count value or is otherwise unsound.

This text has proven confusing as it sounds scarier than it is meant to be. It is just meant to document that the system can and does overallocate. I'm just going to remove it; there is no way for a well-formed program to access any such excess bytes, and this API is already marked unsafe so if you decide to read off the end of the buffer, you've sealed your own fate anyway.

…overallocation.

The markup for the two overloads of `withUnsafeTemporaryAllocation()` states:

> The implementation may allocate a larger buffer pointer than is strictly
> necessary to contain `capacity` values of type `type`. The behavior of a
> program that attempts to access any such additional storage is undefined.

This text is meant to indicate that the compiler, runtime, OS, etc. are free to
allocate a larger buffer, which may occur e.g. if you ask for 1 byte and the
generated assembly pushes `SP` up by 4/8 instead, or if you ask for 1025 bytes
and the OS's `malloc()` gives you a 4KB/16KB page back. It does not imply that
the buffer created and passed to `body` has a bad `count` value or is otherwise
unsound.

This text has proven confusing as it sounds scarier than it is meant to be. It
is just meant to document that the system can and does overallocate. I'm just
going to remove it; there is no way for a well-formed program to access any such
excess bytes, and this API is already marked unsafe so if you decide to read off
the end of the buffer, you've sealed your own fate anyway.
@grynspan grynspan self-assigned this May 27, 2026
@grynspan grynspan requested a review from a team as a code owner May 27, 2026 13:55
@grynspan grynspan added documentation memory safety Feature: memory safety manual memory management Area → standard library: Manual memory management APIs labels May 27, 2026
@grynspan
Copy link
Copy Markdown
Contributor Author

@swift-ci smoke test

@grynspan
Copy link
Copy Markdown
Contributor Author

@swift-ci test Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation manual memory management Area → standard library: Manual memory management APIs memory safety Feature: memory safety

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant