From c701156ce65327312ecd8755fe151c83a527f338 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:56 -0300 Subject: [PATCH] fix: resolve use-afer-free in backup.zig This worked fine on stage1, and works fine in debug in stage3. However, stage3 ReleaseSafe would panic due to what I assume must have been an undefined behaviour optimization. While I'm happy that I was quickly made aware of the issue thanks to the safety checks in ReleaseSafe I do wish that this issue showed itself in Debug, since I *am* using the GPA --- src/Gui.zig | 1 - src/core/bus/backup.zig | 22 +++++++++++----------- src/core/util.zig | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Gui.zig b/src/Gui.zig index 9740240..ec29e8b 100644 --- a/src/Gui.zig +++ b/src/Gui.zig @@ -11,7 +11,6 @@ const pitch = @import("core/ppu.zig").framebuf_pitch; const scale = @import("core/emu.zig").win_scale; const emu = @import("core/emu.zig"); -const asString = @import("core/util.zig").asString; const log = std.log.scoped(.GUI); const default_title: []const u8 = "ZBA"; diff --git a/src/core/bus/backup.zig b/src/core/bus/backup.zig index 5015d12..49ad542 100644 --- a/src/core/bus/backup.zig +++ b/src/core/bus/backup.zig @@ -3,7 +3,7 @@ const Allocator = std.mem.Allocator; const log = std.log.scoped(.Backup); const escape = @import("../util.zig").escape; -const asString = @import("../util.zig").asString; +const asStringSlice = @import("../util.zig").asStringSlice; const backup_kinds = [5]Needle{ .{ .str = "EEPROM_V", .kind = .Eeprom }, @@ -72,7 +72,7 @@ pub const Backup = struct { } fn loadSaveFromDisk(self: *Self, path: []const u8) !void { - const file_path = try self.getSaveFilePath(path); + const file_path = try self.getSaveFilePath(self.alloc, path); defer self.alloc.free(file_path); // FIXME: Don't rely on this lol @@ -111,22 +111,22 @@ pub const Backup = struct { } } - fn getSaveFilePath(self: *const Self, path: []const u8) ![]const u8 { - const filename = try self.getSaveFilename(); - defer self.alloc.free(filename); + fn getSaveFilePath(self: *const Self, allocator: Allocator, path: []const u8) ![]const u8 { + const filename = try self.getSaveFilename(allocator); + defer allocator.free(filename); - return try std.fs.path.join(self.alloc, &[_][]const u8{ path, filename }); + return try std.fs.path.join(allocator, &[_][]const u8{ path, filename }); } - fn getSaveFilename(self: *const Self) ![]const u8 { - const title = asString(escape(self.title)); - const name = if (title.len != 0) title else "untitled"; + fn getSaveFilename(self: *const Self, allocator: Allocator) ![]const u8 { + const title_str = asStringSlice(&escape(self.title)); + const name = if (title_str.len != 0) title_str else "untitled"; - return try std.mem.concat(self.alloc, u8, &[_][]const u8{ name, ".sav" }); + return try std.mem.concat(allocator, u8, &[_][]const u8{ name, ".sav" }); } fn writeSaveToDisk(self: Self, path: []const u8) !void { - const file_path = try self.getSaveFilePath(path); + const file_path = try self.getSaveFilePath(self.alloc, path); defer self.alloc.free(file_path); switch (self.kind) { diff --git a/src/core/util.zig b/src/core/util.zig index 758bf88..33a9a5a 100644 --- a/src/core/util.zig +++ b/src/core/util.zig @@ -69,7 +69,7 @@ pub fn intToBytes(comptime T: type, value: anytype) [@sizeOf(T)]u8 { /// /// This function returns a slice of everything just before the first /// `\0` -pub fn asString(title: [12]u8) []const u8 { +pub fn asStringSlice(title: *const [12]u8) []const u8 { var len = title.len; for (title) |char, i| { if (char == 0) {