Skip to contentSkip to conversation

ui/keymaps: Avoid trace crash and improve error messages

PATCH Applied Markus Armbruster 3 participants 4 messages Jul 23, 2025 – Sep 1, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/keymaps: Avoid trace crash and improve error messages

parse_keyboard_layout() passes a possibly null @filename to trace_keymap_parse(). Trace backend log then formats it with %s, which crashes on some systems.

Fix by moving the null check before the trace_keymap_parse().

While there, improve the error messages a bit.

Fixes d3b787fa7dde (keymaps: add tracing)
---
 ui/keymaps.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
ui/keymaps.c +10 -4
index 6ceaa97085..2359dbfe7e 100644
@@ -86,19 +86,25 @@ static int parse_keyboard_layout(kbd_layout_t *k,
                                  const name2keysym_t *table,
                                  const char *language, Error **errp)
 {
+    g_autofree char *filename = NULL;
     int ret;
     FILE *f;
-    char * filename;
     char line[1024];
     char keyname[64];
     int len;
 
     filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
+    if (!filename) {
+        error_setg(errp, "could not find keymap file for language '%s'",
+                   language);
+        return -1;
+    }
+
     trace_keymap_parse(filename);
-    f = filename ? fopen(filename, "r") : NULL;
-    g_free(filename);
+
+    f = fopen(filename, "r");
     if (!f) {
-        error_setg(errp, "could not read keymap file: '%s'", language);
+        error_setg_file_open(errp, errno, filename);
         return -1;
     }
 
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Markus Armbruster

parse_keyboard_layout() passes a possibly null @filename to trace_keymap_parse(). Trace backend log then formats it with %s, which crashes on some systems.

Fix by moving the null check before the trace_keymap_parse().

While there, improve the error messages a bit.

Fixes d3b787fa7dde (keymaps: add tracing)
Quoted 42 lines
---
 ui/keymaps.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
ui/keymaps.c +10 -5
index 6ceaa97085..2359dbfe7e 100644
@@ -86,19 +86,25 @@ static int parse_keyboard_layout(kbd_layout_t *k,
                                  const name2keysym_t *table,
                                  const char *language, Error **errp)
 {
+    g_autofree char *filename = NULL;
     int ret;
     FILE *f;
-    char * filename;
     char line[1024];
     char keyname[64];
     int len;
 
     filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
+    if (!filename) {
+        error_setg(errp, "could not find keymap file for language '%s'",
+                   language);
+        return -1;
+    }
+
     trace_keymap_parse(filename);
-    f = filename ? fopen(filename, "r") : NULL;
-    g_free(filename);
+
+    f = fopen(filename, "r");
     if (!f) {
-        error_setg(errp, "could not read keymap file: '%s'", language);
+        error_setg_file_open(errp, errno, filename);
         return -1;
     }
 
--

2.49.0

Reviewed-by on v1 1/1
#2 lore raw
In reply to Markus Armbruster
Quoted 6 lines · Markus Armbruster

parse_keyboard_layout() passes a possibly null @filename to trace_keymap_parse(). Trace backend log then formats it with %s, which crashes on some systems.

Fix by moving the null check before the trace_keymap_parse().

Quoted 7 lines

While there, improve the error messages a bit.

Fixes d3b787fa7dde (keymaps: add tracing)
---
  ui/keymaps.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
author Applied
#3 lore raw
In reply to Markus Armbruster
Quoted 10 lines · Markus Armbruster

parse_keyboard_layout() passes a possibly null @filename to trace_keymap_parse(). Trace backend log then formats it with %s, which crashes on some systems.

Fix by moving the null check before the trace_keymap_parse().

While there, improve the error messages a bit.

Fixes d3b787fa7dde (keymaps: add tracing)

Queued. Thanks for the review!