Skip to content

Commit 8f833f4

Browse files
authored
Merge pull request #10618 from ethereum/develop
Merge develop into breaking.
2 parents b764e06 + e347545 commit 8f833f4

34 files changed

+340
-11
lines changed

Changelog.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,22 @@ AST Changes:
4545
### 0.7.6 (unreleased)
4646

4747
Language Features:
48+
* Code generator: Support conversion from calldata slices to memory and storage arrays.
4849
* Code generator: Support copying dynamically encoded structs from calldata to memory.
4950
* Code generator: Support copying of nested arrays from calldata to memory.
50-
* Code generator: Support conversion from calldata slices to memory and storage arrays.
51+
* Scanner: Generate a parser error when comments or unicode strings contain an unbalanced or underflowing set of unicode direction override markers (LRO, RLO, LRE, RLE, PDF).
5152
* The fallback function can now also have a single ``calldata`` argument (equaling ``msg.data``) and return ``bytes memory`` (which will not be ABI-encoded but returned as-is).
5253
* Wasm backend: Add ``i32.select`` and ``i64.select`` instructions.
5354

5455
Compiler Features:
5556
* Build System: Optionally support dynamic loading of Z3 and use that mechanism for Linux release builds.
5657
* Code Generator: Avoid memory allocation for default value if it is not used.
58+
* SMTChecker: Create underflow and overflow verification targets for increment/decrement in the CHC engine.
5759
* SMTChecker: Report struct values in counterexamples from CHC engine.
5860
* SMTChecker: Support early returns in the CHC engine.
5961
* SMTChecker: Support getters.
6062
* SMTChecker: Support named arguments in function calls.
6163
* SMTChecker: Support struct constructor.
62-
* SMTChecker: Create underflow and overflow verification targets for increment/decrement in the CHC engine.
6364
* Standard-Json: Move the recently introduced ``modelCheckerSettings`` key to ``settings.modelChecker``.
6465
* Standard-Json: Properly filter the requested output artifacts.
6566

liblangutil/CharStream.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ class CharStream
9898
std::tuple<int, int> translatePositionToLineColumn(int _position) const;
9999
///@}
100100

101+
/// Tests whether or not given octet sequence is present at the current position in stream.
102+
/// @returns true if the sequence could be found, false otherwise.
103+
bool prefixMatch(std::string_view _sequence)
104+
{
105+
if (isPastEndOfInput(_sequence.size()))
106+
return false;
107+
108+
for (size_t i = 0; i < _sequence.size(); ++i)
109+
if (_sequence[i] != get(i))
110+
return false;
111+
112+
return true;
113+
}
114+
101115
private:
102116
std::string m_source;
103117
std::string m_name;

liblangutil/Scanner.cpp

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@
5454
#include <liblangutil/Exceptions.h>
5555
#include <liblangutil/Scanner.h>
5656

57-
#include <algorithm>
57+
#include <boost/algorithm/string/classification.hpp>
58+
5859
#include <optional>
59-
#include <ostream>
60+
#include <string_view>
6061
#include <tuple>
6162

6263
using namespace std;
@@ -79,6 +80,8 @@ string to_string(ScannerError _errorCode)
7980
case ScannerError::IllegalExponent: return "Invalid exponent.";
8081
case ScannerError::IllegalNumberEnd: return "Identifier-start is not allowed at end of a number.";
8182
case ScannerError::OctalNotAllowed: return "Octal numbers not allowed.";
83+
case ScannerError::DirectionalOverrideUnderflow: return "Unicode direction override underflow in comment or string literal.";
84+
case ScannerError::DirectionalOverrideMismatch: return "Mismatching directional override markers in comment or string literal.";
8285
default:
8386
solAssert(false, "Unhandled case in to_string(ScannerError)");
8487
return "";
@@ -271,12 +274,61 @@ bool Scanner::skipWhitespaceExceptUnicodeLinebreak()
271274
return sourcePos() != startPosition;
272275
}
273276

277+
278+
namespace
279+
{
280+
281+
/// Tries to scan for an RLO/LRO/RLE/LRE/PDF and keeps track of script writing direction override depth.
282+
///
283+
/// @returns ScannerError::NoError in case of successful parsing and directional encodings are paired
284+
/// and error code in case the input's lexical parser state is invalid and this error should be reported
285+
/// to the user.
286+
static ScannerError validateBiDiMarkup(CharStream& _stream, size_t _startPosition)
287+
{
288+
static array<pair<string_view, int>, 5> constexpr directionalSequences{
289+
pair<string_view, int>{"\xE2\x80\xAD", 1}, // U+202D (LRO - Left-to-Right Override)
290+
pair<string_view, int>{"\xE2\x80\xAE", 1}, // U+202E (RLO - Right-to-Left Override)
291+
pair<string_view, int>{"\xE2\x80\xAA", 1}, // U+202A (LRE - Left-to-Right Embedding)
292+
pair<string_view, int>{"\xE2\x80\xAB", 1}, // U+202B (RLE - Right-to-Left Embedding)
293+
pair<string_view, int>{"\xE2\x80\xAC", -1} // U+202C (PDF - Pop Directional Formatting
294+
};
295+
296+
size_t endPosition = _stream.position();
297+
_stream.setPosition(_startPosition);
298+
299+
int directionOverrideDepth = 0;
300+
301+
for (size_t currentPos = _startPosition; currentPos < endPosition; ++currentPos)
302+
{
303+
_stream.setPosition(currentPos);
304+
305+
for (auto const& [sequence, depthChange]: directionalSequences)
306+
if (_stream.prefixMatch(sequence))
307+
directionOverrideDepth += depthChange;
308+
309+
if (directionOverrideDepth < 0)
310+
return ScannerError::DirectionalOverrideUnderflow;
311+
}
312+
313+
_stream.setPosition(endPosition);
314+
315+
return directionOverrideDepth > 0 ? ScannerError::DirectionalOverrideMismatch : ScannerError::NoError;
316+
}
317+
318+
}
319+
274320
Token Scanner::skipSingleLineComment()
275321
{
276322
// Line terminator is not part of the comment. If it is a
277323
// non-ascii line terminator, it will result in a parser error.
324+
size_t startPosition = m_source->position();
278325
while (!isUnicodeLinebreak())
279-
if (!advance()) break;
326+
if (!advance())
327+
break;
328+
329+
ScannerError unicodeDirectionError = validateBiDiMarkup(*m_source, startPosition);
330+
if (unicodeDirectionError != ScannerError::NoError)
331+
return setError(unicodeDirectionError);
280332

281333
return Token::Whitespace;
282334
}
@@ -349,16 +401,21 @@ size_t Scanner::scanSingleLineDocComment()
349401

350402
Token Scanner::skipMultiLineComment()
351403
{
404+
size_t startPosition = m_source->position();
352405
while (!isSourcePastEndOfInput())
353406
{
354-
char ch = m_char;
407+
char prevChar = m_char;
355408
advance();
356409

357410
// If we have reached the end of the multi-line comment, we
358411
// consume the '/' and insert a whitespace. This way all
359412
// multi-line comments are treated as whitespace.
360-
if (ch == '*' && m_char == '/')
413+
if (prevChar == '*' && m_char == '/')
361414
{
415+
ScannerError unicodeDirectionError = validateBiDiMarkup(*m_source, startPosition);
416+
if (unicodeDirectionError != ScannerError::NoError)
417+
return setError(unicodeDirectionError);
418+
362419
m_char = ' ';
363420
return Token::Whitespace;
364421
}
@@ -776,6 +833,7 @@ bool Scanner::isUnicodeLinebreak()
776833

777834
Token Scanner::scanString(bool const _isUnicode)
778835
{
836+
size_t startPosition = m_source->position();
779837
char const quote = m_char;
780838
advance(); // consume quote
781839
LiteralScope literal(this, LITERAL_TYPE_STRING);
@@ -803,6 +861,14 @@ Token Scanner::scanString(bool const _isUnicode)
803861
}
804862
if (m_char != quote)
805863
return setError(ScannerError::IllegalStringEndQuote);
864+
865+
if (_isUnicode)
866+
{
867+
ScannerError unicodeDirectionError = validateBiDiMarkup(*m_source, startPosition);
868+
if (unicodeDirectionError != ScannerError::NoError)
869+
return setError(unicodeDirectionError);
870+
}
871+
806872
literal.complete();
807873
advance(); // consume quote
808874
return _isUnicode ? Token::UnicodeStringLiteral : Token::StringLiteral;

liblangutil/Scanner.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ enum class ScannerError
8989
IllegalExponent,
9090
IllegalNumberEnd,
9191

92+
DirectionalOverrideUnderflow,
93+
DirectionalOverrideMismatch,
94+
9295
OctalNotAllowed,
9396
};
9497

@@ -183,6 +186,7 @@ class Scanner
183186
///@}
184187

185188
private:
189+
186190
inline Token setError(ScannerError _error) noexcept
187191
{
188192
m_tokens[NextNext].error = _error;

libyul/AsmParser.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ variant<Literal, Identifier> Parser::parseLiteralOrIdentifier()
327327
case Token::HexStringLiteral:
328328
fatalParserError(3772_error, "Hex literals are not valid in this context.");
329329
break;
330+
case Token::Illegal:
331+
fatalParserError(1465_error, "Illegal token: " + to_string(m_scanner->currentError()));
332+
break;
330333
default:
331334
fatalParserError(1856_error, "Literal or identifier expected.");
332335
}

scripts/check_style.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
REPO_ROOT="$(dirname "$0")"/..
77
cd $REPO_ROOT
88

9-
WHITESPACE=$(git grep -n -I -E "^.*[[:space:]]+$" | grep -v "test/libsolidity/ASTJSON\|test/libsolidity/ASTRecoveryTests\|test/compilationTests/zeppelin/LICENSE")
9+
WHITESPACE=$(git grep -n -I -E "^.*[[:space:]]+$" |
10+
grep -v "test/libsolidity/ASTJSON\|test/libsolidity/ASTRecoveryTests\|test/compilationTests/zeppelin/LICENSE" |
11+
grep -v -E "test/libsolidity/syntaxTests/comments/unicode_direction_override_1.sol"
12+
)
1013

1114
if [[ "$WHITESPACE" != "" ]]
1215
then

scripts/test_antlr_grammar.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ done < <(
116116
grep -riL -E \
117117
"^\/\/ (Syntax|Type|Declaration)Error|^\/\/ ParserError (2837|3716|3997|5333|6275|6281|6933|7319)|^==== Source:" \
118118
"${ROOT_DIR}/test/libsolidity/syntaxTests" \
119-
"${ROOT_DIR}/test/libsolidity/semanticTests" \
119+
"${ROOT_DIR}/test/libsolidity/semanticTests" |
120+
grep -v -E 'comments/.*_direction_override.*.sol' |
121+
grep -v -E 'literals/.*_direction_override.*.sol'
122+
# Skipping the unicode tests as I couldn't adapt the lexical grammar to recursively counting RLO/LRO/PDF's.
120123
)
121124

122125
YUL_FILES=()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f() public pure
3+
{
4+
// PDF
5+
/*underflow ‬*/
6+
}
7+
}
8+
// ----
9+
// ParserError 8936: (71-83): Unicode direction override underflow in comment or string literal.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f() public pure
3+
{
4+
// PDF PDF
5+
/*underflow ‬‬*/
6+
}
7+
}
8+
// ----
9+
// ParserError 8936: (75-87): Unicode direction override underflow in comment or string literal.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f() public pure
3+
{
4+
// RLO
5+
/*overflow ‮*/
6+
}
7+
}
8+
// ----
9+
// ParserError 8936: (71-86): Mismatching directional override markers in comment or string literal.

0 commit comments

Comments
 (0)