From 3df8c6afdd6d38a7622ff5f4b1a64aff80334ab9 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Fri, 27 Nov 2020 10:46:07 +0000 Subject: [PATCH] gdb: fix potentially uninitialised variable In commit: commit 037d7135de575c9e0c20e9158c105979bfee339c Date: Mon Nov 16 11:36:56 2020 +0000 gdb: improve command completion for 'print', 'x', and 'display' A potential use of an uninitialised variable was introduced. This is fixed in this commit. Previously when analysing /FMT strings for tab completion we considered two possibilities, either the user has typed '/', or the user has typed '/' followed by an alpha-numeric character, as these are the only valid FMT string characters. This meant that if the user type, for example '/@' and then tried to tab complete gdb would use an uninitialised variable. Currently only the first character after the '/' is checked to see if it is alpha-numeric, so if a user typed '/x@@' then gdb would be happy to treat this as a FMT string. Given the goal of this change was primarily to allow tab completion of symbols later in the command when a /FMT was used then I decided to just make the /FMT skipping less smart. Now any characters after the '/' up to the first white space, will be treated as a FMT string. gdb/ChangeLog: * printcmd.c (skip_over_slash_fmt): Reorder code to ensure in_fmt is always initialized. --- gdb/ChangeLog | 5 +++++ gdb/printcmd.c | 25 ++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index aa08449d100..e9beaaba632 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2020-11-27 Andrew Burgess + + * printcmd.c (skip_over_slash_fmt): Reorder code to ensure in_fmt + is always initialized. + 2020-11-26 Przemyslaw Wirkus * features/aarch64-fpu.c (create_feature_aarch64_fpu): Regenerate. diff --git a/gdb/printcmd.c b/gdb/printcmd.c index a9c64b97c81..e95b8802950 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -1258,27 +1258,38 @@ skip_over_slash_fmt (completion_tracker &tracker, const char **args) bool in_fmt; tracker.set_use_custom_word_point (true); - if (ISALNUM (text[1]) || ISSPACE (text[1])) + if (text[1] == '\0') { - /* Skip over the actual format specification. */ + /* The user tried to complete after typing just the '/' character + of the /FMT string. Step the completer past the '/', but we + don't offer any completions. */ + in_fmt = true; + ++text; + } + else + { + /* The user has typed some characters after the '/', we assume + this is a complete /FMT string, first skip over it. */ text = skip_to_space (text); if (*text == '\0') { + /* We're at the end of the input string. The user has typed + '/FMT' and asked for a completion. Push an empty + completion string, this will cause readline to insert a + space so the user now has '/FMT '. */ in_fmt = true; tracker.add_completion (make_unique_xstrdup (text)); } else { + /* The user has already typed things after the /FMT, skip the + whitespace and return false. Whoever called this function + should then try to complete what comes next. */ in_fmt = false; text = skip_spaces (text); } } - else if (text[1] == '\0') - { - in_fmt = true; - ++text; - } tracker.advance_custom_word_point_by (text - *args); *args = text; -- 2.30.2