Skip to contentSkip to conversation

keytable: fix segfault when reading legacy keymaps

PATCH In review Matthias Reichl 2 participants 2 messages Dec 20, 2023 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 keytable: fix segfault when reading legacy keymaps

Since commit ae1492611432 ("keytable: remove line length limits") ir-keytable crashes when reading keymaps in legacy (non-toml) format and valgrind reports an UMR:

valgrind ir-keytable -w /etc/rc_keymaps/hiastest
...
==83788== Conditional jump or move depends on uninitialised value(s)

==83788==    at 0x48E8B8C: getdelim (iogetdelim.c:59)
==83788==    by 0x1105C6: getline (stdio.h:120)
==83788==    by 0x1105C6: parse_plain_keymap (keymap.c:98)
==83788==    by 0x1105C6: parse_keymap (keymap.c:533)
==83788==    by 0x112798: parse_opt (keytable.c:569)
==83788==    by 0x4986E50: group_parse (argp-parse.c:257)
==83788==    by 0x4986E50: parser_parse_opt (argp-parse.c:747)
==83788==    by 0x4986E50: parser_parse_next (argp-parse.c:867)
==83788==    by 0x4986E50: argp_parse (argp-parse.c:921)
==83788==    by 0x10E470: main (keytable.c:2071)

Fix this by properly initializing line to NULL so getline will allocate a buffer instead of using some random memory locations.

---
 utils/common/keymap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/common/keymap.c +1 -1
index 04c5ef54723c..21c1c0b5d581 100644
@@ -75,7 +75,7 @@ static error_t parse_plain_keymap(char *fname, struct keymap **keymap, bool verb
 {
 	FILE *fin;
 	int line_no = 0;
-	char *scancode, *keycode, *line;
+	char *scancode, *keycode, *line = NULL;
 	size_t line_size;
 	struct scancode_entry *se;
 	struct keymap *map;
Signature · 1 line
2.39.2
#1 lore raw
Quoted 18 lines · Matthias Reichl

Since commit ae1492611432 ("keytable: remove line length limits") ir-keytable crashes when reading keymaps in legacy (non-toml) format and valgrind reports an UMR:

valgrind ir-keytable -w /etc/rc_keymaps/hiastest
...
==83788== Conditional jump or move depends on uninitialised value(s)

==83788==    at 0x48E8B8C: getdelim (iogetdelim.c:59)
==83788==    by 0x1105C6: getline (stdio.h:120)
==83788==    by 0x1105C6: parse_plain_keymap (keymap.c:98)
==83788==    by 0x1105C6: parse_keymap (keymap.c:533)
==83788==    by 0x112798: parse_opt (keytable.c:569)
==83788==    by 0x4986E50: group_parse (argp-parse.c:257)
==83788==    by 0x4986E50: parser_parse_opt (argp-parse.c:747)
==83788==    by 0x4986E50: parser_parse_next (argp-parse.c:867)
==83788==    by 0x4986E50: argp_parse (argp-parse.c:921)
==83788==    by 0x10E470: main (keytable.c:2071)
Quoted 2 lines

Fix this by properly initializing line to NULL so getline will allocate a buffer instead of using some random memory locations.

Nice catch, applied.

Thanks
Sean

Quoted 22 lines
---
 utils/common/keymap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/common/keymap.c +1 -1
index 04c5ef54723c..21c1c0b5d581 100644
@@ -75,7 +75,7 @@ static error_t parse_plain_keymap(char *fname, struct keymap **keymap, bool verb
 {
 	FILE *fin;
 	int line_no = 0;
-	char *scancode, *keycode, *line;
+	char *scancode, *keycode, *line = NULL;
 	size_t line_size;
 	struct scancode_entry *se;
 	struct keymap *map;
Signature · 1 line
2.39.2