Internal error trying to print uninitialized string.

Trying to print the value of a string whose size is not known at
compile-time before it gets assigned a value can lead to the following
internal error:

    (gdb) p my_str
    $1 =
    /[...]/utils.c:1089: internal-error: virtual memory exhausted.

What happens is that my_str is described as a reference to an array
type whose bounds are dynamic. During the read of that variable's
value (in default_read_var_value), we end up resolving dynamic types
which, for reference types, makes us also resolve the target of that
reference type. This means we resolve our variable to a reference
to an array whose bounds are undefined, and unfortunately very far
appart.

So, when we pass that value to ada-valprint, and in particular to
da_val_print_ref, we eventually try to allocate too large of a buffer
corresponding to the (bogus) size of our array, hence the internal
error.

This patch fixes the problem by adding a size_check before trying
to print the dereferenced value. To perform this check, a function
that was previously specific to ada-lang.c (check_size) gets
exported, and renamed to something less prone to name collisions
(ada_ensure_varsize_limit).

gdb/ChangeLog:

        * ada-lang.h (ada_ensure_varsize_limit): Declare.
        * ada-lang.c (check_size): Remove advance declaration.
        (ada_ensure_varsize_limit): Renames check_size.
        Replace calls to check_size by calls to ada_ensure_varsize_limit
        throughout.
        * ada-valprint.c (ada_val_print_ref): Add call to
        ada_ensure_varsize_limit.  Add comment explaining why.

gdb/testsuite/ChangeLog:

        * gdb.ada/str_uninit: New testcase.
This commit is contained in:
Joel Brobecker
2014-12-08 10:37:00 -05:00
parent 3c46a02f50
commit c1b5a1a6e7
9 changed files with 143 additions and 9 deletions

@ -232,8 +232,6 @@ static int ada_is_direct_array_type (struct type *);
static void ada_language_arch_info (struct gdbarch *,
struct language_arch_info *);
static void check_size (const struct type *);
static struct value *ada_index_struct_field (int, struct value *, int,
struct type *);
@ -678,7 +676,7 @@ coerce_unspec_val_to_type (struct value *val, struct type *type)
/* Make sure that the object size is not unreasonable before
trying to allocate some memory for it. */
check_size (type);
ada_ensure_varsize_limit (type);
if (value_lazy (val)
|| TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val)))
@ -740,8 +738,8 @@ lim_warning (const char *format, ...)
i.e. if it would be a bad idea to allocate a value of this type in
GDB. */
static void
check_size (const struct type *type)
void
ada_ensure_varsize_limit (const struct type *type)
{
if (TYPE_LENGTH (type) > varsize_limit)
error (_("object size is larger than varsize-limit"));
@ -2052,7 +2050,7 @@ ada_coerce_to_simple_array (struct value *arr)
if (arrVal == NULL)
error (_("Bounds unavailable for null array pointer."));
check_size (TYPE_TARGET_TYPE (value_type (arrVal)));
ada_ensure_varsize_limit (TYPE_TARGET_TYPE (value_type (arrVal)));
return value_ind (arrVal);
}
else if (ada_is_constrained_packed_array_type (value_type (arr)))
@ -7917,7 +7915,7 @@ ada_template_to_fixed_record_type_1 (struct type *type,
initialized, the type size may be completely bogus and
GDB may fail to allocate a value for it. So check the
size first before creating the value. */
check_size (rtype);
ada_ensure_varsize_limit (rtype);
/* Using plain value_from_contents_and_address here
causes problems because we will end up trying to
resolve a type that is currently being
@ -7965,7 +7963,7 @@ ada_template_to_fixed_record_type_1 (struct type *type,
large (due to an uninitialized variable in the inferior)
that it would cause an overflow when adding it to the
record size. */
check_size (field_type);
ada_ensure_varsize_limit (field_type);
TYPE_FIELD_TYPE (rtype, f) = field_type;
TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
@ -10907,7 +10905,7 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
(ada_aligned_type
(ada_check_typedef (TYPE_TARGET_TYPE (type))));
}
check_size (type);
ada_ensure_varsize_limit (type);
return value_zero (type, lval_memory);
}
else if (TYPE_CODE (type) == TYPE_CODE_INT)