Skip to content

Commit 6749a0a

Browse files
committed
fix out of bounds bug
1 parent c4d52b5 commit 6749a0a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

diff.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,11 @@ function fix_cursor (diffs, cursor_pos) {
641641
var d = ndiffs[cursor_pointer];
642642
var d_next = ndiffs[cursor_pointer + 1];
643643

644-
if (d[0] !== DIFF_EQUAL) {
644+
if (d == null) {
645+
// Text was deleted from end of original string,
646+
// cursor is now out of bounds in new string
647+
return diffs;
648+
} else if (d[0] !== DIFF_EQUAL) {
645649
// A modification happened at the cursor location.
646650
// This is the expected outcome, so we can return the original diff.
647651
return diffs;

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ for(var i = 0; i < ITERATIONS; ++i) {
3838

3939
console.log('Running tests *with* cursor information');
4040
for(var i = 0; i < ITERATIONS; ++i) {
41-
var cursor_pos = Math.floor(random() * strings[i].length);
41+
var cursor_pos = Math.floor(random() * strings[i].length + 1);
4242
var diffs = diff(strings[i], strings[i+1], cursor_pos);
4343
var patch = googlediff.patch_make(strings[i], strings[i+1], diffs);
4444
var expected = googlediff.patch_apply(patch, strings[i])[0];

0 commit comments

Comments
 (0)