diff options
author | 2023-05-26 21:17:10 -0700 | |
---|---|---|
committer | 2023-05-26 21:17:10 -0700 | |
commit | 79907fe84ceb186ba96b4ac7dff4b7ada47c6850 (patch) | |
tree | e5cc51eea6071becd1372597574a320f23646933 /src | |
parent | 1a30b4fe2903186658ef0c70e9c6cdbb5c5bed6b (diff) | |
download | bun-79907fe84ceb186ba96b4ac7dff4b7ada47c6850.tar.gz bun-79907fe84ceb186ba96b4ac7dff4b7ada47c6850.tar.zst bun-79907fe84ceb186ba96b4ac7dff4b7ada47c6850.zip |
Add more debug safety checks
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 99c2306f4..2b569282e 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -432,10 +432,22 @@ pub const ZigString = extern struct { } pub inline fn utf16Slice(this: *const ZigString) []align(1) const u16 { + if (comptime bun.Environment.allow_assert) { + if (this.len > 0 and !this.is16Bit()) { + @panic("ZigString.utf16Slice() called on a latin1 string.\nPlease use .toSlice() instead or carefully check that .is16Bit() is false first."); + } + } + return @ptrCast([*]align(1) const u16, untagged(this.ptr))[0..this.len]; } pub inline fn utf16SliceAligned(this: *const ZigString) []const u16 { + if (comptime bun.Environment.allow_assert) { + if (this.len > 0 and !this.is16Bit()) { + @panic("ZigString.utf16SliceAligned() called on a latin1 string.\nPlease use .toSlice() instead or carefully check that .is16Bit() is false first."); + } + } + return @ptrCast([*]const u16, @alignCast(@alignOf(u16), untagged(this.ptr)))[0..this.len]; } @@ -596,6 +608,12 @@ pub const ZigString = extern struct { } pub fn slice(this: *const ZigString) []const u8 { + if (comptime bun.Environment.allow_assert) { + if (this.len > 0 and this.is16Bit()) { + @panic("ZigString.slice() called on a UTF-16 string.\nPlease use .toSlice() instead or carefully check that .is16Bit() is false first."); + } + } + return untagged(this.ptr)[0..@min(this.len, std.math.maxInt(u32))]; } |