Skip to content

Commit 83a01e7

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 e33ce98 commit 83a01e7

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/node_modules/@stdlib/ndarray/flatten-from/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 isInteger = require( '@stdlib/assert/is-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' );
@@ -334,7 +335,9 @@ function flattenFrom( x, dim, options ) {
334335
}
335336
}
336337
if ( hasOwnProp( options, 'dtype' ) ) {
337-
// Delegate `dtype` validation to `emptyLike` during output array creation:
338+
if ( !isMostlySafeCast( opts.dtype, options.dtype ) ) {
339+
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 ) ) );
340+
}
338341
opts.dtype = options.dtype;
339342
}
340343
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
var tape = require( 'tape' );
2626
var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
2727
var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' );
28+
var empty = require( '@stdlib/ndarray/empty' );
2829
var zeros = require( '@stdlib/ndarray/zeros' );
2930
var ndarray = require( '@stdlib/ndarray/ctor' );
3031
var Float64Array = require( '@stdlib/array/float64' );
@@ -269,6 +270,38 @@ tape( 'the function throws an error if provided an invalid `dtype` option', func
269270
}
270271
});
271272

273+
tape( 'the function throws an error if provided a first argument which cannot be safely cast to a specified `dtype` option', function test( t ) {
274+
var values;
275+
var x;
276+
var i;
277+
278+
x = empty( [ 2, 2 ], {
279+
'dtype': 'bool'
280+
});
281+
282+
values = [
283+
'float64',
284+
'complex128',
285+
'float32',
286+
'complex64',
287+
'int32'
288+
];
289+
290+
for ( i = 0; i < values.length; i++ ) {
291+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] );
292+
}
293+
t.end();
294+
295+
function badValue( value ) {
296+
return function badValue() {
297+
var opts = {
298+
'dtype': value
299+
};
300+
flattenFrom( x, 0, opts );
301+
};
302+
}
303+
});
304+
272305
tape( 'by default, the function flattens all dimensions of a provided input ndarray starting from a specified dimension in lexicographic order (row-major, contiguous)', function test( t ) {
273306
var expected;
274307
var xbuf;

0 commit comments

Comments
 (0)