[test_runner] Fix RangeError when parsing static errors

Previously it tested if it was at the end of file (defined as
_currentLine >= _lines.length), if it was not it would try to read the
_next_ line (_peek(1)), meaning that if _currentLine = _lines.length - 1
it would _peek(1) and try to read _lines[lines.length] and crash with a
range error. This CL fixes it by actually checking if it can safely peek
the amount it wants to.

Change-Id: I2e66c539e823f0b39bd0da39e2b2277a9e6c9501
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150624
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Jens Johansen 2020-06-10 17:33:37 +00:00 committed by commit-bot@chromium.org
parent 5900da5418
commit 214cdbdc81
2 changed files with 15 additions and 4 deletions

View file

@ -456,7 +456,7 @@ class _ErrorExpectationParser {
_ErrorExpectationParser(String source) : _lines = source.split("\n");
List<StaticError> _parse() {
while (!_isAtEnd) {
while (_canPeek(0)) {
var sourceLine = _peek(0);
var match = _caretLocationRegExp.firstMatch(sourceLine);
@ -504,7 +504,7 @@ class _ErrorExpectationParser {
var startLine = _currentLine;
while (!_isAtEnd) {
while (_canPeek(1)) {
var match = _errorMessageRegExp.firstMatch(_peek(1));
if (match == null) break;
@ -516,7 +516,7 @@ class _ErrorExpectationParser {
_advance();
// Consume as many additional error message lines as we find.
while (!_isAtEnd) {
while (_canPeek(1)) {
var nextLine = _peek(1);
// A location line shouldn't be treated as part of the message.
@ -585,7 +585,7 @@ class _ErrorExpectationParser {
markerEndLine: _currentLine));
}
bool get _isAtEnd => _currentLine >= _lines.length;
bool _canPeek(int offset) => _currentLine + offset < _lines.length;
void _advance() {
_currentLine++;

View file

@ -287,6 +287,17 @@ someBadCode();
/\/ [cfe] Wrong 1.
""");
// Don't crash with RangeError.
expectUpdate("""
x
// [error line 1, column 1, length 0]
// [cfe] Whatever""", errors: [
makeError(line: 1, column: 1, length: 0, cfeError: "Foo"),
], expected: """
x
// [error line 1, column 1, length 0]
// [cfe] Foo""");
regression();
}