Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1007)

Unified Diff: sdk/lib/convert/latin1.dart

Issue 1100873003: Avoid checking Uint8List for non-Latin-1 characters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/convert/chunked_conversion.dart ('k') | sdk/lib/convert/utf.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/convert/latin1.dart
diff --git a/sdk/lib/convert/latin1.dart b/sdk/lib/convert/latin1.dart
index 7f7fef883ca18a23d209a27193fdfea92e7ff277..98d7ba57aa5f007d410090992c18ed36c4e928e5 100644
--- a/sdk/lib/convert/latin1.dart
+++ b/sdk/lib/convert/latin1.dart
@@ -116,6 +116,7 @@ class _Latin1DecoderSink extends ByteConversionSinkBase {
void close() {
_sink.close();
+ _sink = null;
}
void add(List<int> source) {
@@ -132,19 +133,40 @@ class _Latin1DecoderSink extends ByteConversionSinkBase {
}
void addSlice(List<int> source, int start, int end, bool isLast) {
- RangeError.checkValidRange(start, end, source.length);
+ end = RangeError.checkValidRange(start, end, source.length);
+ if (start == end) return;
+ if (source is! Uint8List) {
+ // List may contain value outside of the 0..255 range. If so, throw.
+ // Technically, we could excuse Uint8ClampedList as well, but it unlikely
+ // to be relevant.
+ _checkValidLatin1(source, start, end);
+ }
+ _addSliceToSink(source, start, end, isLast);
+ }
+
+ static void _checkValidLatin1(List<int> source, int start, int end) {
+ int mask = 0;
for (int i = start; i < end; i++) {
- int char = source[i];
- if (char > _LATIN1_MASK || char < 0) {
- throw new FormatException("Source contains non-Latin-1 characters.");
- }
+ mask |= source[i];
}
- if (start < end) {
- _addSliceToSink(source, start, end, isLast);
+ if (mask >= 0 && mask <= _LATIN1_MASK) {
+ return;
}
- if (isLast) {
- close();
+ _reportInvalidLatin1(source, start, end); // Always throws.
+ }
+
+
+ static void _reportInvalidLatin1(List<int> source, int start, int end) {
+ // Find the index of the first non-Latin-1 character code.
+ for (int i = start; i < end; i++) {
+ int char = source[i];
+ if (char < 0 || char > _LATIN1_MASK) {
+ throw new FormatException("Source contains non-Latin-1 characters.",
+ source, i);
+ }
}
+ // Unreachable - we only call the function if the loop above throws.
+ assert(false);
}
}
« no previous file with comments | « sdk/lib/convert/chunked_conversion.dart ('k') | sdk/lib/convert/utf.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698