authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-22 16:55:26-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-22 17:09:20-08:00
logc0c911bfa78ea2968cd110ad64a4ba0d70ca93e5
treeb2bb3bc307ec55ab0f12c5633b9263574a881031
parented55b2ef1723c29edf53ba332e7cb18c1c738bff

zig fmt: fix invalid alignment on frees


2 files changed, 13 insertions(+), 12 deletions(-)

lib/std/zig.zig+12-12
...@@ -535,16 +535,12 @@ test isUnderscore {...@@ -535,16 +535,12 @@ test isUnderscore {
535 try std.testing.expect(!isUnderscore("\\x5f"));535 try std.testing.expect(!isUnderscore("\\x5f"));
536}536}
537537
538pub fn readSourceFileToEndAlloc(538pub fn readSourceFileToEndAlloc(gpa: Allocator, input: std.fs.File, size_hint: ?usize) ![:0]u8 {
539 allocator: Allocator,
540 input: std.fs.File,
541 size_hint: ?usize,
542) ![:0]u8 {
543 const source_code = input.readToEndAllocOptions(539 const source_code = input.readToEndAllocOptions(
544 allocator,540 gpa,
545 max_src_size,541 max_src_size,
546 size_hint,542 size_hint,
547 @alignOf(u16),543 @alignOf(u8),
548 0,544 0,
549 ) catch |err| switch (err) {545 ) catch |err| switch (err) {
550 error.ConnectionResetByPeer => unreachable,546 error.ConnectionResetByPeer => unreachable,
...@@ -552,7 +548,7 @@ pub fn readSourceFileToEndAlloc(...@@ -552,7 +548,7 @@ pub fn readSourceFileToEndAlloc(
552 error.NotOpenForReading => unreachable,548 error.NotOpenForReading => unreachable,
553 else => |e| return e,549 else => |e| return e,
554 };550 };
555 errdefer allocator.free(source_code);551 errdefer gpa.free(source_code);
556552
557 // Detect unsupported file types with their Byte Order Mark553 // Detect unsupported file types with their Byte Order Mark
558 const unsupported_boms = [_][]const u8{554 const unsupported_boms = [_][]const u8{
...@@ -568,15 +564,19 @@ pub fn readSourceFileToEndAlloc(...@@ -568,15 +564,19 @@ pub fn readSourceFileToEndAlloc(
568564
569 // If the file starts with a UTF-16 little endian BOM, translate it to UTF-8565 // If the file starts with a UTF-16 little endian BOM, translate it to UTF-8
570 if (std.mem.startsWith(u8, source_code, "\xff\xfe")) {566 if (std.mem.startsWith(u8, source_code, "\xff\xfe")) {
571 const source_code_utf16_le = std.mem.bytesAsSlice(u16, source_code);567 if (source_code.len % 2 != 0) return error.InvalidEncoding;
572 const source_code_utf8 = std.unicode.utf16LeToUtf8AllocZ(allocator, source_code_utf16_le) catch |err| switch (err) {568 // TODO: after wrangle-writer-buffering branch is merged,
569 // avoid this unnecessary allocation
570 const aligned_copy = try gpa.alloc(u16, source_code.len / 2);
571 defer gpa.free(aligned_copy);
572 @memcpy(std.mem.sliceAsBytes(aligned_copy), source_code);
573 const source_code_utf8 = std.unicode.utf16LeToUtf8AllocZ(gpa, aligned_copy) catch |err| switch (err) {
573 error.DanglingSurrogateHalf => error.UnsupportedEncoding,574 error.DanglingSurrogateHalf => error.UnsupportedEncoding,
574 error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding,575 error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding,
575 error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding,576 error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding,
576 else => |e| return e,577 else => |e| return e,
577 };578 };
578579 gpa.free(source_code);
579 allocator.free(source_code);
580 return source_code_utf8;580 return source_code_utf8;
581 }581 }
582582
src/fmt.zig+1
...@@ -214,6 +214,7 @@ const FmtError = error{...@@ -214,6 +214,7 @@ const FmtError = error{
214 Unseekable,214 Unseekable,
215 NotOpenForWriting,215 NotOpenForWriting,
216 UnsupportedEncoding,216 UnsupportedEncoding,
217 InvalidEncoding,
217 ConnectionResetByPeer,218 ConnectionResetByPeer,
218 SocketNotConnected,219 SocketNotConnected,
219 LockViolation,220 LockViolation,