From de0805283eddcd3339963e2a839bf4afceba59fa Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Mon, 24 Feb 2020 23:37:43 +0000 Subject: [PATCH] [nnbd] Re-add null checks in NNBD SDK file_impl.dart and directory_impl.dart They were removed here: https://dart-review.googlesource.com/c/sdk/+/133060 Until everyone is migrated, we still need these null checks This fixes standalone_2/io/file_constructor_test and standalone_2/io/directory_fuzz_test Change-Id: I5882ae924d1de0b88f4f0d2012e571fcef697868 Bug: https://github.com/dart-lang/sdk/issues/40614 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135540 Commit-Queue: Liam Appelbe Reviewed-by: Jonas Termansen Reviewed-by: Alexander Markov --- .../tool/dart2js_nnbd_sdk_error_golden.txt | 2 +- .../tool/dartdevc_nnbd_sdk_error_golden.txt | 2 +- sdk_nnbd/lib/io/directory_impl.dart | 11 ++++- sdk_nnbd/lib/io/file_impl.dart | 40 +++++++++++++------ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/pkg/dev_compiler/tool/dart2js_nnbd_sdk_error_golden.txt b/pkg/dev_compiler/tool/dart2js_nnbd_sdk_error_golden.txt index 6afb35ee369..10ea63c7d25 100644 --- a/pkg/dev_compiler/tool/dart2js_nnbd_sdk_error_golden.txt +++ b/pkg/dev_compiler/tool/dart2js_nnbd_sdk_error_golden.txt @@ -15,4 +15,4 @@ WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/_http/http.dart|1476|39|5| WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/_http/http.dart|8384|60|5|The left operand can't be null, so the right operand is never executed. WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/_http/http.dart|9311|54|5|The left operand can't be null, so the right operand is never executed. WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/developer/developer.dart|315|25|23|The left operand can't be null, so the right operand is never executed. -WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/io/io.dart|9167|16|1|The left operand can't be null, so the right operand is never executed. +WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/io/io.dart|9188|16|1|The left operand can't be null, so the right operand is never executed. diff --git a/pkg/dev_compiler/tool/dartdevc_nnbd_sdk_error_golden.txt b/pkg/dev_compiler/tool/dartdevc_nnbd_sdk_error_golden.txt index 0dee66c7075..a0dda804b25 100644 --- a/pkg/dev_compiler/tool/dartdevc_nnbd_sdk_error_golden.txt +++ b/pkg/dev_compiler/tool/dartdevc_nnbd_sdk_error_golden.txt @@ -20,4 +20,4 @@ WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/_http/http.dart|8384|60|5| WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/_http/http.dart|9311|54|5|The left operand can't be null, so the right operand is never executed. WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/collection/collection.dart|1076|46|13|The left operand can't be null, so the right operand is never executed. WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/developer/developer.dart|332|25|23|The left operand can't be null, so the right operand is never executed. -WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/io/io.dart|9167|16|1|The left operand can't be null, so the right operand is never executed. +WARNING|STATIC_WARNING|DEAD_NULL_AWARE_EXPRESSION|lib/io/io.dart|9188|16|1|The left operand can't be null, so the right operand is never executed. diff --git a/sdk_nnbd/lib/io/directory_impl.dart b/sdk_nnbd/lib/io/directory_impl.dart index 29342c50417..06777e7e55c 100644 --- a/sdk_nnbd/lib/io/directory_impl.dart +++ b/sdk_nnbd/lib/io/directory_impl.dart @@ -9,11 +9,12 @@ class _Directory extends FileSystemEntity implements Directory { final Uint8List _rawPath; _Directory(String path) - : _path = path, + : _path = _checkNotNull(path, "path"), _rawPath = FileSystemEntity._toUtf8Array(path); _Directory.fromRawPath(Uint8List rawPath) - : _rawPath = FileSystemEntity._toNullTerminatedUtf8Array(rawPath), + : _rawPath = FileSystemEntity._toNullTerminatedUtf8Array( + _checkNotNull(rawPath, "rawPath")), _path = FileSystemEntity._toStringFromUtf8Array(rawPath); String get path => _path; @@ -269,6 +270,12 @@ class _Directory extends FileSystemEntity implements Directory { return new Exception("Unknown error"); } } + + // TODO(40614): Remove once non-nullability is sound. + static T _checkNotNull(T t, String name) { + ArgumentError.checkNotNull(t, name); + return t; + } } abstract class _AsyncDirectoryListerOps { diff --git a/sdk_nnbd/lib/io/file_impl.dart b/sdk_nnbd/lib/io/file_impl.dart index e0b2d305c11..7be1bf37c0f 100644 --- a/sdk_nnbd/lib/io/file_impl.dart +++ b/sdk_nnbd/lib/io/file_impl.dart @@ -202,11 +202,12 @@ class _File extends FileSystemEntity implements File { final Uint8List _rawPath; _File(String path) - : _path = path, + : _path = _checkNotNull(path, "path"), _rawPath = FileSystemEntity._toUtf8Array(path); _File.fromRawPath(Uint8List rawPath) - : _rawPath = FileSystemEntity._toNullTerminatedUtf8Array(rawPath), + : _rawPath = FileSystemEntity._toNullTerminatedUtf8Array( + _checkNotNull(rawPath, "rawPath")), _path = FileSystemEntity._toStringFromUtf8Array(rawPath); String get path => _path; @@ -634,6 +635,12 @@ class _File extends FileSystemEntity implements File { throw new FileSystemException(msg, path, result); } } + + // TODO(40614): Remove once non-nullability is sound. + static T _checkNotNull(T t, String name) { + ArgumentError.checkNotNull(t, name); + return t; + } } abstract class _RandomAccessFileOps { @@ -732,7 +739,7 @@ class _RandomAccessFile implements RandomAccessFile { } Future read(int bytes) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(bytes, "bytes"); return _dispatch(_IOService.fileRead, [null, bytes]).then((response) { if (_isErrorResponse(response)) { @@ -745,7 +752,7 @@ class _RandomAccessFile implements RandomAccessFile { } Uint8List readSync(int bytes) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(bytes, "bytes"); _checkAvailable(); var result = _ops.read(bytes); @@ -757,7 +764,7 @@ class _RandomAccessFile implements RandomAccessFile { } Future readInto(List buffer, [int start = 0, int? end]) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(buffer, "buffer"); end = RangeError.checkValidRange(start, end, buffer.length); if (end == start) { @@ -777,7 +784,7 @@ class _RandomAccessFile implements RandomAccessFile { } int readIntoSync(List buffer, [int start = 0, int? end]) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(buffer, "buffer"); _checkAvailable(); end = RangeError.checkValidRange(start, end, buffer.length); @@ -793,7 +800,7 @@ class _RandomAccessFile implements RandomAccessFile { } Future writeByte(int value) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(value, "value"); return _dispatch(_IOService.fileWriteByte, [null, value]).then((response) { if (_isErrorResponse(response)) { @@ -806,7 +813,7 @@ class _RandomAccessFile implements RandomAccessFile { int writeByteSync(int value) { _checkAvailable(); - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(value, "value"); var result = _ops.writeByte(value); if (result is OSError) { @@ -818,7 +825,7 @@ class _RandomAccessFile implements RandomAccessFile { Future writeFrom(List buffer, [int start = 0, int? end]) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(buffer, "buffer"); ArgumentError.checkNotNull(start, "start"); end = RangeError.checkValidRange(start, end, buffer.length); @@ -848,6 +855,9 @@ class _RandomAccessFile implements RandomAccessFile { void writeFromSync(List buffer, [int start = 0, int? end]) { _checkAvailable(); + // TODO(40614): Remove once non-nullability is sound. + ArgumentError.checkNotNull(buffer, "buffer"); + ArgumentError.checkNotNull(start, "start"); end = RangeError.checkValidRange(start, end, buffer.length); if (end == start) { return; @@ -864,11 +874,15 @@ class _RandomAccessFile implements RandomAccessFile { Future writeString(String string, {Encoding encoding: utf8}) { + // TODO(40614): Remove once non-nullability is sound. + ArgumentError.checkNotNull(encoding, "encoding"); var data = encoding.encode(string); return writeFrom(data, 0, data.length); } void writeStringSync(String string, {Encoding encoding: utf8}) { + // TODO(40614): Remove once non-nullability is sound. + ArgumentError.checkNotNull(encoding, "encoding"); var data = encoding.encode(string); writeFromSync(data, 0, data.length); } @@ -971,7 +985,7 @@ class _RandomAccessFile implements RandomAccessFile { Future lock( [FileLock mode = FileLock.exclusive, int start = 0, int end = -1]) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(mode, "mode"); ArgumentError.checkNotNull(start, "start"); ArgumentError.checkNotNull(end, "end"); @@ -989,7 +1003,7 @@ class _RandomAccessFile implements RandomAccessFile { } Future unlock([int start = 0, int end = -1]) { - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(start, "start"); ArgumentError.checkNotNull(end, "end"); if (start == end) { @@ -1007,7 +1021,7 @@ class _RandomAccessFile implements RandomAccessFile { void lockSync( [FileLock mode = FileLock.exclusive, int start = 0, int end = -1]) { _checkAvailable(); - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(mode, "mode"); ArgumentError.checkNotNull(start, "start"); ArgumentError.checkNotNull(end, "end"); @@ -1023,7 +1037,7 @@ class _RandomAccessFile implements RandomAccessFile { void unlockSync([int start = 0, int end = -1]) { _checkAvailable(); - // TODO: Remove once non-nullability is sound. + // TODO(40614): Remove once non-nullability is sound. ArgumentError.checkNotNull(start, "start"); ArgumentError.checkNotNull(end, "end"); if (start == end) {