[dart:io] Clean up on parsing scoped IPv6 InternetAddress
A follow up of https://dart-review.googlesource.com/c/sdk/+/147060
Fixes https://github.com/dart-lang/sdk/issues/42908
Change-Id: I7e1456457552993e21e52f211bdbab42233ced1e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155840
Commit-Queue: Zichang Guo <zichangguo@google.com>
Reviewed-by: Jonas Termansen <sortie@google.com>
diff --git a/runtime/bin/socket_base.cc b/runtime/bin/socket_base.cc
index 4e61ce4..e90b138 100644
--- a/runtime/bin/socket_base.cc
+++ b/runtime/bin/socket_base.cc
@@ -254,14 +254,10 @@
AddressList<SocketAddress>* addresses =
SocketBase::LookupAddress(address, type, &os_error);
if (addresses != NULL) {
- Dart_Handle list = Dart_NewList(addresses->count());
- for (intptr_t i = 0; i < addresses->count(); i++) {
- SocketAddress* addr = addresses->GetAt(i);
- Dart_ListSetAt(
- list, i, Dart_NewInteger(SocketAddress::GetAddrScope(addr->addr())));
- }
+ SocketAddress* addr = addresses->GetAt(0);
+ Dart_SetReturnValue(
+ args, Dart_NewInteger(SocketAddress::GetAddrScope(addr->addr())));
delete addresses;
- Dart_SetReturnValue(args, list);
} else {
Dart_SetReturnValue(args, DartUtils::NewDartOSError(os_error));
delete os_error;
diff --git a/sdk/lib/_internal/vm/bin/socket_patch.dart b/sdk/lib/_internal/vm/bin/socket_patch.dart
index a4cd2f2..abd886f 100644
--- a/sdk/lib/_internal/vm/bin/socket_patch.dart
+++ b/sdk/lib/_internal/vm/bin/socket_patch.dart
@@ -235,24 +235,25 @@
}
var inAddr = _parse(address);
if (inAddr == null) {
- throw ArgumentError.value("Invalid internet address $address");
+ throw ArgumentError('Invalid internet address $address');
}
InternetAddressType type = inAddr.length == _IPv4AddrLength
? InternetAddressType.IPv4
: InternetAddressType.IPv6;
if (scopeID != null && scopeID.length > 0) {
if (type != InternetAddressType.IPv6) {
- throw ArgumentError.value("IPv4 addresses cannot have a scope id");
+ throw ArgumentError.value(
+ address, 'address', 'IPv4 addresses cannot have a scope ID');
}
- // This is an IPv6 address with scope id.
- var list = _parseScopedLinkLocalAddress(originalAddress);
- if (list is! OSError && (list as List).isNotEmpty) {
- return _InternetAddress(InternetAddressType.IPv6, originalAddress,
- null, inAddr, list.first);
+ final scopeID = _parseScopedLinkLocalAddress(originalAddress);
+
+ if (scopeID is int) {
+ return _InternetAddress(
+ InternetAddressType.IPv6, originalAddress, null, inAddr, scopeID);
} else {
throw ArgumentError.value(
- "Invalid IPv6 address $address with scope ID");
+ address, 'address', 'Invalid IPv6 address with scope ID');
}
}
return _InternetAddress(type, originalAddress, null, inAddr, 0);
@@ -286,7 +287,7 @@
checkNotNullable(address, "address");
try {
return _InternetAddress.fromString(address);
- } catch (e) {
+ } on ArgumentError catch (_) {
return null;
}
}
@@ -352,8 +353,8 @@
static String _rawAddrToString(Uint8List address)
native "InternetAddress_RawAddrToString";
- static List _parseScopedLinkLocalAddress(String address)
- native "InternetAddress_ParseScopedLinkLocalAddress";
+ static dynamic /* int | OSError */ _parseScopedLinkLocalAddress(
+ String address) native "InternetAddress_ParseScopedLinkLocalAddress";
static Uint8List? _parse(String address) native "InternetAddress_Parse";
}
diff --git a/tests/standalone/io/internet_address_test.dart b/tests/standalone/io/internet_address_test.dart
index d4d6a06..6671b92 100644
--- a/tests/standalone/io/internet_address_test.dart
+++ b/tests/standalone/io/internet_address_test.dart
@@ -209,6 +209,12 @@
Expect.equals(InternetAddressType.unix, address.type);
}
+void testInvalidScopedId() {
+ Expect.throws<ArgumentError>(() => InternetAddress('::1%invalid'), (error) {
+ return error.toString().contains('scope ID');
+ });
+}
+
void main() {
testDefaultAddresses();
testConstructor();
@@ -219,4 +225,5 @@
testRawAddress();
testRawAddressIPv6();
testRawPath();
+ testInvalidScopedId();
}
diff --git a/tests/standalone_2/io/internet_address_test.dart b/tests/standalone_2/io/internet_address_test.dart
index 6d2dd54..47fc491 100644
--- a/tests/standalone_2/io/internet_address_test.dart
+++ b/tests/standalone_2/io/internet_address_test.dart
@@ -208,6 +208,12 @@
Expect.equals(InternetAddressType.unix, address.type);
}
+void testInvalidScopedId() {
+ Expect.throws<ArgumentError>(() => InternetAddress('::1%invalid'), (error) {
+ return error.toString().contains('scope ID');
+ });
+}
+
void main() {
testDefaultAddresses();
testConstructor();
@@ -218,4 +224,5 @@
testRawAddress();
testRawAddressIPv6();
testRawPath();
+ testInvalidScopedId();
}