Skip to content

Commit 8a80e77

Browse files
committed
fix(json-ot): 🐛 fix another compose() bug
1 parent b0b6a95 commit 8a80e77

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/json-ot/types/ot-string/StringType.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,32 +197,26 @@ export const compose = (op1: StringTypeOp, op2: StringTypeOp): StringTypeOp => {
197197
const comp1 = i1 >= len1 ? remaining : op1[i1];
198198
const length1 = componentLength(comp1);
199199
const isDelete = idDeleteComponent(comp1);
200-
if (remaining >= (length1 - off1)) {
201-
if (isDelete) {
202-
append(op3, copyComponent(comp1));
203-
i1++;
204-
off1 = 0;
205-
} else {
206-
const end = off2 + (length1 - off1);
207-
switch (typeof comp1) {
208-
case 'number': append(op3, isReversible ? [comp2[0].substring(off2, end)] : -length1); break;
209-
case 'string': {
210-
off2 += (length1 - off1);
211-
break;
212-
}
200+
if (isDelete) {
201+
append(op3, copyComponent(comp1));
202+
i1++;
203+
off1 = 0;
204+
} else if (remaining >= (length1 - off1)) {
205+
const end = off2 + (length1 - off1);
206+
switch (typeof comp1) {
207+
case 'number': append(op3, isReversible ? [comp2[0].substring(off2, end)] : -length1); break;
208+
case 'string': {
209+
off2 += (length1 - off1);
210+
break;
213211
}
214-
i1++;
215-
off1 = 0;
216-
off2 = end;
217212
}
213+
i1++;
214+
off1 = 0;
215+
off2 = end;
218216
} else {
219-
if (isDelete) {
220-
append(op3, copyComponent(comp1));
221-
} else {
222-
switch (typeof comp1) {
223-
case 'number': append(op3, isReversible ? [comp2[0].substring(off2)] : -remaining); break;
224-
// case 'string': break;
225-
}
217+
switch (typeof comp1) {
218+
case 'number': append(op3, isReversible ? [comp2[0].substring(off2)] : -remaining); break;
219+
// case 'string': break;
226220
}
227221
off1 += remaining;
228222
off2 = length2;

src/json-ot/types/ot-string/__tests__/StringType.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ describe('compose()', () => {
105105
['fuzzer bug #1', 'd6', ['}B'], [['}'], ';0q', 2, ['6']], ';0qBd'],
106106
['fuzzer bug #2', 'Ai', [['A'], '#', -1], [-1], ''],
107107
['fuzzer bug #3', 'M}', ['!y1'], ["'/*s", 2, ',/@', -2, ['}']], "'/*s!y,/@"],
108+
['fuzzer bug #4', '8sL', [-2, 'w', ['L']], [['w']], ''],
108109
];
109110

110111
describe('can compose', () => {

src/json-ot/types/ot-string/__tests__/fuzzer.compose.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import {StringOtFuzzer} from './StringOtFuzzer';
44
const fuzzer = new StringOtFuzzer();
55

66
test('works', () => {
7-
for (let i = 0; i < 100; i++) {
7+
for (let i = 0; i < 100000; i++) {
88
const str1 = fuzzer.genString();
9-
let op1 = fuzzer.genOp(str1);
9+
const op1 = fuzzer.genOp(str1);
1010
const str2 = apply(str1, op1);
11-
let op2 = fuzzer.genOp(str2);
11+
const op2 = fuzzer.genOp(str2);
1212
const str3 = apply(str2, op2);
1313
const op3 = compose(op1, op2);
1414
const str4 = apply(str1, op3);

0 commit comments

Comments
 (0)