Skip to content

Commit 16c9908

Browse files
committed
fix: perform explicit copy to avoid unintended argument mutation
--- 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 028683a commit 16c9908

File tree

1 file changed

+14
-9
lines changed
  • lib/node_modules/@stdlib/ndarray/base/broadcast-array-except-dimensions/lib

1 file changed

+14
-9
lines changed

lib/node_modules/@stdlib/ndarray/base/broadcast-array-except-dimensions/lib/main.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var format = require( '@stdlib/string/format' );
8383
*/
8484
function broadcastArrayExceptDimensions( arr, shape, dims ) { // eslint-disable-line id-length
8585
var strides;
86+
var idx;
8687
var dim;
8788
var sh;
8889
var st;
@@ -94,10 +95,14 @@ function broadcastArrayExceptDimensions( arr, shape, dims ) { // eslint-disable-
9495
var i;
9596
var j;
9697

97-
shape = copy( shape ); // perform a copy to avoid unintended mutation
98-
N = shape.length;
98+
// Copy input arguments to avoid unintended mutation:
99+
shape = copy( shape );
100+
idx = copy( dims );
99101

102+
// Resolve the shape of the input array:
100103
sh = getShape( arr, false );
104+
105+
N = shape.length;
101106
M = sh.length;
102107
if ( N < M ) {
103108
throw new Error( 'invalid argument. Cannot broadcast an array to a shape having fewer dimensions. Arrays can only be broadcasted to shapes having the same or more dimensions.' );
@@ -106,22 +111,22 @@ function broadcastArrayExceptDimensions( arr, shape, dims ) { // eslint-disable-
106111
strides = zeros( N );
107112

108113
// Verify that we've been provided a list of unique dimension indices...
109-
dl = dims.length;
110-
dims = normalizeIndices( dims, N-1 );
111-
if ( dims === null ) {
114+
dl = idx.length;
115+
idx = normalizeIndices( idx, N-1 );
116+
if ( idx === null ) {
112117
throw new RangeError( format( 'invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].', join( dims, ',' ) ) );
113118
}
114-
dims.sort();
115-
if ( dims.length !== dl ) {
119+
idx.sort();
120+
if ( idx.length !== dl ) {
116121
throw new Error( format( 'invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].', join( dims, ',' ) ) );
117122
}
118-
di = dims.length - 1;
123+
di = idx.length - 1;
119124

120125
// Determine the output array strides...
121126
st = getStrides( arr, false );
122127
for ( i = N-1; i >= 0; i-- ) {
123128
j = M - N + i;
124-
if ( di >= 0 && dims[ di ] === i ) {
129+
if ( di >= 0 && idx[ di ] === i ) {
125130
if ( j >= 0 ) {
126131
shape[ i ] = sh[ j ];
127132
strides[ i ] = st[ j ];

0 commit comments

Comments
 (0)