Skip to content

Commit 40ea537

Browse files
committed
fix: remove abusive uses of catch unreachable
closes #106
1 parent fdb5cc3 commit 40ea537

File tree

16 files changed

+97
-51
lines changed

16 files changed

+97
-51
lines changed

build_capy.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const WebServerStep = struct {
4848
options: CapyRunOptions,
4949

5050
pub fn create(owner: *std.Build, exe: *std.Build.Step.Compile, options: CapyRunOptions) *WebServerStep {
51-
const self = owner.allocator.create(WebServerStep) catch unreachable;
51+
const self = owner.allocator.create(WebServerStep) catch @panic("OOM");
5252
self.* = .{
5353
.step = std.Build.Step.init(.{
5454
.id = .custom,

examples/calculator.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn erase(_: *anyopaque) !void {
3131
}
3232

3333
fn findOperator(computation: []const u8, pos: usize) ?usize {
34-
return std.mem.indexOfScalarPos(u8, computation, pos, '+') orelse std.mem.indexOfScalarPos(u8, computation, pos, '-') orelse std.mem.indexOfScalarPos(u8, computation, pos, '*') orelse std.mem.indexOfScalarPos(u8, computation, pos, '/');
34+
return std.mem.indexOfAnyPos(u8, computation, pos, "+-*/");
3535
}
3636

3737
// TODO: switch back to *capy.button_Impl when ziglang/zig#12325 is fixed

src/backends/gtk/Label.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ fn setText_uiThread(userdata: ?*anyopaque) callconv(.C) c_int {
3737
}
3838

3939
pub fn setText(self: *Label, text: []const u8) void {
40-
self.nullTerminated = lib.internal.allocator.dupeZ(u8, text) catch unreachable;
40+
self.nullTerminated = lib.internal.allocator.dupeZ(u8, text) catch @panic("OOM");
4141

4242
// It must be run in UI thread otherwise set_text might crash randomly
43-
const runOpts = lib.internal.allocator.create(RunOpts) catch unreachable;
43+
const runOpts = lib.internal.allocator.create(RunOpts) catch @panic("OOM");
4444
runOpts.* = .{
4545
.label = @as(*c.GtkLabel, @ptrCast(self.peer)),
4646
.text = self.nullTerminated.?,

src/backends/gtk/common.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn Events(comptime T: type) type {
9090
if (finalKeyval >= 32768) return 0;
9191
const codepoint = c.gdk_keyval_to_unicode(finalKeyval);
9292
var buf: [4]u8 = undefined;
93-
const str_length = std.unicode.utf8Encode(@intCast(codepoint), &buf) catch unreachable;
93+
const str_length = std.unicode.utf8Encode(@intCast(codepoint), &buf) catch unreachable; // unreachable because 'buf' is big enough
9494
const str = buf[0..str_length];
9595

9696
if (str.len != 0) {

src/backends/wasm/Button.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn getLabel(self: *const Button) [:0]const u8 {
3434
return text;
3535
} else {
3636
const len = js.getTextLen(self.peer.element);
37-
const text = lib.lasting_allocator.allocSentinel(u8, len, 0) catch unreachable;
37+
const text = lib.lasting_allocator.allocSentinel(u8, len, 0) catch @panic("OOM");
3838
js.getText(self.peer.element, text.ptr);
3939
self.temp_label = text;
4040

src/backends/wasm/Container.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn create() !Container {
2323

2424
pub fn add(self: *Container, peer: *GuiWidget) void {
2525
js.appendElement(self.peer.element, peer.element);
26-
self.peer.children.append(peer) catch unreachable;
26+
self.peer.children.append(peer) catch @panic("OOM");
2727
}
2828

2929
pub fn remove(self: *const Container, peer: *GuiWidget) void {

src/backends/wasm/Label.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn setFont(self: *Label, font: lib.Font) void {
3535
js.setStyle(
3636
self.peer.element,
3737
"fontSize",
38-
std.fmt.bufPrint(&buf, "{d}pt", .{size}) catch unreachable,
38+
std.fmt.bufPrint(&buf, "{d}pt", .{size}) catch @panic("OOM"),
3939
);
4040
} else {
4141
js.removeAttribute(self.peer.element, "fontSize");
@@ -55,7 +55,7 @@ pub fn getText(self: *Label) []const u8 {
5555
return text;
5656
} else {
5757
const len = js.getTextLen(self.peer.element);
58-
const text = lib.lasting_allocator.allocSentinel(u8, len, 0) catch unreachable;
58+
const text = lib.lasting_allocator.allocSentinel(u8, len, 0) catch @panic("OOM");
5959
js.getText(self.peer.element, text.ptr);
6060
self.temp_text = text;
6161

src/backends/wasm/TextField.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Events = common.Events;
77
const TextField = @This();
88

99
peer: *GuiWidget,
10+
text: ?[:0]const u8 = null,
1011

1112
pub usingnamespace Events(TextField);
1213

@@ -24,11 +25,14 @@ pub fn setText(self: *TextField, text: []const u8) void {
2425
}
2526

2627
pub fn getText(self: *TextField) [:0]const u8 {
28+
if (self.text) |previous_text| {
29+
lib.allocator.free(previous_text);
30+
}
31+
2732
const len = js.getTextLen(self.peer.element);
28-
// TODO: fix the obvious memory leak
29-
const text = lib.lasting_allocator.allocSentinel(u8, len, 0) catch unreachable;
33+
const text = lib.lasting_allocator.allocSentinel(u8, len, 0) catch @panic("OOM");
3034
js.getText(self.peer.element, text.ptr);
31-
35+
self.text = text;
3236
return text;
3337
}
3438

src/backends/win32/Monitor.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pub fn getList() []Monitor {
3535
while (win32.EnumDisplayDevicesW(null, i, &display_device, 0) != 0) : (i += 1) {
3636
if (display_device.StateFlags & win32.DISPLAY_DEVICE_ATTACHED_TO_DESKTOP != 0) {
3737
const device_name: [:0]const u16 = std.mem.span(@as([*:0]u16, @ptrCast(&display_device.DeviceName)));
38-
const cloned_device_name = allocator.dupeZ(u16, device_name) catch unreachable;
39-
adapters.append(cloned_device_name) catch unreachable;
38+
const cloned_device_name = allocator.dupeZ(u16, device_name) catch @panic("OOM");
39+
adapters.append(cloned_device_name) catch @panic("OOM");
4040
}
4141
}
4242
}
@@ -62,23 +62,23 @@ pub fn getList() []Monitor {
6262
var i: u32 = 0;
6363
while (win32.EnumDisplayDevicesW(adapter, i, &display_device, 0) != 0) : (i += 1) {
6464
const device_name: [:0]const u16 = std.mem.span(@as([*:0]u16, @ptrCast(&display_device.DeviceName)));
65-
const cloned_device_name = allocator.dupeZ(u16, device_name) catch unreachable;
65+
const cloned_device_name = allocator.dupeZ(u16, device_name) catch @panic("OOM");
6666
const device_string: [:0]const u16 = std.mem.span(@as([*:0]u16, @ptrCast(&display_device.DeviceString)));
67-
const cloned_device_string = std.unicode.utf16LeToUtf8Alloc(allocator, device_string) catch unreachable;
68-
monitor_names.append(.{ .adapter = adapter, .monitor_name = cloned_device_name, .monitor_friendly_name = cloned_device_string }) catch unreachable;
67+
const cloned_device_string = std.unicode.utf16LeToUtf8Alloc(allocator, device_string) catch @panic("OOM");
68+
monitor_names.append(.{ .adapter = adapter, .monitor_name = cloned_device_name, .monitor_friendly_name = cloned_device_string }) catch @panic("OOM");
6969
}
7070
}
7171
}
7272

7373
for (monitor_names.items) |name| {
7474
monitors.append(Monitor{
75-
.adapter_win32_name = allocator.dupeZ(u16, name.adapter) catch unreachable,
76-
.win32_name = allocator.dupeZ(u16, name.monitor_name) catch unreachable,
77-
.device_name = allocator.dupe(u8, name.monitor_friendly_name) catch unreachable,
78-
}) catch unreachable;
75+
.adapter_win32_name = allocator.dupeZ(u16, name.adapter) catch @panic("OOM"),
76+
.win32_name = allocator.dupeZ(u16, name.monitor_name) catch @panic("OOM"),
77+
.device_name = allocator.dupe(u8, name.monitor_friendly_name) catch @panic("OOM"),
78+
}) catch @panic("OOM");
7979
}
8080

81-
monitor_list = monitors.toOwnedSlice() catch unreachable;
81+
monitor_list = monitors.toOwnedSlice() catch @panic("OOM");
8282
return monitor_list.?;
8383
}
8484
}

src/backends/win32/backend.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ pub const TextField = struct {
999999

10001000
pub fn getText(self: *TextField) [:0]const u8 {
10011001
const len = win32.GetWindowTextLengthW(self.peer);
1002-
var buf = lib.internal.allocator.allocSentinel(u16, @as(usize, @intCast(len)), 0) catch unreachable; // TODO return error
1002+
var buf = lib.internal.allocator.allocSentinel(u16, @as(usize, @intCast(len)), 0) catch @panic("OOM");
10031003
defer lib.internal.allocator.free(buf);
10041004
const realLen = @as(usize, @intCast(win32.GetWindowTextW(self.peer, buf.ptr, len + 1)));
10051005
const utf16Slice = buf[0..realLen];
@@ -1060,11 +1060,11 @@ pub const TextArea = struct {
10601060
pub fn getText(self: *TextArea) [:0]const u8 {
10611061
const allocator = self.arena.allocator();
10621062
const len = win32.GetWindowTextLengthW(self.peer);
1063-
var buf = allocator.allocSentinel(u16, @as(usize, @intCast(len)), 0) catch unreachable; // TODO return error
1063+
var buf = allocator.allocSentinel(u16, @as(usize, @intCast(len)), 0) catch @panic("OOM");
10641064
defer allocator.free(buf);
10651065
const realLen = @as(usize, @intCast(win32.GetWindowTextW(self.peer, buf.ptr, len + 1)));
10661066
const utf16Slice = buf[0..realLen];
1067-
const text = std.unicode.utf16LeToUtf8AllocZ(allocator, utf16Slice) catch unreachable; // TODO return error
1067+
const text = std.unicode.utf16LeToUtf8AllocZ(allocator, utf16Slice) catch @panic("OOM");
10681068
return text;
10691069
}
10701070

@@ -1122,11 +1122,11 @@ pub const Button = struct {
11221122
pub fn getLabel(self: *Button) [:0]const u8 {
11231123
const allocator = self.arena.allocator();
11241124
const len = win32.GetWindowTextLengthW(self.peer);
1125-
var buf = allocator.allocSentinel(u16, @as(usize, @intCast(len)), 0) catch unreachable; // TODO return error
1125+
var buf = allocator.allocSentinel(u16, @as(usize, @intCast(len)), 0) catch @panic("OOM");
11261126
defer allocator.free(buf);
11271127
const realLen = @as(usize, @intCast(win32.GetWindowTextW(self.peer, buf.ptr, len + 1)));
11281128
const utf16Slice = buf[0..realLen];
1129-
const text = std.unicode.utf16leToUtf8AllocZ(allocator, utf16Slice) catch unreachable; // TODO return error
1129+
const text = std.unicode.utf16leToUtf8AllocZ(allocator, utf16Slice) catch @panic("OOM");
11301130
return text;
11311131
}
11321132

@@ -1415,7 +1415,7 @@ pub const TabContainer = struct {
14151415
pub fn insert(self: *TabContainer, position: usize, peer: PeerType) usize {
14161416
const item = win32Backend.TCITEMA{ .mask = 0 };
14171417
const newIndex = win32Backend.TabCtrl_InsertItemW(self.tabControl, @as(c_int, @intCast(position)), &item);
1418-
self.peerList.append(peer) catch unreachable;
1418+
self.peerList.append(peer) catch @panic("OOM");
14191419

14201420
if (self.shownPeer == null) {
14211421
_ = win32.SetParent(peer, self.peer);

0 commit comments

Comments
 (0)