Skip to content

Commit b2789ea

Browse files
Petr Bauchpetr-bauch
authored andcommitted
Clear get_pointer_value
Unify handling pointers-to-members/to-char/to-non-char and add comments.
1 parent 3e22387 commit b2789ea

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/memory-analyzer/analyze_symbol.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -341,28 +341,51 @@ exprt gdb_value_extractort::get_pointer_value(
341341

342342
if(!memory_location.is_null())
343343
{
344-
if(is_c_char_type(expr.type().subtype()))
344+
// pointers-to-char can point to members as well, e.g. char[]
345+
if(points_to_member(value))
345346
{
346-
return get_char_pointer_value(expr, memory_location, location);
347+
const auto target_expr =
348+
get_pointer_to_member_value(expr, value, location);
349+
CHECK_RETURN(target_expr.id() != ID_nil);
350+
const auto result_expr = address_of_exprt(target_expr);
351+
CHECK_RETURN(result_expr.type() == zero_expr.type());
352+
return result_expr;
347353
}
348-
else
354+
355+
// non-member: split for char/non-char
356+
const auto target_expr =
357+
is_c_char(expr.type().subtype())
358+
? get_char_pointer_value(expr, memory_location, location)
359+
: get_non_char_pointer_value(expr, memory_location, location);
360+
361+
// postpone if we cannot resolve now
362+
if(target_expr.id() == ID_nil)
363+
{
364+
outstanding_assignments[expr] = memory_location;
365+
return zero_expr;
366+
}
367+
368+
// the pointee was (probably) dynamically allocated (but the allocation
369+
// would not be visible in the snapshot) so we pretend it is statically
370+
// allocated (we have the value) and return address to the first element
371+
// of the array (instead of the array as char*)
372+
if(target_expr.type().id() == ID_array)
349373
{
350-
const exprt target_expr =
351-
points_to_member(value)
352-
? get_pointer_to_member_value(expr, value, location)
353-
: get_non_char_pointer_value(expr, memory_location, location);
354-
355-
if(target_expr.id() == ID_nil)
356-
{
357-
outstanding_assignments[expr] = memory_location;
358-
}
359-
else
360-
{
361-
const auto result_expr = address_of_exprt(target_expr);
362-
CHECK_RETURN(result_expr.type() == zero_expr.type());
363-
return result_expr;
364-
}
374+
const auto result_indexed_expr = get_subexpression_at_offset(
375+
target_expr, 0, zero_expr.type().subtype(), ns);
376+
CHECK_RETURN(result_indexed_expr.has_value());
377+
const auto result_expr = address_of_exprt{*result_indexed_expr};
378+
return result_expr;
365379
}
380+
381+
// if the types match return right away
382+
if(target_expr.type() == zero_expr.type())
383+
return target_expr;
384+
385+
//otherwise the address of target should type-match
386+
const auto result_expr = address_of_exprt(target_expr);
387+
CHECK_RETURN(result_expr.type() == zero_expr.type());
388+
return result_expr;
366389
}
367390

368391
return zero_expr;

0 commit comments

Comments
 (0)