Skip to content

Commit 81c3e8c

Browse files
lrhncommit-bot@chromium.org
authored andcommitted
* Rename `completer` to `result` (its role, rather than repeating its type). * Change &times; in non-dartdoc comment to the actual character. * Throw on an unexpected source address argument type. Also handle failing unix socket connections better. The current code did not account for all possible return values from the native connect functions. Likely, those other values never occurred, but unless it's proven that they can't, not even in unsound mode, the code should be prepared for them. (And added missing `return` to the native code). TEST= Refactoring, no change to tests. Change-Id: Ie27670f62ae6ecc64dc045c28869e3d5ab218fda Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175042 Commit-Queue: Lasse R.H. Nielsen <[email protected]> Reviewed-by: Martin Kustermann <[email protected]> Reviewed-by: Nate Bosch <[email protected]>
1 parent 7316f34 commit 81c3e8c

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

runtime/bin/socket.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ void FUNCTION_NAME(Socket_CreateUnixDomainBindConnect)(
431431
RawAddr sourceAddr;
432432
address = Dart_GetNativeArgument(args, 2);
433433
if (Dart_IsNull(address)) {
434-
Dart_SetReturnValue(args,
434+
return Dart_SetReturnValue(
435+
args,
435436
DartUtils::NewDartArgumentError("expect address to be of type String"));
436437
}
437438
result = SocketAddress::GetUnixDomainSockAddr(
@@ -462,7 +463,8 @@ void FUNCTION_NAME(Socket_CreateUnixDomainConnect)(Dart_NativeArguments args) {
462463
RawAddr addr;
463464
Dart_Handle address = Dart_GetNativeArgument(args, 1);
464465
if (Dart_IsNull(address)) {
465-
Dart_SetReturnValue(args,
466+
return Dart_SetReturnValue(
467+
args,
466468
DartUtils::NewDartArgumentError("expect address to be of type String"));
467469
}
468470
Dart_Handle result = SocketAddress::GetUnixDomainSockAddr(

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

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -580,21 +580,22 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
580580
// attempt isn't made until either a previous attempt has *failed*,
581581
// or the delay has passed.
582582
// This ensures that at most *n* uncompleted connections can be
583-
// active after *n* &times; *delay* time has passed.
583+
// active after *n* × *delay* time has passed.
584584
if (host is String) {
585585
host = escapeLinkLocalAddress(host);
586586
}
587587
_throwOnBadPort(port);
588588
_InternetAddress? source;
589-
if (sourceAddress is _InternetAddress) {
590-
source = sourceAddress;
591-
} else if (sourceAddress is String) {
592-
source = new _InternetAddress.fromString(sourceAddress);
593-
}
594-
// Should we throw if sourceAddress is not one of:
595-
// null, _InternetAddress or String?
596-
// Is it somehow ensured upstream
597-
// that only those three types will reach here?
589+
if (sourceAddress != null) {
590+
if (sourceAddress is _InternetAddress) {
591+
source = sourceAddress;
592+
} else if (sourceAddress is String) {
593+
source = new _InternetAddress.fromString(sourceAddress);
594+
} else {
595+
throw ArgumentError.value(sourceAddress, "sourceAddress",
596+
"Must be a string or native InternetAddress");
597+
}
598+
}
598599
return new Future.value(host).then<List<InternetAddress>>((host) {
599600
if (host is _InternetAddress) return [host];
600601
return lookup(host).then((addresses) {
@@ -606,7 +607,7 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
606607
}).then((addresses) {
607608
assert(addresses.isNotEmpty);
608609
// Completer for result.
609-
var completer = new Completer<_NativeSocket>();
610+
var result = new Completer<_NativeSocket>();
610611
// Index of next address in [addresses] to try.
611612
var index = 0;
612613
// Error, set if an error occurs.
@@ -632,43 +633,55 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
632633
if (index >= addresses.length) {
633634
if (connecting.isEmpty) {
634635
assert(error != null);
635-
assert(!completer.isCompleted);
636-
completer.completeError(error);
636+
assert(!result.isCompleted);
637+
result.completeError(error);
637638
}
638639
return;
639640
}
640641
final address = addresses[index++] as _InternetAddress;
641642
var socket = new _NativeSocket.normal(address);
642-
var result;
643-
if (source == null) {
644-
if (address.type == InternetAddressType.unix) {
645-
result = socket.nativeCreateUnixDomainConnect(
643+
// Will contain values of various types representing the result
644+
// of trying to create a connection.
645+
// A value of `true` means success, everything else means failure.
646+
Object? connectionResult;
647+
if (address.type == InternetAddressType.unix) {
648+
if (source == null) {
649+
connectionResult = socket.nativeCreateUnixDomainConnect(
646650
address.address, _Namespace._namespace);
647651
} else {
648-
result = socket.nativeCreateConnect(
649-
address._in_addr, port, address._scope_id);
650-
}
651-
} else {
652-
if (address.type == InternetAddressType.unix) {
653652
assert(source.type == InternetAddressType.unix);
654-
result = socket.nativeCreateUnixDomainBindConnect(
653+
connectionResult = socket.nativeCreateUnixDomainBindConnect(
655654
address.address, source.address, _Namespace._namespace);
655+
}
656+
assert(connectionResult == true ||
657+
connectionResult is Error ||
658+
connectionResult is OSError);
659+
} else {
660+
if (source == null) {
661+
connectionResult = socket.nativeCreateConnect(
662+
address._in_addr, port, address._scope_id);
656663
} else {
657-
result = socket.nativeCreateBindConnect(
664+
connectionResult = socket.nativeCreateBindConnect(
658665
address._in_addr, port, source._in_addr, address._scope_id);
659666
}
667+
assert(connectionResult == true || connectionResult is OSError);
660668
}
661-
if (result is OSError) {
662-
// Keep first error, if present.
663-
if (error == null) {
664-
int errorCode = result.errorCode;
669+
if (connectionResult != true) {
670+
// connectionResult was not a success.
671+
if (connectionResult is OSError) {
672+
int errorCode = connectionResult.errorCode;
665673
if (source != null &&
666674
errorCode != null &&
667675
socket.isBindError(errorCode)) {
668-
error = createError(result, "Bind failed", source);
676+
error = createError(connectionResult, "Bind failed", source);
669677
} else {
670-
error = createError(result, "Connection failed", address, port);
678+
error = createError(
679+
connectionResult, "Connection failed", address, port);
671680
}
681+
} else if (connectionResult is Error) {
682+
error = connectionResult;
683+
} else {
684+
error = createError(null, "Connection failed", address);
672685
}
673686
connectNext(); // Try again after failure to connect.
674687
return;
@@ -722,7 +735,7 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
722735
s.setListening(read: false, write: false);
723736
}
724737
connecting.clear();
725-
completer.complete(socket);
738+
result.complete(socket);
726739
}, error: (e, st) {
727740
connecting.remove(socket);
728741
socket.close();
@@ -743,15 +756,15 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
743756
s.setListening(read: false, write: false);
744757
}
745758
connecting.clear();
746-
if (!completer.isCompleted) {
759+
if (!result.isCompleted) {
747760
error ??= createError(null,
748761
"Connection attempt cancelled, host: ${host}, port: ${port}");
749-
completer.completeError(error);
762+
result.completeError(error);
750763
}
751764
}
752765

753766
connectNext();
754-
return new ConnectionTask<_NativeSocket>._(completer.future, onCancel);
767+
return new ConnectionTask<_NativeSocket>._(result.future, onCancel);
755768
});
756769
}
757770

0 commit comments

Comments
 (0)