Skip to content

Commit 58f2d59

Browse files
rakudramaCommit Queue
authored andcommitted
[dart2js] Allow malformed UTF-8
The kernel Source class allows malformed UTF-8. Dart2js should too. Hopefully this will allow more informative reports, e.g. #60801, where the location of the crash is hidden by a subsequent crash in crash reporting due to malformed UTF-8. Wrap the source access with a try-catch so that other errors in finding the source lines do not prevent the file name from being displayed. Change-Id: I0f70c2d7a9044732d038a918d120c05961f3d705 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/432101 Reviewed-by: Nate Biggs <[email protected]> Commit-Queue: Stephen Adams <[email protected]>
1 parent 018ad19 commit 58f2d59

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

pkg/compiler/lib/src/io/source_file.dart

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -112,57 +112,67 @@ abstract class SourceFile implements api.Input<Uint8List>, LocationProvider {
112112
}) {
113113
colorize ??= (text) => text;
114114

115-
kernel.Location startLocation = kernelSource.getLocation(uri, start);
116-
kernel.Location endLocation = kernelSource.getLocation(uri, end);
117-
int lineStart = startLocation.line - 1;
118-
int columnStart = startLocation.column - 1;
119-
int lineEnd = endLocation.line - 1;
120-
int columnEnd = endLocation.column - 1;
121-
122115
StringBuffer buf = StringBuffer('$filename:');
123-
if (start != end || start != 0) {
124-
// Line/column info is relevant.
125-
buf.write('${lineStart + 1}:${columnStart + 1}:');
126-
}
127-
buf.write('\n$message\n');
116+
bool wroteMessage = false;
117+
try {
118+
kernel.Location startLocation = kernelSource.getLocation(uri, start);
119+
kernel.Location endLocation = kernelSource.getLocation(uri, end);
120+
int lineStart = startLocation.line - 1;
121+
int columnStart = startLocation.column - 1;
122+
int lineEnd = endLocation.line - 1;
123+
int columnEnd = endLocation.column - 1;
124+
125+
if (start != end || start != 0) {
126+
// Line/column info is relevant.
127+
buf.write('${lineStart + 1}:${columnStart + 1}:');
128+
}
129+
buf.write('\n$message\n');
130+
wroteMessage = true;
128131

129-
if (start != end && includeSourceLine) {
130-
if (lineStart == lineEnd) {
131-
String textLine = kernelSource.getTextLine(startLocation.line)!;
132+
if (start != end && includeSourceLine) {
133+
if (lineStart == lineEnd) {
134+
String textLine = kernelSource.getTextLine(startLocation.line)!;
132135

133-
int toColumn = min(columnStart + (end - start), textLine.length);
134-
buf.write(textLine.substring(0, columnStart));
135-
buf.write(colorize(textLine.substring(columnStart, toColumn)));
136-
buf.writeln(textLine.substring(toColumn));
136+
int toColumn = min(columnStart + (end - start), textLine.length);
137+
buf.write(textLine.substring(0, columnStart));
138+
buf.write(colorize(textLine.substring(columnStart, toColumn)));
139+
buf.writeln(textLine.substring(toColumn));
137140

138-
int i = 0;
139-
for (; i < columnStart; i++) {
140-
buf.write(' ');
141-
}
141+
int i = 0;
142+
for (; i < columnStart; i++) {
143+
buf.write(' ');
144+
}
142145

143-
for (; i < toColumn; i++) {
144-
buf.write(colorize('^'));
145-
}
146-
} else {
147-
for (int line = lineStart; line <= lineEnd; line++) {
148-
String textLine = kernelSource.getTextLine(line + 1)!;
149-
if (line == lineStart) {
150-
if (columnStart > textLine.length) {
151-
columnStart = textLine.length;
152-
}
153-
buf.write(textLine.substring(0, columnStart));
154-
buf.writeln(colorize(textLine.substring(columnStart)));
155-
} else if (line == lineEnd) {
156-
if (columnEnd > textLine.length) {
157-
columnEnd = textLine.length;
146+
for (; i < toColumn; i++) {
147+
buf.write(colorize('^'));
148+
}
149+
} else {
150+
for (int line = lineStart; line <= lineEnd; line++) {
151+
String textLine = kernelSource.getTextLine(line + 1)!;
152+
if (line == lineStart) {
153+
if (columnStart > textLine.length) {
154+
columnStart = textLine.length;
155+
}
156+
buf.write(textLine.substring(0, columnStart));
157+
buf.writeln(colorize(textLine.substring(columnStart)));
158+
} else if (line == lineEnd) {
159+
if (columnEnd > textLine.length) {
160+
columnEnd = textLine.length;
161+
}
162+
buf.write(colorize(textLine.substring(0, columnEnd)));
163+
buf.writeln(textLine.substring(columnEnd));
164+
} else {
165+
buf.writeln(colorize(textLine));
158166
}
159-
buf.write(colorize(textLine.substring(0, columnEnd)));
160-
buf.writeln(textLine.substring(columnEnd));
161-
} else {
162-
buf.writeln(colorize(textLine));
163167
}
164168
}
165169
}
170+
} catch (e) {
171+
if (!wroteMessage) {
172+
buf.write('+$start');
173+
buf.write('\n$message\n');
174+
buf.write('$e\n');
175+
}
166176
}
167177

168178
return buf.toString();
@@ -186,7 +196,7 @@ class Utf8BytesSourceFile extends SourceFile {
186196

187197
@override
188198
String slowText() {
189-
return utf8.decoder.convert(content);
199+
return utf8.decode(content, allowMalformed: true);
190200
}
191201

192202
@override

0 commit comments

Comments
 (0)