Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String builder to_cstring is unsafe #4890

Open
andreas-jonsson opened this issue Feb 27, 2025 · 3 comments · May be fixed by #4891
Open

String builder to_cstring is unsafe #4890

andreas-jonsson opened this issue Feb 27, 2025 · 3 comments · May be fixed by #4891

Comments

@andreas-jonsson
Copy link
Contributor

Context

to_cstring in string builder is unsafe and there is no real indication that it is.

Expected Behavior

Ensures that string is always terminated or returns an error or panics.
Alternatively is renamed to, to_unsafe_cstring

Current Behavior

Worst case can provide unterminated string without the user knowing.

@laytan
Copy link
Collaborator

laytan commented Feb 27, 2025

What do you mean unsafe?

It returns the contents as a valid and safe cstring, granted you don't write new stuff to it afterwards.

@andreas-jonsson
Copy link
Contributor Author

andreas-jonsson commented Feb 27, 2025

Not if the backing buffer is full right?
It seems to be that it can fail to write the terminator in that case.

@laytan
Copy link
Collaborator

laytan commented Feb 27, 2025

Ok yes, if the buffer is full, and append fails due to an allocation error, you indeed won't know it failed.

Feoramund added a commit to Feoramund/Odin that referenced this issue Feb 27, 2025
`strings.to_cstring` previously would not check if the buffer could
handle the extra null byte and could lead to segmentation violations
when using the resulting string in an API expecting the terminator.
Feoramund added a commit to Feoramund/Odin that referenced this issue Feb 27, 2025
`strings.to_cstring` previously would not check if the buffer could
handle the extra null byte and could lead to segmentation violations
when using the resulting string in an API expecting the terminator.
@Feoramund Feoramund linked a pull request Feb 28, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants