Skip to content

Commit bc1768e

Browse files
committed
fix: prevent creating writable views when an input array is read-only
1 parent 64bd3a8 commit bc1768e

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

lib/node_modules/@stdlib/ndarray/iter/columns/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ arr = ndarray2array( v );
7676

7777
The function accepts the following `options`:
7878

79-
- **readonly**: boolean indicating whether returned [`ndarray`][@stdlib/ndarray/ctor] views should be read-only. Default: `true`.
79+
- **readonly**: boolean indicating whether returned [`ndarray`][@stdlib/ndarray/ctor] views should be read-only. In order to return writable [`ndarray`][@stdlib/ndarray/ctor] views, the input [`ndarray`][@stdlib/ndarray/ctor] must be writable. If the input [`ndarray`][@stdlib/ndarray/ctor] is read-only, setting this option to `false` raises an exception. Default: `true`.
8080

8181
By default, the iterator returns [`ndarray`][@stdlib/ndarray/ctor] views which are **read-only**. To return writable [views][@stdlib/ndarray/slice], set the `readonly` option to `false`.
8282

lib/node_modules/@stdlib/ndarray/iter/columns/docs/repl.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
options.readonly: boolean (optional)
2222
Boolean indicating whether returned ndarray views should be read-only.
23-
Default: true.
23+
If the input ndarray is read-only, setting this option to `false` raises
24+
an exception. Default: true.
2425

2526
Returns
2627
-------

lib/node_modules/@stdlib/ndarray/iter/columns/docs/types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type Iterator<T> = TypedIterator<T> | TypedIterableIterator<T>;
3232
interface Options {
3333
/**
3434
* Boolean indicating whether returned views should be read-only (default: true).
35+
*
36+
* ## Notes
37+
*
38+
* - If the input array is read-only, setting this option to `false` raises an exception.
3539
*/
3640
readonly?: boolean;
3741
}

lib/node_modules/@stdlib/ndarray/iter/columns/lib/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-propert
2424
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
2525
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2626
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
27+
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
2728
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2829
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
2930
var zeros = require( '@stdlib/array/base/zeros' );
@@ -46,6 +47,7 @@ var format = require( '@stdlib/string/format' );
4647
* @throws {TypeError} first argument must have at least two dimensions
4748
* @throws {TypeError} options argument must be an object
4849
* @throws {TypeError} must provide valid options
50+
* @throws {Error} cannot write to a read-only array
4951
* @returns {Iterator} iterator
5052
*
5153
* @example
@@ -106,6 +108,9 @@ function nditerColumns( x ) {
106108
throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'readonly', options.readonly ) );
107109
}
108110
opts.writable = !options.readonly;
111+
if ( opts.writable && isReadOnly( x ) ) {
112+
throw new Error( format( 'invalid option. Cannot write to read-only array.' ) );
113+
}
109114
}
110115
}
111116
// Retrieve input array meta data:

lib/node_modules/@stdlib/ndarray/iter/columns/test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ tape( 'the function throws an error if provided a `readonly` option which is not
155155
}
156156
});
157157

158+
tape( 'the function throws an error if provided a `readonly` option which equal to `false` when the input array is read-only', function test( t ) {
159+
t.throws( badValue, Error, 'throws an error' );
160+
t.end();
161+
162+
function badValue() {
163+
var x = zeros( [ 2, 2 ], {
164+
'readonly': true
165+
});
166+
nditerColumns( x, {
167+
'readonly': false
168+
});
169+
}
170+
});
171+
158172
tape( 'the function throws an error if provided an ndarray having fewer than two dimensions', function test( t ) {
159173
var values;
160174
var i;

0 commit comments

Comments
 (0)