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
This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:56 -03:00
parent 4b6897aedf
commit c701156ce6
3 changed files with 12 additions and 13 deletions

View File

@ -11,7 +11,6 @@ const pitch = @import("core/ppu.zig").framebuf_pitch;
const scale = @import("core/emu.zig").win_scale; const scale = @import("core/emu.zig").win_scale;
const emu = @import("core/emu.zig"); const emu = @import("core/emu.zig");
const asString = @import("core/util.zig").asString;
const log = std.log.scoped(.GUI); const log = std.log.scoped(.GUI);
const default_title: []const u8 = "ZBA"; const default_title: []const u8 = "ZBA";

View File

@ -3,7 +3,7 @@ const Allocator = std.mem.Allocator;
const log = std.log.scoped(.Backup); const log = std.log.scoped(.Backup);
const escape = @import("../util.zig").escape; const escape = @import("../util.zig").escape;
const asString = @import("../util.zig").asString; const asStringSlice = @import("../util.zig").asStringSlice;
const backup_kinds = [5]Needle{ const backup_kinds = [5]Needle{
.{ .str = "EEPROM_V", .kind = .Eeprom }, .{ .str = "EEPROM_V", .kind = .Eeprom },
@ -72,7 +72,7 @@ pub const Backup = struct {
} }
fn loadSaveFromDisk(self: *Self, path: []const u8) !void { 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); defer self.alloc.free(file_path);
// FIXME: Don't rely on this lol // 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 { fn getSaveFilePath(self: *const Self, allocator: Allocator, path: []const u8) ![]const u8 {
const filename = try self.getSaveFilename(); const filename = try self.getSaveFilename(allocator);
defer self.alloc.free(filename); 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 { fn getSaveFilename(self: *const Self, allocator: Allocator) ![]const u8 {
const title = asString(escape(self.title)); const title_str = asStringSlice(&escape(self.title));
const name = if (title.len != 0) title else "untitled"; 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 { 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); defer self.alloc.free(file_path);
switch (self.kind) { switch (self.kind) {

View File

@ -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 /// This function returns a slice of everything just before the first
/// `\0` /// `\0`
pub fn asString(title: [12]u8) []const u8 { pub fn asStringSlice(title: *const [12]u8) []const u8 {
var len = title.len; var len = title.len;
for (title) |char, i| { for (title) |char, i| {
if (char == 0) { if (char == 0) {