Skip to content

Commit c0c911b

Browse files
committed
zig fmt: fix invalid alignment on frees
1 parent ed55b2e commit c0c911b

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

lib/std/zig.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -535,24 +535,20 @@ test isUnderscore {
535535
try std.testing.expect(!isUnderscore("\\x5f"));
536536
}
537537

538-
pub fn readSourceFileToEndAlloc(
539-
allocator: Allocator,
540-
input: std.fs.File,
541-
size_hint: ?usize,
542-
) ![:0]u8 {
538+
pub fn readSourceFileToEndAlloc(gpa: Allocator, input: std.fs.File, size_hint: ?usize) ![:0]u8 {
543539
const source_code = input.readToEndAllocOptions(
544-
allocator,
540+
gpa,
545541
max_src_size,
546542
size_hint,
547-
@alignOf(u16),
543+
@alignOf(u8),
548544
0,
549545
) catch |err| switch (err) {
550546
error.ConnectionResetByPeer => unreachable,
551547
error.ConnectionTimedOut => unreachable,
552548
error.NotOpenForReading => unreachable,
553549
else => |e| return e,
554550
};
555-
errdefer allocator.free(source_code);
551+
errdefer gpa.free(source_code);
556552

557553
// Detect unsupported file types with their Byte Order Mark
558554
const unsupported_boms = [_][]const u8{
@@ -568,15 +564,19 @@ pub fn readSourceFileToEndAlloc(
568564

569565
// If the file starts with a UTF-16 little endian BOM, translate it to UTF-8
570566
if (std.mem.startsWith(u8, source_code, "\xff\xfe")) {
571-
const source_code_utf16_le = std.mem.bytesAsSlice(u16, source_code);
572-
const source_code_utf8 = std.unicode.utf16LeToUtf8AllocZ(allocator, source_code_utf16_le) catch |err| switch (err) {
567+
if (source_code.len % 2 != 0) return error.InvalidEncoding;
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) {
573574
error.DanglingSurrogateHalf => error.UnsupportedEncoding,
574575
error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding,
575576
error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding,
576577
else => |e| return e,
577578
};
578-
579-
allocator.free(source_code);
579+
gpa.free(source_code);
580580
return source_code_utf8;
581581
}
582582

src/fmt.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ const FmtError = error{
214214
Unseekable,
215215
NotOpenForWriting,
216216
UnsupportedEncoding,
217+
InvalidEncoding,
217218
ConnectionResetByPeer,
218219
SocketNotConnected,
219220
LockViolation,

0 commit comments

Comments
 (0)