Skip to content

Commit 141b697

Browse files
authored
apprt/embedded: utf8 encoding buffer lifetime must extend beyond call (#6834)
Fixes #6821 UTF8 translation using KeymapDarwin requires a buffer and the buffer was stack allocated in the coreKeyEvent call and returned from the function. We need the buffer to live longer than this. Long term, we're removing KeymapDarwin (there is a whole TODO comment in there about how to do it), but this fixes a real problem today.
2 parents 88ff566 + f31f8bb commit 141b697

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/Surface.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ pub fn keyCallback(
17271727
self: *Surface,
17281728
event: input.KeyEvent,
17291729
) !InputEffect {
1730-
// log.debug("text keyCallback event={}", .{event});
1730+
// log.warn("text keyCallback event={}", .{event});
17311731

17321732
// Crash metadata in case we crash in here
17331733
crash.sentry.thread_state = self.crashThreadState();

src/apprt/embedded.zig

+16-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,17 @@ pub const App = struct {
149149
}
150150

151151
/// Convert a C key event into a Zig key event.
152+
///
153+
/// The buffer is needed for possibly storing translated UTF-8 text.
154+
/// This buffer may (or may not) be referenced by the resulting KeyEvent
155+
/// so it should be valid for the lifetime of the KeyEvent.
156+
///
157+
/// The size of the buffer doesn't need to be large, we always
158+
/// used to hardcode 128 bytes and never ran into issues. If it isn't
159+
/// large enough an error will be returned.
152160
fn coreKeyEvent(
153161
self: *App,
162+
buf: []u8,
154163
target: KeyTarget,
155164
event: KeyEvent,
156165
) !?input.KeyEvent {
@@ -217,7 +226,6 @@ pub const App = struct {
217226
// Translate our key using the keymap for our localized keyboard layout.
218227
// We only translate for keydown events. Otherwise, we only care about
219228
// the raw keycode.
220-
var buf: [128]u8 = undefined;
221229
const result: input.Keymap.Translation = if (is_down) translate: {
222230
// If the event provided us with text, then we use this as a result
223231
// and do not do manual translation.
@@ -226,7 +234,7 @@ pub const App = struct {
226234
.composing = event.composing,
227235
.mods = translate_mods,
228236
} else try self.keymap.translate(
229-
&buf,
237+
buf,
230238
switch (target) {
231239
.app => &self.keymap_state,
232240
.surface => |surface| &surface.keymap_state,
@@ -360,7 +368,9 @@ pub const App = struct {
360368
event: KeyEvent,
361369
) !bool {
362370
// Convert our C key event into a Zig one.
371+
var buf: [128]u8 = undefined;
363372
const input_event: input.KeyEvent = (try self.coreKeyEvent(
373+
&buf,
364374
target,
365375
event,
366376
)) orelse return false;
@@ -1432,7 +1442,9 @@ pub const CAPI = struct {
14321442
app: *App,
14331443
event: KeyEvent,
14341444
) bool {
1445+
var buf: [128]u8 = undefined;
14351446
const core_event = app.coreKeyEvent(
1447+
&buf,
14361448
.app,
14371449
event.keyEvent(),
14381450
) catch |err| {
@@ -1684,7 +1696,9 @@ pub const CAPI = struct {
16841696
surface: *Surface,
16851697
event: KeyEvent,
16861698
) bool {
1699+
var buf: [128]u8 = undefined;
16871700
const core_event = surface.app.coreKeyEvent(
1701+
&buf,
16881702
// Note: this "app" target here looks like a bug, but it is
16891703
// intentional. coreKeyEvent uses the target only as a way to
16901704
// trigger preedit callbacks for keymap translation and we don't

src/build/Config.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn init(b: *std.Build) !Config {
7272
if (result.result.os.tag == .macos and
7373
builtin.target.os.tag.isDarwin())
7474
{
75-
result = genericMacOSTarget(b, null);
75+
result = genericMacOSTarget(b, result.query.cpu_arch);
7676
}
7777

7878
// If we have no minimum OS version, we set the default based on

0 commit comments

Comments
 (0)