[gdb/cli] Fix use of uninitialized variable in complete_command
authorTom de Vries <tdevries@suse.de>
Tue, 21 May 2019 14:32:41 +0000 (16:32 +0200)
committerTom de Vries <tdevries@suse.de>
Tue, 21 May 2019 14:32:41 +0000 (16:32 +0200)
commitfb7806c7a49d6eb75cdbff183d10d00f75968c0f
tree60137fcb017373136def38a45563664f544ca283
parent669d0468399d8375f4d25289938a0c06d12e7f2e
[gdb/cli] Fix use of uninitialized variable in complete_command

When building gdb on ubuntu 16.04 with gcc 5.4.0, and running the gdb
testsuite we run into:
...
FAIL: gdb.linespec/explicit.exp: complete after -line: \
  cmd complete "b -line argument " (timeout)
...

The failure is reproducible outside the testsuite like this:
...
$ gdb -q build/gdb/testsuite/outputs/gdb.linespec/explicit/explicit \
  -ex "complete b -line argument"
Reading symbols from \
  build/gdb/testsuite/outputs/gdb.linespec/explicit/explicit...
terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_M_create
  Aborted (core dumped)
...

The problem is here in complete_command:
...
  completion_result result = complete (arg, &word, &quote_char);

  std::string arg_prefix (arg, word - arg);

  if (result.number_matches != 0)
...
The problem is that the word variable is not initialized when
result.number_matches == 0, but the variable is still used in the arg_prefix
initialization.

Fix this by guarding the arg_prefix initialization with the
'result.number_matches != 0' test.

Build and tested on x86_64-linux.

gdb/ChangeLog:

2019-05-21  Tom de Vries  <tdevries@suse.de>

PR cli/24587
* cli/cli-cmds.c (complete_command): Fix use of unitialized variable.
gdb/ChangeLog
gdb/cli/cli-cmds.c