File tree 3 files changed +17
-0
lines changed
3 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ const { isObject } = require('../src/common')
9
9
class Cursor {
10
10
constructor ( object , index , elemId = undefined ) {
11
11
if ( Array . isArray ( object ) && object [ ELEM_IDS ] && typeof index === 'number' ) {
12
+ if ( index < 0 || index >= object [ ELEM_IDS ] . length ) throw new RangeError ( 'list index out of bounds' )
12
13
this . objectId = object [ OBJECT_ID ]
13
14
this . elemId = object [ ELEM_IDS ] [ index ]
14
15
this . index = index
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ class Text {
33
33
}
34
34
35
35
getElemId ( index ) {
36
+ if ( index < 0 || index >= this . elems . length ) throw new RangeError ( 'text index out of bounds' )
36
37
return this . elems [ index ] . elemId
37
38
}
38
39
Original file line number Diff line number Diff line change @@ -39,4 +39,19 @@ describe('Automerge.Cursor', () => {
39
39
assert . ok ( s3 . cursor instanceof Automerge . Cursor )
40
40
assert . strictEqual ( s3 . cursor . elemId , `4@${ Automerge . getActorId ( s1 ) } ` )
41
41
} )
42
+
43
+ it ( 'should not allow an index beyond the length of the list' , ( ) => {
44
+ assert . throws ( ( ) => {
45
+ Automerge . change ( Automerge . init ( ) , doc => {
46
+ doc . list = [ 1 ]
47
+ doc . cursor = new Automerge . Cursor ( doc . list , 1 )
48
+ } )
49
+ } , / i n d e x o u t o f b o u n d s / )
50
+ assert . throws ( ( ) => {
51
+ Automerge . change ( Automerge . init ( ) , doc => {
52
+ doc . text = new Automerge . Text ( 'a' )
53
+ doc . cursor = doc . text . getCursorAt ( 1 )
54
+ } )
55
+ } , / i n d e x o u t o f b o u n d s / )
56
+ } )
42
57
} )
You can’t perform that action at this time.
0 commit comments