gdb: generate the prefix name for prefix commands on demand
authorMarco Barisione <mbarisione@undo.io>
Wed, 12 May 2021 10:19:22 +0000 (11:19 +0100)
committerMarco Barisione <mbarisione@undo.io>
Wed, 12 May 2021 10:19:22 +0000 (11:19 +0100)
commit2f822da535ba9b159174d02d7114b4fc4f7c8818
tree7014b48d9f7893385ed27091caa75c28964c3514
parentf0bbe8bab84e9b9dc5367a5fe1d0b8d233bf213c
gdb: generate the prefix name for prefix commands on demand

Previously, the prefixname field of struct cmd_list_element was manually
set for prefix commands.  This seems verbose and error prone as it
required every single call to functions adding prefix commands to
specify the prefix name while the same information can be easily
generated.

Historically, this was not possible as the prefix field was null for
many commands, but this was fixed in commit
3f4d92ebdf7f848b5ccc9e8d8e8514c64fde1183 by Philippe Waroquiers, so
we can rely on the prefix field being set when generating the prefix
name.

This commit also fixes a use after free in this scenario:
* A command gets created via Python (using the gdb.Command class).
  The prefix name member is dynamically allocated.
* An alias to the new command is created. The alias's prefixname is set
  to point to the prefixname for the original command with a direct
  assignment.
* A new command with the same name as the Python command is created.
* The object for the original Python command gets freed and its
  prefixname gets freed as well.
* The alias is updated to point to the new command, but its prefixname
  is not updated so it keeps pointing to the freed one.

gdb/ChangeLog:

* command.h (add_prefix_cmd): Remove the prefixname argument as
it can now be generated automatically.  Update all callers.
(add_basic_prefix_cmd): Ditto.
(add_show_prefix_cmd): Ditto.
(add_prefix_cmd_suppress_notification): Ditto.
(add_abbrev_prefix_cmd): Ditto.
* cli/cli-decode.c (add_prefix_cmd): Ditto.
(add_basic_prefix_cmd): Ditto.
(add_show_prefix_cmd): Ditto.
(add_prefix_cmd_suppress_notification): Ditto.
(add_prefix_cmd_suppress_notification): Ditto.
(add_abbrev_prefix_cmd): Ditto.
* cli/cli-decode.h (struct cmd_list_element): Replace the
prefixname member variable with a method which generates the
prefix name at runtime.  Update all code reading the prefix
name to use the method, and remove all code setting it.
* python/py-cmd.c (cmdpy_destroyer): Remove code to free the
prefixname member as it's now a method.
(cmdpy_function): Determine if the command is a prefix by
looking at prefixlist, not prefixname.
69 files changed:
gdb/ada-lang.c
gdb/arc-tdep.c
gdb/arm-tdep.c
gdb/auto-load.c
gdb/breakpoint.c
gdb/bsd-kvm.c
gdb/btrace.c
gdb/cli/cli-cmds.c
gdb/cli/cli-decode.c
gdb/cli/cli-decode.h
gdb/cli/cli-dump.c
gdb/cli/cli-logging.c
gdb/cli/cli-script.c
gdb/cli/cli-setshow.c
gdb/cli/cli-style.c
gdb/cli/cli-style.h
gdb/command.h
gdb/compile/compile.c
gdb/cp-support.c
gdb/dcache.c
gdb/dwarf2/index-cache.c
gdb/dwarf2/read.c
gdb/f-lang.c
gdb/frame.c
gdb/gnu-nat.c
gdb/go32-nat.c
gdb/guile/guile.c
gdb/guile/scm-cmd.c
gdb/i386-tdep.c
gdb/infcmd.c
gdb/language.c
gdb/macrocmd.c
gdb/maint-test-options.c
gdb/maint-test-settings.c
gdb/maint.c
gdb/memattr.c
gdb/mips-tdep.c
gdb/printcmd.c
gdb/probe.c
gdb/python/py-cmd.c
gdb/python/python.c
gdb/ravenscar-thread.c
gdb/record-btrace.c
gdb/record-full.c
gdb/record.c
gdb/remote.c
gdb/riscv-tdep.c
gdb/rs6000-tdep.c
gdb/ser-tcp.c
gdb/serial.c
gdb/sh-tdep.c
gdb/skip.c
gdb/sparc64-tdep.c
gdb/stack.c
gdb/symfile.c
gdb/symtab.c
gdb/target-descriptions.c
gdb/target.c
gdb/thread.c
gdb/top.c
gdb/tracepoint.c
gdb/tui/tui-layout.c
gdb/tui/tui-win.c
gdb/typeprint.c
gdb/unittests/command-def-selftests.c
gdb/utils.c
gdb/valprint.c
gdb/value.c
gdb/windows-tdep.c
This page took 0.031665 seconds and 4 git commands to generate.