JavascriptNet / Javascript.Net

.Net bindings to the V8 JavaScript engine
BSD 2-Clause "Simplified" License
829 stars 150 forks source link

Make sure zero bytes are not discarded when converting strings #79

Closed spahnke closed 5 years ago

spahnke commented 5 years ago

Since C++ uses zero byte terminated strings, a cast to wchar_t* discards all characters after a zero byte when used directly to create a C# or V8 string. This is bad for strings containing a zero byte somewhere. We fix that by using a constructor overload taking the actual length of the string as parameter.

Example:

const text = 'a\0b'
text

would arrive as "a" in C# without the fix and vice versa.

Ablu commented 5 years ago

a cast to wchar_t* discards all characters after a zero byte

Not the cast discards it, but the String constructor without length parameter :)

EDIT: Ok, the comment in the code is right, just the PR description was slightly misleading.