Skip to content

Commit de45656

Browse files
brianquinlancommit-bot@chromium.org
authored andcommitted
Fix a bug where calling _WindowsCodePageEncoder.convert would call an abort on non-Windows platforms.
TEST=issue_4636_test.dart Bug: 46436,47402 Change-Id: I11579428f1d3c791ed64b7d5e6f75f41aef8c6c8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215981 Commit-Queue: Brian Quinlan <[email protected]> Reviewed-by: Alexander Aprelev <[email protected]>
1 parent c7e1276 commit de45656

10 files changed

+104
-22
lines changed

runtime/bin/process.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ void FUNCTION_NAME(SystemEncodingToString)(Dart_NativeArguments args) {
319319
char* str = StringUtils::ConsoleStringToUtf8(reinterpret_cast<char*>(buffer),
320320
bytes_length, &len);
321321
if (str == NULL) {
322-
Dart_ThrowException(
323-
DartUtils::NewInternalError("SystemEncodingToString failed"));
322+
Dart_ThrowException(DartUtils::NewDartUnsupportedError(
323+
"SystemEncodingToString not supported on this operating system"));
324324
}
325325
result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len);
326326
ThrowIfError(result);
@@ -338,8 +338,8 @@ void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) {
338338
const char* system_string =
339339
StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len);
340340
if (system_string == NULL) {
341-
Dart_ThrowException(
342-
DartUtils::NewInternalError("StringToSystemEncoding failed"));
341+
Dart_ThrowException(DartUtils::NewDartUnsupportedError(
342+
"StringToSystemEncoding not supported on this operating system"));
343343
}
344344
uint8_t* buffer = NULL;
345345
Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer);

runtime/bin/utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class StringUtils {
6767
// character. If result_len is not NUL, it is used to set the number
6868
// of characters in the result.
6969
//
70-
// These conversion functions are only implemented on Windows as the
71-
// Dart code only hit this path on Windows.
70+
// A return value of `nullptr` indicates that the conversion is not supported,
71+
// which is true on all platforms other than Windows.
7272
static const char* ConsoleStringToUtf8(const char* str,
7373
intptr_t len = -1,
7474
intptr_t* result_len = NULL);

runtime/bin/utils_android.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,24 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
4343
const char* StringUtils::ConsoleStringToUtf8(const char* str,
4444
intptr_t len,
4545
intptr_t* result_len) {
46-
UNIMPLEMENTED();
4746
return NULL;
4847
}
4948

5049
const char* StringUtils::Utf8ToConsoleString(const char* utf8,
5150
intptr_t len,
5251
intptr_t* result_len) {
53-
UNIMPLEMENTED();
5452
return NULL;
5553
}
5654

5755
char* StringUtils::ConsoleStringToUtf8(char* str,
5856
intptr_t len,
5957
intptr_t* result_len) {
60-
UNIMPLEMENTED();
6158
return NULL;
6259
}
6360

6461
char* StringUtils::Utf8ToConsoleString(char* utf8,
6562
intptr_t len,
6663
intptr_t* result_len) {
67-
UNIMPLEMENTED();
6864
return NULL;
6965
}
7066

runtime/bin/utils_fuchsia.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,24 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
4141
const char* StringUtils::ConsoleStringToUtf8(const char* str,
4242
intptr_t len,
4343
intptr_t* result_len) {
44-
UNIMPLEMENTED();
4544
return NULL;
4645
}
4746

4847
const char* StringUtils::Utf8ToConsoleString(const char* utf8,
4948
intptr_t len,
5049
intptr_t* result_len) {
51-
UNIMPLEMENTED();
5250
return NULL;
5351
}
5452

5553
char* StringUtils::ConsoleStringToUtf8(char* str,
5654
intptr_t len,
5755
intptr_t* result_len) {
58-
UNIMPLEMENTED();
5956
return NULL;
6057
}
6158

6259
char* StringUtils::Utf8ToConsoleString(char* utf8,
6360
intptr_t len,
6461
intptr_t* result_len) {
65-
UNIMPLEMENTED();
6662
return NULL;
6763
}
6864

runtime/bin/utils_linux.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,24 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
4242
const char* StringUtils::ConsoleStringToUtf8(const char* str,
4343
intptr_t len,
4444
intptr_t* result_len) {
45-
UNIMPLEMENTED();
4645
return NULL;
4746
}
4847

4948
const char* StringUtils::Utf8ToConsoleString(const char* utf8,
5049
intptr_t len,
5150
intptr_t* result_len) {
52-
UNIMPLEMENTED();
5351
return NULL;
5452
}
5553

5654
char* StringUtils::ConsoleStringToUtf8(char* str,
5755
intptr_t len,
5856
intptr_t* result_len) {
59-
UNIMPLEMENTED();
6057
return NULL;
6158
}
6259

6360
char* StringUtils::Utf8ToConsoleString(char* utf8,
6461
intptr_t len,
6562
intptr_t* result_len) {
66-
UNIMPLEMENTED();
6763
return NULL;
6864
}
6965

runtime/bin/utils_macos.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,24 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
4646
const char* StringUtils::ConsoleStringToUtf8(const char* str,
4747
intptr_t len,
4848
intptr_t* result_len) {
49-
UNIMPLEMENTED();
5049
return NULL;
5150
}
5251

5352
const char* StringUtils::Utf8ToConsoleString(const char* utf8,
5453
intptr_t len,
5554
intptr_t* result_len) {
56-
UNIMPLEMENTED();
5755
return NULL;
5856
}
5957

6058
char* StringUtils::ConsoleStringToUtf8(char* str,
6159
intptr_t len,
6260
intptr_t* result_len) {
63-
UNIMPLEMENTED();
6461
return NULL;
6562
}
6663

6764
char* StringUtils::Utf8ToConsoleString(char* utf8,
6865
intptr_t len,
6966
intptr_t* result_len) {
70-
UNIMPLEMENTED();
7167
return NULL;
7268
}
7369

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
//
5+
// Checks that _WindowsCodePageEncoder.convert() throws an exception on
6+
// platforms other than Windows.
7+
8+
import "dart:io";
9+
import 'dart:mirrors';
10+
11+
import "package:expect/expect.dart";
12+
13+
ClassMirror findWindowsCodePageEncoder() {
14+
final dartIo =
15+
currentMirrorSystem().libraries[Uri(scheme: "dart", path: "io")];
16+
if (dartIo == null) {
17+
throw StateError("dart:io not present");
18+
}
19+
20+
final classes = dartIo.declarations.values
21+
.where((d) =>
22+
d is ClassMirror &&
23+
d.simpleName.toString().contains('"_WindowsCodePageEncoder"'))
24+
.map((d) => d as ClassMirror)
25+
.toList();
26+
27+
Expect.equals(
28+
1, classes.length, "Expected exactly one _WindowsCodePageEncoder");
29+
return classes[0];
30+
}
31+
32+
test() {
33+
final winCodePageEncoder = findWindowsCodePageEncoder();
34+
final encoder = winCodePageEncoder.newInstance(Symbol(""), List.empty());
35+
try {
36+
encoder.invoke(Symbol("convert"), List.of(["test"]));
37+
Expect.isTrue(Platform.isWindows,
38+
"expected UnsupportedError on ${Platform.operatingSystem}");
39+
} on UnsupportedError catch (e) {
40+
Expect.isFalse(
41+
Platform.isWindows, "unexpected UnsupportedError on Windows: $e");
42+
}
43+
}
44+
45+
void main() {
46+
test();
47+
}

tests/standalone/standalone.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ verbose_gc_to_bmu_test: SkipByDesign # No verbose_gc in product mode
5656
[ $runtime == dart_precompiled ]
5757
http_launch_test: Skip
5858
io/addlatexhash_test: Skip
59+
io/issue_46436_test: SkipByDesign # Uses mirrors.
5960
io/socket_sigpipe_test: SkipByDesign # Spawns server process using Platform.executable
6061
io/wait_for_event_isolate_test: SkipByDesign # Uses mirrors.
6162
io/wait_for_event_microtask_test: SkipByDesign # Uses mirrors.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
//
5+
// Checks that _WindowsCodePageEncoder.convert() throws an exception on
6+
// platforms other than Windows.
7+
8+
// @dart = 2.9
9+
10+
import "dart:io";
11+
import 'dart:mirrors';
12+
13+
import "package:expect/expect.dart";
14+
15+
ClassMirror findWindowsCodePageEncoder() {
16+
final dartIo =
17+
currentMirrorSystem().libraries[Uri(scheme: "dart", path: "io")];
18+
if (dartIo == null) {
19+
throw StateError("dart:io not present");
20+
}
21+
22+
final classes = dartIo.declarations.values
23+
.where((d) =>
24+
d is ClassMirror &&
25+
d.simpleName.toString().contains('"_WindowsCodePageEncoder"'))
26+
.map((d) => d as ClassMirror)
27+
.toList();
28+
29+
Expect.equals(
30+
1, classes.length, "Expected exactly one _WindowsCodePageEncoder");
31+
return classes[0];
32+
}
33+
34+
test() {
35+
final winCodePageEncoder = findWindowsCodePageEncoder();
36+
final encoder = winCodePageEncoder.newInstance(Symbol(""), List.empty());
37+
try {
38+
encoder.invoke(Symbol("convert"), List.of(["test"]));
39+
Expect.isTrue(Platform.isWindows,
40+
"expected UnsupportedError on ${Platform.operatingSystem}");
41+
} on UnsupportedError catch (e) {
42+
Expect.isFalse(
43+
Platform.isWindows, "unexpected UnsupportedError on Windows: $e");
44+
}
45+
}
46+
47+
void main() {
48+
test();
49+
}

tests/standalone_2/standalone_2.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ verbose_gc_to_bmu_test: SkipByDesign # No verbose_gc in product mode
5656
[ $runtime == dart_precompiled ]
5757
http_launch_test: Skip
5858
io/addlatexhash_test: Skip
59+
io/issue_46436_test: SkipByDesign # Uses mirrors.
5960
io/socket_sigpipe_test: SkipByDesign # Spawns server process using Platform.executable
6061
io/wait_for_event_isolate_test: SkipByDesign # Uses mirrors.
6162
io/wait_for_event_microtask_test: SkipByDesign # Uses mirrors.

0 commit comments

Comments
 (0)