Change errdefers in create()/init() functions

They should use gpa.destroy() instead of foo.destroy() because (most) of
them have fields that may not be initialized by the first error, so
the foo.destroy() could crash.
This commit is contained in:
Ben Buhse 2026-02-13 19:30:21 -06:00
commit 6bf607b759
No known key found for this signature in database
GPG key ID: 7916ACFCD38FD0B4
11 changed files with 12 additions and 11 deletions

View file

@ -78,6 +78,7 @@ pub fn initSurface(bar: *Bar) !void {
pub fn deinit(bar: *Bar) void {
bar.configured = false;
bar.timezone.deinit();
if (bar.wl_surface) |wl_surface| {
wl_surface.destroy();
}

View file

@ -19,7 +19,7 @@ link: wl.list.Link,
pub fn create(river_input_device_v1: *river.InputDeviceV1) !*InputDevice {
const input_device = try utils.gpa.create(InputDevice);
errdefer input_device.destroy();
errdefer utils.gpa.destroy(input_device);
input_device.* = .{
.river_input_device_v1 = river_input_device_v1,

View file

@ -18,7 +18,7 @@ libinput_devices: wl.list.Head(LibinputDevice, .link),
pub fn create(context: *Context, river_input_manager_v1: *river.InputManagerV1, river_libinput_config_v1: *river.LibinputConfigV1) !*InputManager {
log.debug("Creating new InputManager", .{});
const im = try utils.gpa.create(InputManager);
errdefer im.destroy();
errdefer utils.gpa.destroy(im);
im.* = .{
.context = context,

View file

@ -79,7 +79,7 @@ link: wl.list.Link,
pub fn create(context: *Context, river_libinput_device_v1: *river.LibinputDeviceV1) !*LibinputDevice {
const libinput_device = try utils.gpa.create(LibinputDevice);
errdefer libinput_device.destroy();
errdefer utils.gpa.destroy(libinput_device);
libinput_device.* = .{
.context = context,

View file

@ -74,7 +74,7 @@ pub const PendingManage = struct {
pub fn create(context: *Context, river_output_v1: *river.OutputV1) !*Output {
var output = try utils.gpa.create(Output);
errdefer output.destroy();
errdefer utils.gpa.destroy(output);
const bar = Bar.init(context, output) catch |e| blk: {
log.err("Failed to create a bar: {}", .{e});

View file

@ -58,7 +58,7 @@ pub const PointerOp = union(enum) {
pub fn create(context: *Context, river_seat_v1: *river.SeatV1) !*Seat {
var seat = try utils.gpa.create(Seat);
errdefer seat.destroy();
errdefer utils.gpa.destroy(seat);
seat.* = .{
.context = context,

View file

@ -69,7 +69,7 @@ pub const PendingRender = struct {
pub fn create(context: *Context, river_window_v1: *river.WindowV1, output: ?*Output) !*Window {
var window = try utils.gpa.create(Window);
errdefer window.destroy();
errdefer utils.gpa.destroy(window);
window.* = .{
.context = context,

View file

@ -18,7 +18,7 @@ orphan_windows: wl.list.Head(Window, .link),
pub fn create(context: *Context, window_manager_v1: *river.WindowManagerV1) !*WindowManager {
const wm = try utils.gpa.create(WindowManager);
errdefer wm.destroy();
errdefer utils.gpa.destroy(wm);
wm.* = .{
.context = context,

View file

@ -56,7 +56,7 @@ const XkbBinding = struct {
fn create(xkb_binding_v1: *river.XkbBindingV1, command: Command, context: *Context) !*XkbBinding {
var xkb_binding = try utils.gpa.create(XkbBinding);
errdefer xkb_binding.destroy();
errdefer utils.gpa.destroy(xkb_binding);
xkb_binding.* = .{
.xkb_binding_v1 = xkb_binding_v1,
@ -405,7 +405,7 @@ bindings: wl.list.Head(XkbBinding, .link),
pub fn create(context: *Context, xkb_bindings_v1: *river.XkbBindingsV1) !*XkbBindings {
const xkb_bindings = try utils.gpa.create(XkbBindings);
errdefer xkb_bindings.destroy();
errdefer utils.gpa.destroy(xkb_bindings);
xkb_bindings.* = .{
.context = context,

View file

@ -11,7 +11,7 @@ pub const Flag = struct {
kind: enum { boolean, arg },
};
pub fn parser(comptime Arg: type, comptime flags: []const Flag) type {
pub fn Parser(comptime Arg: type, comptime flags: []const Flag) type {
switch (Arg) {
// TODO consider allowing []const u8
[:0]const u8, [*:0]const u8 => {}, // ok

View file

@ -185,7 +185,7 @@ fn run(wl_display: *wl.Display, context: *Context) !void {
}
fn parseArgs() void {
const result = flags.parser([*:0]const u8, &.{
const result = flags.Parser([*:0]const u8, &.{
.{ .name = "h", .kind = .boolean },
.{ .name = "version", .kind = .boolean },
.{ .name = "log-level", .kind = .arg },