Skip to content

Commit 8b1f86b

Browse files
committed
fix: prevent writing to read-only ndarrays
1 parent 73a198b commit 8b1f86b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

lib/node_modules/@stdlib/blas/dswap/lib/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' );
2424
var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive;
25+
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
2526
var hasEqualValues = require( '@stdlib/array/base/assert/has-equal-values-indexed' );
2627
var min = require( '@stdlib/math/base/special/fast/min' );
2728
var without = require( '@stdlib/array/base/without' );
@@ -47,6 +48,7 @@ var format = require( '@stdlib/string/format' );
4748
* @throws {TypeError} second argument must have at least one dimension
4849
* @throws {Error} both input arrays must have the same shape
4950
* @throws {RangeError} third argument is out-of-bounds
51+
* @throws {Error} cannot write to read-only array
5052
* @returns {ndarrayLike} `y`
5153
*
5254
* @example
@@ -84,6 +86,9 @@ function dswap( x, y ) {
8486
if ( !isFloat64ndarrayLike( y ) ) {
8587
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray containing double-precision floating-point numbers. Value: `%s`.', y ) );
8688
}
89+
if ( isReadOnly( x ) || isReadOnly( y ) ) {
90+
throw new Error( 'invalid argument. Cannot write to read-only array.' );
91+
}
8792
// Convert the input arrays to "base" ndarrays:
8893
xc = ndarraylike2ndarray( x );
8994
yc = ndarraylike2ndarray( y );

lib/node_modules/@stdlib/blas/dswap/test/test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ tape( 'the function throws an error if provided a first argument which is not a
109109
}
110110
});
111111

112+
tape( 'the function throws an error if provided a first argument which is a read-only ndarray', function test( t ) {
113+
var values;
114+
var opts;
115+
var i;
116+
117+
opts = {
118+
'readonly': true
119+
};
120+
121+
values = [
122+
array( new Float64Array( 10 ), opts ),
123+
array( new Float64Array( 5 ), opts )
124+
];
125+
126+
for ( i = 0; i < values.length; i++ ) {
127+
t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] );
128+
}
129+
t.end();
130+
131+
function badValue( value ) {
132+
var y = array( new Float64Array( value.length ) );
133+
134+
return function badValue() {
135+
dswap( value, y );
136+
};
137+
}
138+
});
139+
112140
tape( 'the function throws an error if provided a second argument which is not a non-zero-dimensional ndarray containing double-precision floating-point numbers', function test( t ) {
113141
var values;
114142
var i;
@@ -175,6 +203,34 @@ tape( 'the function throws an error if provided a second argument which is not a
175203
}
176204
});
177205

206+
tape( 'the function throws an error if provided a second argument which is a read-only ndarray', function test( t ) {
207+
var values;
208+
var opts;
209+
var i;
210+
211+
opts = {
212+
'readonly': true
213+
};
214+
215+
values = [
216+
array( new Float64Array( 10 ), opts ),
217+
array( new Float64Array( 5 ), opts )
218+
];
219+
220+
for ( i = 0; i < values.length; i++ ) {
221+
t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] );
222+
}
223+
t.end();
224+
225+
function badValue( value ) {
226+
var x = array( new Float64Array( value.length ) );
227+
228+
return function badValue() {
229+
dswap( x, value );
230+
};
231+
}
232+
});
233+
178234
tape( 'the function throws an error if provided a third argument which is not a negative integer (vectors)', function test( t ) {
179235
var values;
180236
var i;

0 commit comments

Comments
 (0)