mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
zig fmt: fix invalid alignment on frees
This commit is contained in:
parent
ed55b2ef17
commit
c0c911bfa7
@ -535,16 +535,12 @@ test isUnderscore {
|
|||||||
try std.testing.expect(!isUnderscore("\\x5f"));
|
try std.testing.expect(!isUnderscore("\\x5f"));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn readSourceFileToEndAlloc(
|
pub fn readSourceFileToEndAlloc(gpa: Allocator, input: std.fs.File, size_hint: ?usize) ![:0]u8 {
|
||||||
allocator: Allocator,
|
|
||||||
input: std.fs.File,
|
|
||||||
size_hint: ?usize,
|
|
||||||
) ![:0]u8 {
|
|
||||||
const source_code = input.readToEndAllocOptions(
|
const source_code = input.readToEndAllocOptions(
|
||||||
allocator,
|
gpa,
|
||||||
max_src_size,
|
max_src_size,
|
||||||
size_hint,
|
size_hint,
|
||||||
@alignOf(u16),
|
@alignOf(u8),
|
||||||
0,
|
0,
|
||||||
) catch |err| switch (err) {
|
) catch |err| switch (err) {
|
||||||
error.ConnectionResetByPeer => unreachable,
|
error.ConnectionResetByPeer => unreachable,
|
||||||
@ -552,7 +548,7 @@ pub fn readSourceFileToEndAlloc(
|
|||||||
error.NotOpenForReading => unreachable,
|
error.NotOpenForReading => unreachable,
|
||||||
else => |e| return e,
|
else => |e| return e,
|
||||||
};
|
};
|
||||||
errdefer allocator.free(source_code);
|
errdefer gpa.free(source_code);
|
||||||
|
|
||||||
// Detect unsupported file types with their Byte Order Mark
|
// Detect unsupported file types with their Byte Order Mark
|
||||||
const unsupported_boms = [_][]const u8{
|
const unsupported_boms = [_][]const u8{
|
||||||
@ -568,15 +564,19 @@ pub fn readSourceFileToEndAlloc(
|
|||||||
|
|
||||||
// If the file starts with a UTF-16 little endian BOM, translate it to UTF-8
|
// If the file starts with a UTF-16 little endian BOM, translate it to UTF-8
|
||||||
if (std.mem.startsWith(u8, source_code, "\xff\xfe")) {
|
if (std.mem.startsWith(u8, source_code, "\xff\xfe")) {
|
||||||
const source_code_utf16_le = std.mem.bytesAsSlice(u16, source_code);
|
if (source_code.len % 2 != 0) return error.InvalidEncoding;
|
||||||
const source_code_utf8 = std.unicode.utf16LeToUtf8AllocZ(allocator, source_code_utf16_le) catch |err| switch (err) {
|
// TODO: after wrangle-writer-buffering branch is merged,
|
||||||
|
// avoid this unnecessary allocation
|
||||||
|
const aligned_copy = try gpa.alloc(u16, source_code.len / 2);
|
||||||
|
defer gpa.free(aligned_copy);
|
||||||
|
@memcpy(std.mem.sliceAsBytes(aligned_copy), source_code);
|
||||||
|
const source_code_utf8 = std.unicode.utf16LeToUtf8AllocZ(gpa, aligned_copy) catch |err| switch (err) {
|
||||||
error.DanglingSurrogateHalf => error.UnsupportedEncoding,
|
error.DanglingSurrogateHalf => error.UnsupportedEncoding,
|
||||||
error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding,
|
error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding,
|
||||||
error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding,
|
error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding,
|
||||||
else => |e| return e,
|
else => |e| return e,
|
||||||
};
|
};
|
||||||
|
gpa.free(source_code);
|
||||||
allocator.free(source_code);
|
|
||||||
return source_code_utf8;
|
return source_code_utf8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -214,6 +214,7 @@ const FmtError = error{
|
|||||||
Unseekable,
|
Unseekable,
|
||||||
NotOpenForWriting,
|
NotOpenForWriting,
|
||||||
UnsupportedEncoding,
|
UnsupportedEncoding,
|
||||||
|
InvalidEncoding,
|
||||||
ConnectionResetByPeer,
|
ConnectionResetByPeer,
|
||||||
SocketNotConnected,
|
SocketNotConnected,
|
||||||
LockViolation,
|
LockViolation,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user