Skip to content

Commit 46cc6f3

Browse files
committed
Clear get_pointer_value
Unify handling pointers-to-members/to-char/to-non-char and add comments.
1 parent f6a8415 commit 46cc6f3

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
@@ -338,28 +338,51 @@ exprt gdb_value_extractort::get_pointer_value(
338338

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

365388
return zero_expr;

0 commit comments

Comments
 (0)