Skip to content

Add null_terminated parameter to String.new(Bytes) and .from_utf16 #15887

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

HertzDevil
Copy link
Contributor

It is common to find C strings in StaticArrays or Slices that are null-terminated, but may also take up the entire slice if its size is equal to the slice's size. In this case String.new(UInt8*) is unsafe, whereas String.new(Bytes) followed by rstrip('\0') is not very efficient. This PR allows the Bytes constructor to stop as soon as it encounters a null character.

LibC::SockaddrUn#sun_path is not necessarily null-terminated in practice, according to https://man7.org/linux/man-pages/man7/unix.7.html. LibC::Dirent#d_name is always null-terminated (in fact, it is declared as a variable-length array on Solaris).

There are probably more places where this is applicable to Win32 code than the ones included here.

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

Successfully merging this pull request may close these issues.

1 participant