Skip to content

Commit 9b916b2

Browse files
mkustermannCommit Queue
authored andcommitted
[io] Avoid uncatchable error in RawDatagramSocket.bind()
Currently the error happens very late in C code which throws a `Dart_NewApiError()` which isn't catchable via try/catch in Dart. Our `dart:io` APIs should never result in uncatchable errors being thrown. This CL adds the verification that the address is a IPv4/IPv6 address (and not a unix domain socket address) before actually binding the code. Issue #60421 TEST=standalone/io/datagram_error_test Change-Id: I0ead36b8c90dd8b04523f9e64d3be63a8e66a56c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/438140 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Slava Egorov <[email protected]>
1 parent 9918fa0 commit 9b916b2

File tree

3 files changed

+20
-50
lines changed

3 files changed

+20
-50
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,13 @@ base class _NativeSocket extends _NativeSocketNativeWrapper
12411241

12421242
final address = await _resolveHost(host);
12431243

1244+
if (address.type != InternetAddressType.IPv6 &&
1245+
address.type != InternetAddressType.IPv4) {
1246+
throw SocketException(
1247+
'Cannot bind datagram socket on non-IPv4/IPv6 address (was: $address)',
1248+
);
1249+
}
1250+
12441251
var socket = _NativeSocket.datagram(address);
12451252
var result = socket._nativeCreateBindDatagram(
12461253
address._in_addr,

tests/standalone/io/datagram_error.dart

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
//
5-
// Dart test program for testing error path in datagram bind call.
6-
//
7-
// OtherResources=datagram_error.dart
84

95
import 'dart:io';
106

7+
import "package:async_helper/async_helper.dart";
118
import "package:expect/expect.dart";
12-
import 'package:path/path.dart' as path;
139

1410
void main() async {
15-
var sdkPath = path.absolute(path.dirname(Platform.executable));
16-
var dartPath = path.absolute(
17-
sdkPath,
18-
Platform.isWindows ? 'dart.exe' : 'dart',
19-
);
20-
// Get the Dart script file that generates output.
21-
var scriptFile = new File(
22-
Platform.script.resolve("datagram_error.dart").toFilePath(),
23-
);
24-
var args = <String>[scriptFile.path];
25-
ProcessResult syncResult = Process.runSync(dartPath, args);
26-
Expect.notEquals(0, syncResult.exitCode);
27-
Expect.stringEquals(
28-
syncResult.stderr,
29-
"Unexpected type for socket address" + Platform.lineTerminator,
11+
SocketException? error;
12+
try {
13+
await RawDatagramSocket.bind(
14+
InternetAddress('/tmp/test_socket', type: InternetAddressType.unix),
15+
0,
16+
);
17+
} on SocketException catch (e) {
18+
error = e;
19+
}
20+
Expect.contains(
21+
'Cannot bind datagram socket on non-IPv4/IPv6 address',
22+
'$error',
3023
);
3124
}

0 commit comments

Comments
 (0)