Visitar URL original
Dump the source bytes if incorrect UTF-8 sequences are present by blasten · Pull Request #19 · flutter/packages · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Dump the source bytes if incorrect UTF-8 sequences are present
  • Loading branch information
blasten committed May 1, 2019
commit 815c8251c5136665ea4f5c237d5cb82885ecd7d1
25 changes: 22 additions & 3 deletions packages/multicast_dns/lib/src/packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,18 @@ _FQDNReadResult _readFQDN(
final Uint8List partBytes =
Uint8List.view(data.buffer, offset, partLength);
offset += partLength;
parts.add(utf8.decode(partBytes));

String partString;
try {
partString = utf8.decode(partBytes);
} on FormatException {
throw MDnsDecodeException(offset,
detailError: 'Bad UTF-8 encoding found while decoding string. '

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just message instead of detailError

'The Flutter team would greatly appreciate if you could file a bug or leave a'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is too specific for a general purpose package. the flutter_tool should add the flutter specific phrasing if it catches this error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, we should probably either create a new type of exception or add an error code field to this one. I would probably lean towards a new type of exception that derives from MDnsDecodeException, e.g. MDnsUtf8DecodeException that still has the offset as well as the byte list. That way the tool could do a catch on it and do whatever it thinks is necessary with the byte array.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, I also lean toward a new type of exception.

'comment on the issue https://github.com/flutter/flutter/issues/31854.\n'
'The source bytes were:\n$partBytes\n\n');
}
parts.add(partString);
} else {
break;
}
Expand Down Expand Up @@ -365,11 +376,19 @@ class MDnsDecodeException implements Exception {
/// specified [offset].
///
/// The [offset] parameter should not be null.
const MDnsDecodeException(this.offset) : assert(offset != null);
/// An optional [detailError] message.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...specified [offset], potentially containing additional details in [message].

The [offset] and the [message] parameters must not be null.

const MDnsDecodeException(this.offset, {this.detailError = ''})
: assert(offset != null),
assert(detailError != null);

/// The offset in the packet at which the exception occurred.
final int offset;

/// An optional detail error message.
final String detailError;

@override
String toString() => 'Decoding error at $offset';
String toString() => detailError.isEmpty
? 'Decoding error at $offset'
: 'Decoding error at $offset\n$detailError';
}
31 changes: 31 additions & 0 deletions packages/multicast_dns/test/decode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ void testBadPackages() {
}
}
});

// Expect developers to report the packets that contain invalid UTF-8 code points
// https://github.com/flutter/flutter/issues/31854
test('Dumps the source bytes if incorrect UTF-8 sequences are present', () {
expect(
() => readFQDN(packetWithInvalidUtf8),
throwsA(predicate((error) =>
error is MDnsDecodeException &&
error
.toString()
.contains('https://github.com/flutter/flutter/issues/31854') &&
error.toString().contains(
'[254, 102, 108, 101, 116, 99, 104, 45, 97, 110, 116, 0]'))));
});
}

void testPTRRData() {
Expand Down Expand Up @@ -832,3 +846,20 @@ const List<int> srvRData = <int>[
0x6c,
0x00
];

const List<int> packetWithInvalidUtf8 = <int>[
0x0c,
// Invalid code point from https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
0xfe,
0x66,
0x6c,
0x65,
0x74,
0x63,
0x68,
0x2d,
0x61,
0x6e,
0x74,
0x00,
];