binutils-gdb/gdb/python
Simon Marchi e25d6d93c4 gdb: fix getting range of flexible array member in Python
As reported in bug 27757, we get an internal error when doing:

    $ cat test.c
    struct foo {
        int len;
        int items[];
    };

    struct foo *p;

    int main() {
        return 0;
    }
    $ gcc test.c -g -O0 -o test
    $ ./gdb -q -nx --data-directory=data-directory ./test -ex 'python gdb.parse_and_eval("p").type.target()["items"].type.range()'
    Reading symbols from ./test...
    /home/simark/src/binutils-gdb/gdb/gdbtypes.h:435: internal-error: LONGEST dynamic_prop::const_val() const: Assertion `m_kind == PROP_CONST' failed.
    A problem internal to GDB has been detected,
    further debugging may prove unreliable.
    Quit this debugging session? (y or n)

This is because the Python code (typy_range) blindly reads the high
bound of the type of `items` as a constant value.  Since it is a
flexible array member, it has no high bound, the property is undefined.
Since commit 8c2e4e0689 ("gdb: add accessors to struct dynamic_prop"),
the getters check that you are not getting a property value of the wrong
kind, so this causes a failed assertion.

Fix it by checking if the property is indeed a constant value before
accessing it as such.  Otherwise, use 0.  This restores the previous GDB
behavior: because the structure was zero-initialized, this is what was
returned before.  But now this behavior is explicit and not accidental.

Add a test, gdb.python/flexible-array-member.exp, that is derived from
gdb.base/flexible-array-member.exp.  It tests the same things, but
through the Python API.  It also specifically tests getting the range
from the various kinds of flexible array member types (AFAIK it wasn't
possible to do the equivalent through the CLI).

gdb/ChangeLog:

	PR gdb/27757
	* python/py-type.c (typy_range): Check that bounds are constant
	before accessing them as such.
	* guile/scm-type.c (gdbscm_type_range): Likewise.

gdb/testsuite/ChangeLog:

	PR gdb/27757
	* gdb.python/flexible-array-member.c: New test.
	* gdb.python/flexible-array-member.exp: New test.
	* gdb.guile/scm-type.exp (test_range): Add test for flexible
	array member.
	* gdb.guile/scm-type.c (struct flex_member): New.
	(main): Use it.

Change-Id: Ibef92ee5fd871ecb7c791db2a788f203dff2b841
2021-04-22 15:01:28 -04:00
..
lib/gdb Use importlib instead of imp module on python 3.4+ 2021-04-01 12:26:52 -06:00
py-all-events.def Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-arch.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-auto-load.c gdb: bool-ify ext_lang_auto_load_enabled and friends 2021-01-13 11:57:24 -05:00
py-block.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-bpevent.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-breakpoint.c gdb/breakpoint: add a 'force_condition' parameter to 'create_breakpoint' 2021-04-21 16:47:17 +02:00
py-cmd.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-continueevent.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-event-types.def Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-event.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-event.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-events.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-evtregistry.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-evts.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-exitedevent.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-finishbreakpoint.c gdb/breakpoint: add a 'force_condition' parameter to 'create_breakpoint' 2021-04-21 16:47:17 +02:00
py-frame.c Also compare frame_id_is_next in frapy_richcompare 2021-02-07 19:08:23 +01:00
py-framefilter.c gdb/python: fix FrameDecorator regression on Python 2 2021-03-16 09:31:56 +00:00
py-function.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-gdb-readline.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-inferior.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-infevents.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-infthread.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-instruction.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-instruction.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-lazy-string.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-linetable.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-newobjfileevent.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-objfile.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-param.c gdb/doc: fix the example for get_set_string in Python API docs 2021-03-18 16:55:37 +00:00
py-prettyprint.c gdb: rename get_type_arch to type::arch 2021-01-28 10:12:10 -05:00
py-progspace.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-record-btrace.c gdb: make some variables static 2021-01-20 20:55:05 -05:00
py-record-btrace.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-record-full.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-record-full.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-record.c gdb: make some variables static 2021-01-20 20:55:05 -05:00
py-record.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-ref.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-registers.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-signalevent.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-stopevent.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-stopevent.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-symbol.c Add search_flags to expand_symtabs_matching 2021-04-17 09:35:05 -06:00
py-symtab.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-threadevent.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-tui.c gdb: return true in TuiWindow.is_valid only if TUI is enabled 2021-02-08 11:56:16 +00:00
py-type.c gdb: fix getting range of flexible array member in Python 2021-04-22 15:01:28 -04:00
py-unwind.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-utils.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-value.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-varobj.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
py-xmethods.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
python-config.py
python-internal.h gdb: bool-ify ext_lang_auto_load_enabled and friends 2021-01-13 11:57:24 -05:00
python.c gdb: use make_scoped_restore to restore gdbpy_current_objfile 2021-03-15 09:21:37 +00:00
python.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00