Merge pull request #11575 from ethereum/fix-invalid-yul-escapes-from-literals-in-assembly-blocks

Fix invalid Yul escapes generated from literals in assembly blocks
This commit is contained in:
chriseth 2021-06-28 14:53:48 +02:00 committed by GitHub
commit b3ac0976fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 11 deletions

View File

@ -3157,5 +3157,5 @@ bool IRGeneratorForStatements::visit(TryCatchClause const& _clause)
string IRGeneratorForStatements::linkerSymbol(ContractDefinition const& _library) const string IRGeneratorForStatements::linkerSymbol(ContractDefinition const& _library) const
{ {
solAssert(_library.isLibrary(), ""); solAssert(_library.isLibrary(), "");
return "linkersymbol(" + util::escapeAndQuoteString(_library.fullyQualifiedName()) + ")"; return "linkersymbol(" + util::escapeAndQuoteYulString(_library.fullyQualifiedName()) + ")";
} }

View File

@ -192,11 +192,13 @@ string solidity::util::formatAsStringOrNumber(string const& _value)
if (c <= 0x1f || c >= 0x7f || c == '"') if (c <= 0x1f || c >= 0x7f || c == '"')
return "0x" + h256(_value, h256::AlignLeft).hex(); return "0x" + h256(_value, h256::AlignLeft).hex();
return escapeAndQuoteString(_value); // The difference in escaping is only in characters below 0x1f and the string does not have them
// so this will work for Solidity strings too.
return escapeAndQuoteYulString(_value);
} }
string solidity::util::escapeAndQuoteString(string const& _input) string solidity::util::escapeAndQuoteYulString(string const& _input)
{ {
string out; string out;
@ -205,18 +207,12 @@ string solidity::util::escapeAndQuoteString(string const& _input)
out += "\\\\"; out += "\\\\";
else if (c == '"') else if (c == '"')
out += "\\\""; out += "\\\"";
else if (c == '\b')
out += "\\b";
else if (c == '\f')
out += "\\f";
else if (c == '\n') else if (c == '\n')
out += "\\n"; out += "\\n";
else if (c == '\r') else if (c == '\r')
out += "\\r"; out += "\\r";
else if (c == '\t') else if (c == '\t')
out += "\\t"; out += "\\t";
else if (c == '\v')
out += "\\v";
else if (!isprint(c, locale::classic())) else if (!isprint(c, locale::classic()))
{ {
ostringstream o; ostringstream o;

View File

@ -554,7 +554,7 @@ std::string formatAsStringOrNumber(std::string const& _value);
/// @returns a string with the usual backslash-escapes for non-ASCII /// @returns a string with the usual backslash-escapes for non-ASCII
/// characters and surrounded by '"'-characters. /// characters and surrounded by '"'-characters.
std::string escapeAndQuoteString(std::string const& _input); std::string escapeAndQuoteYulString(std::string const& _input);
template<typename Container, typename Compare> template<typename Container, typename Compare>
bool containerEqual(Container const& _lhs, Container const& _rhs, Compare&& _compare) bool containerEqual(Container const& _lhs, Container const& _rhs, Compare&& _compare)

View File

@ -57,7 +57,7 @@ string AsmPrinter::operator()(Literal const& _literal) const
break; break;
} }
return escapeAndQuoteString(_literal.value.str()) + appendTypeName(_literal.type); return escapeAndQuoteYulString(_literal.value.str()) + appendTypeName(_literal.type);
} }
string AsmPrinter::operator()(Identifier const& _identifier) const string AsmPrinter::operator()(Identifier const& _identifier) const

View File

@ -0,0 +1,12 @@
contract C {
function f() public pure returns (bytes32 result) {
assembly {
result := hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
}
}
}
// ====
// compileToEwasm: also
// compileViaYul: also
// ----
// f() -> 0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f

View File

@ -0,0 +1,6 @@
{
let name := hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
}
// ====
// dialect: evm
// ----