Skip to content

Commit e33ce98

Browse files
committed
fix: enforce mostly-safe casting
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 73b90f9 commit e33ce98

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/node_modules/@stdlib/ndarray/flatten/lib/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var isPlainObject = require( '@stdlib/assert/is-plain-object' );
2424
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2525
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2626
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' );
27+
var isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' );
2728
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
2829
var getShape = require( '@stdlib/ndarray/shape' );
2930
var getOrder = require( '@stdlib/ndarray/order' );
@@ -336,7 +337,9 @@ function flatten( x, options ) {
336337
}
337338
}
338339
if ( hasOwnProp( options, 'dtype' ) ) {
339-
// Delegate `dtype` validation to `emptyLike` during output array creation:
340+
if ( !isMostlySafeCast( opts.dtype, options.dtype ) ) {
341+
throw new TypeError( format( 'invalid option. First argument cannot be safely cast to the specified data type. Input data type: %s. Option: `%s`.', String( opts.dtype ), String( options.dtype ) ) );
342+
}
340343
opts.dtype = options.dtype;
341344
}
342345
}

lib/node_modules/@stdlib/ndarray/flatten/test/test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var tape = require( 'tape' );
2626
var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
2727
var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' );
2828
var zeros = require( '@stdlib/ndarray/zeros' );
29+
var empty = require( '@stdlib/ndarray/empty' );
2930
var ndarray = require( '@stdlib/ndarray/ctor' );
3031
var Float64Array = require( '@stdlib/array/float64' );
3132
var Float32Array = require( '@stdlib/array/float32' );
@@ -226,6 +227,38 @@ tape( 'the function throws an error if provided an invalid `dtype` option', func
226227
}
227228
});
228229

230+
tape( 'the function throws an error if provided a first argument which cannot be safely cast to a specified `dtype` option', function test( t ) {
231+
var values;
232+
var x;
233+
var i;
234+
235+
x = empty( [ 2, 2 ], {
236+
'dtype': 'bool'
237+
});
238+
239+
values = [
240+
'float64',
241+
'complex128',
242+
'float32',
243+
'complex64',
244+
'int32'
245+
];
246+
247+
for ( i = 0; i < values.length; i++ ) {
248+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] );
249+
}
250+
t.end();
251+
252+
function badValue( value ) {
253+
return function badValue() {
254+
var opts = {
255+
'dtype': value
256+
};
257+
flatten( x, opts );
258+
};
259+
}
260+
});
261+
229262
tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) {
230263
var expected;
231264
var xbuf;

0 commit comments

Comments
 (0)