@@ -300,18 +300,61 @@ exprt gdb_value_extractort::get_non_char_pointer_value(
300300 symbol_exprt dummy (expr.type ());
301301 code_blockt assignments;
302302
303+ const auto zero_expr = zero_initializer (target_type, location, ns);
304+ CHECK_RETURN (zero_expr);
305+
306+ // Check if pointer was dynamically allocated (via malloc). If so we will
307+ // replace the pointee with a static array filled with values stored at the
308+ // expected positions. Since the allocated size is over-approximation we may
309+ // end up querying pass the allocated bounds and building larger array with
310+ // meaningless values.
311+ size_t allocated_size =
312+ gdb_api.query_malloc_size (c_converter.convert (expr));
313+ // get the sizeof(target_type) and thus the number of elements
314+ const auto target_size_bits = pointer_offset_bits (target_type, ns);
315+ CHECK_RETURN (target_size_bits.has_value ());
316+ const auto number_of_elements = allocated_size / (*target_size_bits / 8 );
317+ if (number_of_elements > 1 )
318+ {
319+ array_exprt::operandst elements;
320+ // build the operands by querying for an index expression
321+ for (size_t i = 0 ; i < number_of_elements; i++)
322+ {
323+ const auto sub_expr_value = get_expr_value (
324+ index_exprt{expr, from_integer (i, index_type ())},
325+ *zero_expr,
326+ location);
327+ elements.push_back (sub_expr_value);
328+ }
329+ CHECK_RETURN (elements.size () == number_of_elements);
330+
331+ // knowing the number of elements we can build the type
332+ const typet target_array_type =
333+ array_typet{target_type, from_integer (elements.size (), index_type ())};
334+
335+ array_exprt new_array{elements, to_array_type (target_array_type)};
336+
337+ // allocate a new symbol for the temporary static array
338+ symbol_exprt array_dummy (
339+ pointer_typet (target_array_type, config.ansi_c .pointer_width ));
340+ const auto array_symbol =
341+ allocate_objects.allocate_automatic_local_object (
342+ assignments, array_dummy, target_array_type);
343+
344+ // add assignment of value to newly created symbol
345+ add_assignment (array_symbol, new_array);
346+ values[memory_location] = array_symbol;
347+ return array_symbol;
348+ }
349+
303350 const symbol_exprt new_symbol =
304351 to_symbol_expr (allocate_objects.allocate_automatic_local_object (
305352 assignments, dummy, target_type));
306353
307354 dereference_exprt dereference_expr (expr);
308355
309- const auto zero_expr = zero_initializer (target_type, location, ns);
310- CHECK_RETURN (zero_expr);
311-
312356 const exprt target_expr =
313357 get_expr_value (dereference_expr, *zero_expr, location);
314-
315358 // add assignment of value to newly created symbol
316359 add_assignment (new_symbol, target_expr);
317360
0 commit comments