Skip to content

Commit 813d25b

Browse files
zichanggcommit-bot@chromium.org
authored andcommitted
[dart:io] Clean up on parsing scoped IPv6 InternetAddress
A follow up of https://dart-review.googlesource.com/c/sdk/+/147060 Fixes #42908 Change-Id: I7e1456457552993e21e52f211bdbab42233ced1e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155840 Commit-Queue: Zichang Guo <[email protected]> Reviewed-by: Jonas Termansen <[email protected]>
1 parent 45643c2 commit 813d25b

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

runtime/bin/socket_base.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,10 @@ void FUNCTION_NAME(InternetAddress_ParseScopedLinkLocalAddress)(
254254
AddressList<SocketAddress>* addresses =
255255
SocketBase::LookupAddress(address, type, &os_error);
256256
if (addresses != NULL) {
257-
Dart_Handle list = Dart_NewList(addresses->count());
258-
for (intptr_t i = 0; i < addresses->count(); i++) {
259-
SocketAddress* addr = addresses->GetAt(i);
260-
Dart_ListSetAt(
261-
list, i, Dart_NewInteger(SocketAddress::GetAddrScope(addr->addr())));
262-
}
257+
SocketAddress* addr = addresses->GetAt(0);
258+
Dart_SetReturnValue(
259+
args, Dart_NewInteger(SocketAddress::GetAddrScope(addr->addr())));
263260
delete addresses;
264-
Dart_SetReturnValue(args, list);
265261
} else {
266262
Dart_SetReturnValue(args, DartUtils::NewDartOSError(os_error));
267263
delete os_error;

sdk/lib/_internal/vm/bin/socket_patch.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,25 @@ class _InternetAddress implements InternetAddress {
235235
}
236236
var inAddr = _parse(address);
237237
if (inAddr == null) {
238-
throw ArgumentError.value("Invalid internet address $address");
238+
throw ArgumentError('Invalid internet address $address');
239239
}
240240
InternetAddressType type = inAddr.length == _IPv4AddrLength
241241
? InternetAddressType.IPv4
242242
: InternetAddressType.IPv6;
243243
if (scopeID != null && scopeID.length > 0) {
244244
if (type != InternetAddressType.IPv6) {
245-
throw ArgumentError.value("IPv4 addresses cannot have a scope id");
245+
throw ArgumentError.value(
246+
address, 'address', 'IPv4 addresses cannot have a scope ID');
246247
}
247-
// This is an IPv6 address with scope id.
248-
var list = _parseScopedLinkLocalAddress(originalAddress);
249248

250-
if (list is! OSError && (list as List).isNotEmpty) {
251-
return _InternetAddress(InternetAddressType.IPv6, originalAddress,
252-
null, inAddr, list.first);
249+
final scopeID = _parseScopedLinkLocalAddress(originalAddress);
250+
251+
if (scopeID is int) {
252+
return _InternetAddress(
253+
InternetAddressType.IPv6, originalAddress, null, inAddr, scopeID);
253254
} else {
254255
throw ArgumentError.value(
255-
"Invalid IPv6 address $address with scope ID");
256+
address, 'address', 'Invalid IPv6 address with scope ID');
256257
}
257258
}
258259
return _InternetAddress(type, originalAddress, null, inAddr, 0);
@@ -286,7 +287,7 @@ class _InternetAddress implements InternetAddress {
286287
checkNotNullable(address, "address");
287288
try {
288289
return _InternetAddress.fromString(address);
289-
} catch (e) {
290+
} on ArgumentError catch (_) {
290291
return null;
291292
}
292293
}
@@ -352,8 +353,8 @@ class _InternetAddress implements InternetAddress {
352353

353354
static String _rawAddrToString(Uint8List address)
354355
native "InternetAddress_RawAddrToString";
355-
static List _parseScopedLinkLocalAddress(String address)
356-
native "InternetAddress_ParseScopedLinkLocalAddress";
356+
static dynamic /* int | OSError */ _parseScopedLinkLocalAddress(
357+
String address) native "InternetAddress_ParseScopedLinkLocalAddress";
357358
static Uint8List? _parse(String address) native "InternetAddress_Parse";
358359
}
359360

tests/standalone/io/internet_address_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ void testRawPath() {
209209
Expect.equals(InternetAddressType.unix, address.type);
210210
}
211211

212+
void testInvalidScopedId() {
213+
Expect.throws<ArgumentError>(() => InternetAddress('::1%invalid'), (error) {
214+
return error.toString().contains('scope ID');
215+
});
216+
}
217+
212218
void main() {
213219
testDefaultAddresses();
214220
testConstructor();
@@ -219,4 +225,5 @@ void main() {
219225
testRawAddress();
220226
testRawAddressIPv6();
221227
testRawPath();
228+
testInvalidScopedId();
222229
}

tests/standalone_2/io/internet_address_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ void testRawPath() {
208208
Expect.equals(InternetAddressType.unix, address.type);
209209
}
210210

211+
void testInvalidScopedId() {
212+
Expect.throws<ArgumentError>(() => InternetAddress('::1%invalid'), (error) {
213+
return error.toString().contains('scope ID');
214+
});
215+
}
216+
211217
void main() {
212218
testDefaultAddresses();
213219
testConstructor();
@@ -218,4 +224,5 @@ void main() {
218224
testRawAddress();
219225
testRawAddressIPv6();
220226
testRawPath();
227+
testInvalidScopedId();
221228
}

0 commit comments

Comments
 (0)