From 4fe30e3c6329e3b23fcfaa181119b9796219b487 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Wed, 3 Aug 2022 16:41:18 -0400 Subject: [PATCH 1/3] Fix overflow --- src/keytables.cc | 6 +++--- src/logkeys.cc | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/keytables.cc b/src/keytables.cc index 2ec5dad..94b139b 100644 --- a/src/keytables.cc +++ b/src/keytables.cc @@ -40,19 +40,19 @@ const char char_or_func[] = // c = character key, f = function key, _ = blank/e inline bool is_char_key(unsigned int code) { - assert(code < sizeof(char_or_func)); + assert(code < sizeof(char_or_func)-1); return (char_or_func[code] == 'c'); } inline bool is_func_key(unsigned int code) { - assert(code < sizeof(char_or_func)); + assert(code < sizeof(char_or_func)-1); return (char_or_func[code] == 'f'); } inline bool is_used_key(unsigned int code) { - assert(code < sizeof(char_or_func)); + assert(code < sizeof(char_or_func)-1); return (char_or_func[code] != '_'); } diff --git a/src/logkeys.cc b/src/logkeys.cc index 5da3cff..63c1a21 100644 --- a/src/logkeys.cc +++ b/src/logkeys.cc @@ -189,7 +189,7 @@ void determine_system_keymap() std::stringstream ss, dump(execute(COMMAND_STR_DUMPKEYS)); // see example output after i.e. `loadkeys slovene` std::string line; - unsigned int i = 0; // keycode + unsigned int i = -1; // keycode int index; int utf8code; // utf-8 code of keysym answering keycode i @@ -205,7 +205,7 @@ void determine_system_keymap() index = line.find("U+", index); } - if (++i >= sizeof(char_or_func)) break; // only ever map keycodes up to 128 (currently N_KEYS_DEFINED are used) + if (++i >= sizeof(char_or_func)-1) break; // only ever map keycodes up to 128 (currently N_KEYS_DEFINED are used) if (!is_char_key(i)) continue; // only map character keys of keyboard assert(line.size() > 0); From 1df13c41bc1a8a0ba895a82c3817f41c4f91a789 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Thu, 4 Aug 2022 15:57:19 -0400 Subject: [PATCH 2/3] Fix another error --- src/logkeys.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logkeys.cc b/src/logkeys.cc index 63c1a21..3a940c1 100644 --- a/src/logkeys.cc +++ b/src/logkeys.cc @@ -311,7 +311,7 @@ void export_keymap_to_file() char buffer[32]; int buflen = 0; unsigned int index; - for (unsigned int i = 0; i < sizeof(char_or_func); ++i) { + for (unsigned int i = 0; i < sizeof(char_or_func)-1; ++i) { buflen = 0; if (is_char_key(i)) { index = to_char_keys_index(i); From 3595cb7bf533c32b8bfae2c547fec8a2a5fefd8e Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Thu, 4 Aug 2022 17:07:40 -0400 Subject: [PATCH 3/3] Write more cleanly --- src/logkeys.cc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/logkeys.cc b/src/logkeys.cc index 3a940c1..ba83890 100644 --- a/src/logkeys.cc +++ b/src/logkeys.cc @@ -258,15 +258,12 @@ void parse_input_keymap() if (stdin == NULL) error(EXIT_FAILURE, errno, "Error opening input keymap '%s'", args.keymap.c_str()); - unsigned int i = -1; unsigned int line_number = 0; wchar_t func_string[32]; wchar_t line[32]; - while (!feof(stdin)) { - - if (++i >= sizeof(char_or_func)) break; // only ever read up to 128 keycode bindings (currently N_KEYS_DEFINED are used) - + // only ever read up to 128 keycode bindings (currently N_KEYS_DEFINED are used) + for (unsigned int i=0; i < sizeof(char_or_func)-1 && !feof(stdin); ++i) { if (is_used_key(i)) { ++line_number; if(fgetws(line, sizeof(line), stdin) == NULL) { @@ -295,7 +292,7 @@ void parse_input_keymap() error_at_line(EXIT_FAILURE, 0, args.keymap.c_str(), line_number, "Invalid function key string"); // does this ever happen? wcscpy(func_keys[to_func_keys_index(i)], func_string); } - } // while (!feof(stdin)) + } // for fclose(stdin); if (line_number < N_KEYS_DEFINED)