Skip to content

Switch to stateless Java string escaping #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/java_bytecode/java_bytecode_typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,3 @@ bool java_bytecode_typecheck(
// fail for now
return true;
}

// Static members of java_bytecode_typecheckt:
std::map<irep_idt, irep_idt>
java_bytecode_typecheckt::string_literal_to_symbol_name;
std::map<irep_idt, size_t>
java_bytecode_typecheckt::escaped_string_literal_count;
2 changes: 0 additions & 2 deletions src/java_bytecode/java_bytecode_typecheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class java_bytecode_typecheckt:public typecheckt
virtual std::string to_string(const typet &type);

std::set<irep_idt> already_typechecked;
static std::map<irep_idt, irep_idt> string_literal_to_symbol_name;
static std::map<irep_idt, size_t> escaped_string_literal_count;
};

#endif // CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_TYPECHECK_H
45 changes: 25 additions & 20 deletions src/java_bytecode/java_bytecode_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Author: Daniel Kroening, [email protected]

\*******************************************************************/

#include <iomanip>

#include <util/std_expr.h>
#include <util/prefix.h>

Expand Down Expand Up @@ -91,11 +93,23 @@ void java_bytecode_typecheckt::typecheck_expr_java_new_array(
typecheck_type(type);
}

static void escape_non_alnum(std::string &toescape)
static std::string escape_non_alnum(const std::string &toescape)
{
std::ostringstream escaped;
for(auto &ch : toescape)
if(!isalnum(ch))
ch='_';
{
if(ch=='_')
escaped << "__";
else if(isalnum(ch))
escaped << ch;
else
escaped << '_'
<< std::hex
<< std::setfill('0')
<< std::setw(2)
<< (unsigned int)ch;
}
return escaped.str();
}

/*******************************************************************\
Expand All @@ -115,29 +129,20 @@ void java_bytecode_typecheckt::typecheck_expr_java_string_literal(exprt &expr)
const irep_idt value=expr.get(ID_value);
const symbol_typet string_type("java::java.lang.String");

auto findit=string_literal_to_symbol_name.find(value);
if(findit!=string_literal_to_symbol_name.end())
std::string escaped_symbol_name=
"java::java.lang.String.Literal.";
escaped_symbol_name+=escape_non_alnum(id2string(value));

auto findit=symbol_table.symbols.find(escaped_symbol_name);
if(findit!=symbol_table.symbols.end())
{
expr=symbol_exprt(findit->second, pointer_typet(string_type));
expr=findit->second.symbol_expr();
return;
}

// Create a new symbol:
std::ostringstream identifier_str;
std::string escaped=id2string(value);
escape_non_alnum(escaped);
identifier_str << "java::java.lang.String.Literal." << escaped;
// Avoid naming clashes by virtue of escaping:
// NOTE this increments the count stored in the map.
size_t unique_num=++(escaped_string_literal_count[identifier_str.str()]);
if(unique_num!=1)
identifier_str << unique_num;

irep_idt identifier_id=identifier_str.str();
string_literal_to_symbol_name.insert(std::make_pair(value, identifier_id));

symbolt new_symbol;
new_symbol.name=identifier_id;
new_symbol.name=escaped_symbol_name;
new_symbol.type=pointer_typet(string_type);
new_symbol.base_name="Literal";
new_symbol.pretty_name=value;
Expand Down