Skip to content

Commit 17d230f

Browse files
Fix String.equals to check for class identifier
1 parent 02e7b4a commit 17d230f

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,6 @@ codet java_string_library_preprocesst::make_equals_function_code(
913913
const source_locationt &loc,
914914
symbol_table_baset &symbol_table)
915915
{
916-
// TODO: Code should return false if Object is not String.
917916
code_blockt code;
918917
exprt::operandst ops;
919918
for(const auto &p : type.parameters())
@@ -923,8 +922,30 @@ codet java_string_library_preprocesst::make_equals_function_code(
923922
}
924923
exprt::operandst args=process_equals_function_operands(
925924
ops, loc, symbol_table, code);
926-
code.add(code_return_function_application(
927-
ID_cprover_string_equal_func, args, type.return_type(), symbol_table));
925+
926+
member_exprt class_identifier(
927+
checked_dereference(ops[1], ops[1].type().subtype()),
928+
"@class_identifier",
929+
string_typet());
930+
931+
// Check the object argument is a String.
932+
equal_exprt arg_is_string(
933+
class_identifier, constant_exprt("java::java.lang.String", string_typet()));
934+
935+
// Check content equality
936+
const symbolt string_equals_sym = get_fresh_aux_symbol(
937+
java_boolean_type(), "string_equals", "str_eq", loc, ID_java, symbol_table);
938+
const symbol_exprt string_equals = string_equals_sym.symbol_expr();
939+
code.add(code_declt(string_equals));
940+
code.add(code_assignt(
941+
string_equals,
942+
make_function_application(
943+
ID_cprover_string_equal_func, args, type.return_type(), symbol_table)));
944+
945+
code.add(code_returnt(if_exprt(
946+
arg_is_string,
947+
string_equals,
948+
from_integer(false, java_boolean_type()))));
928949
return code;
929950
}
930951

0 commit comments

Comments
 (0)