mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 14:25:16 +00:00
Fix crash when freeing empty string as null-terminated sentinel
This commit is contained in:
parent
1091fee242
commit
f5954dad83
@ -1780,10 +1780,11 @@ fn SliceAsBytesReturnType(comptime sliceType: type) type {
|
||||
|
||||
pub fn sliceAsBytes(slice: var) SliceAsBytesReturnType(@TypeOf(slice)) {
|
||||
const actualSlice = if (comptime trait.isPtrTo(.Array)(@TypeOf(slice))) slice[0..] else slice;
|
||||
const actualSliceTypeInfo = @typeInfo(@TypeOf(actualSlice)).Pointer;
|
||||
|
||||
// let's not give an undefined pointer to @ptrCast
|
||||
// it may be equal to zero and fail a null check
|
||||
if (actualSlice.len == 0) {
|
||||
if (actualSlice.len == 0 and actualSliceTypeInfo.sentinel == null) {
|
||||
return &[0]u8{};
|
||||
}
|
||||
|
||||
@ -1805,6 +1806,12 @@ test "sliceAsBytes" {
|
||||
}));
|
||||
}
|
||||
|
||||
test "sliceAsBytes with sentinel slice" {
|
||||
const empty_string:[:0]const u8 = "";
|
||||
const bytes = sliceAsBytes(empty_string);
|
||||
testing.expect(bytes.len == 0);
|
||||
}
|
||||
|
||||
test "sliceAsBytes packed struct at runtime and comptime" {
|
||||
const Foo = packed struct {
|
||||
a: u4,
|
||||
@ -1941,3 +1948,8 @@ test "isAligned" {
|
||||
testing.expect(!isAligned(4, 8));
|
||||
testing.expect(!isAligned(4, 16));
|
||||
}
|
||||
|
||||
test "freeing empty string with null-terminated sentinel" {
|
||||
const empty_string = try dupeZ(testing.allocator, u8, "");
|
||||
testing.allocator.free(empty_string);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user