Patchwork [pushed/Ada] preserve type length in ada-lang.c::to_fixed_range_type

login
register
mail settings
Submitter Joel Brobecker
Date Nov. 29, 2017, 11:33 p.m.
Message ID <1511998413-64211-1-git-send-email-brobecker@adacore.com>
Download mbox | patch
Permalink /patch/24611/
State New
Headers show

Comments

Joel Brobecker - Nov. 29, 2017, 11:33 p.m.
Hello,

This patch fixes a potential issue which was noticed by code inspection:
ada-lang.c::to_fixed_range_type uses gdbtypes.c::create_static_range_type
to create most of the range type, which relies on create_range_type to
do most of the work. The latter has the following piece of code which
sets the length of the range type to match the length of the index_type:

    if (TYPE_STUB (index_type))
      TYPE_TARGET_STUB (result_type) = 1;
    else
      TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));

In Ada, it is actually possible to have a range type whose size
is smaller than its base type. For instance, with:

    type Unsigned2_T is  range 0 .. 2 ** 16 - 1;
    for Unsigned2_T'SIZE use 16;

The compiler generates the following DWARF:

        .uleb128 0x3    # (DIE (0x4e) DW_TAG_subrange_type)
        .byte   0x2     # DW_AT_byte_size
        .byte   0       # DW_AT_lower_bound
        .value  0xffff  # DW_AT_upper_bound
        .long   .LASF64 # DW_AT_name: "try__unsigned2_t___XDLU_0__65535"
        .long   0x616   # DW_AT_type

... which points to the following base type...

        .uleb128 0x1d   # (DIE (0x616) DW_TAG_base_type)
        .byte   0x4     # DW_AT_byte_size
        .byte   0x5     # DW_AT_encoding
        .long   .LASF57 # DW_AT_name: "try__Tunsigned2_tB"
                        # DW_AT_artificial

... which has a size of 4 bytes.

With a type like this one, create_range_type returns a type whose
size is 4 bytes, instead of 2, which is not what we we would normally
expect.

Currently, this function is only used to handle array index types,
so the length of the type actually does not matter and there should
not be any user-visible consequences of the current behavior. But
it seems best to plug this latent bug now, rather than wait for it
to surface....

gdb/ChangeLog:

        * ada-lang.c (to_fixed_range_type): Make sure that the size
        of the range type being returned is the same as the size
        of the range type being fixed.

Tested on x86_64-linux, no regression.
Pushed to master.

Thanks,

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 567cc00..ca8c5df 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@ 
+2017-11-29  Joel Brobecker  <brobecker@adacore.com>
+
+	* ada-lang.c (to_fixed_range_type): Make sure that the size
+	of the range type being returned is the same as the size
+	of the range type being fixed.
+
 2017-11-29  Pedro Alves  <palves@redhat.com>
 
 	PR c++/19436
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 38d1ce6..8e35ee6 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -11688,6 +11688,10 @@  to_fixed_range_type (struct type *raw_type, struct value *dval)
 
       type = create_static_range_type (alloc_type_copy (raw_type),
 				       base_type, L, U);
+      /* create_static_range_type alters the resulting type's length
+         to match the size of the base_type, which is not what we want.
+         Set it back to the original range type's length.  */
+      TYPE_LENGTH (type) = TYPE_LENGTH (raw_type);
       TYPE_NAME (type) = name;
       return type;
     }