Skip to content

Commit b4cebee

Browse files
Copilotstreamich
andcommitted
Fix critical bugs: parameter reassignment, unsafe non-null assertions, NaN checks, and Array.isArray usage
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent 3f0ada3 commit b4cebee

File tree

8 files changed

+15
-12
lines changed

8 files changed

+15
-12
lines changed

src/json-crdt-patch/codec/binary/Decoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class Decoder extends CborDecoder<CrdtReader> {
4949
this.patchSid = clock.sid;
5050
const builder = (this.builder = new PatchBuilder(clock));
5151
const map = this.val();
52-
if (map instanceof Array) builder.patch.meta = map[0];
52+
if (Array.isArray(map)) builder.patch.meta = map[0];
5353
this.decodeOperations();
5454
return builder.patch;
5555
}

src/json-crdt-patch/codec/compact/encode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const timestamp = (sid: number, ts: ITimestampStruct): types.CompactCodecTimesta
1212

1313
const timespan = (sid: number, span: ITimespanStruct): types.CompactCodecTimespan => {
1414
const ts = timestamp(sid, span);
15-
if (ts instanceof Array) {
15+
if (Array.isArray(ts)) {
1616
ts.push(span.span);
1717
return ts;
1818
}

src/json-crdt/json-patch/JsonPatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class JsonPatch<N extends JsonNode = JsonNode<any>> {
160160
if (!(node instanceof StrNode)) throw new Error('NOT_FOUND');
161161
const length = node.length();
162162
if (length <= pos) return;
163-
const deletionLength = Math.min(len ?? str!.length, length - pos);
163+
const deletionLength = Math.min(len ?? str.length, length - pos);
164164
const range = node.findInterval(pos, deletionLength);
165165
if (!range) throw new Error('OUT_OF_BOUNDS');
166166
this.builder().del(node.id, range);

src/json-crdt/model/api/nodes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
237237
index = ~~key;
238238
if (index + '' !== key) break ADD;
239239
}
240-
if (index !== index) break ADD;
240+
if (Number.isNaN(index)) break ADD;
241241
if (index < 0) index = 0;
242242
if (index > length) index = length;
243243
if (node instanceof ArrApi) node.ins(index, Array.isArray(value) ? value : [value]);
@@ -270,7 +270,7 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
270270
index = ~~key;
271271
if (index + '' !== key) break REPLACE;
272272
}
273-
if (index !== index || index < 0 || index > length) break REPLACE;
273+
if (Number.isNaN(index) || index < 0 || index > length) break REPLACE;
274274
if (index === length) node.ins(index, [value]);
275275
else node.upd(index, value);
276276
} else if (node instanceof VecApi) node.set([[~~key, value]]);
@@ -297,7 +297,7 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
297297
index = ~~key;
298298
if (index + '' !== key) break REMOVE;
299299
}
300-
if (index !== index || index < 0 || index > len) break REMOVE;
300+
if (Number.isNaN(index) || index < 0 || index > len) break REMOVE;
301301
node.del(index, Math.min(length, len - index));
302302
} else if (node instanceof VecApi) {
303303
node.set([[~~key, void 0]]);

src/json-size/msgpackSizeFast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const msgpackSizeFast = (value: unknown): number => {
3434
case 'boolean':
3535
return 1;
3636
}
37-
if (value instanceof Array) return arraySize(value);
37+
if (Array.isArray(value)) return arraySize(value);
3838
if (isUint8Array(value)) return 5 + value.length;
3939
if (value instanceof JsonPackValue) return (value as JsonPackValue).val.length;
4040
if (value instanceof JsonPackExtension) return 6 + (value as JsonPackExtension).val.length;

src/json-text/toTree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {stringify} from './stringify';
44
const isPrimitive = (value: unknown): boolean => typeof value !== 'object' || value === null;
55
const isOneLineValue = (value: unknown): boolean => {
66
if (isPrimitive(value)) return true;
7-
if (value instanceof Array && !value.length) return true;
7+
if (Array.isArray(value) && !value.length) return true;
88
if (value && typeof value === 'object' && !Object.keys(value).length) return true;
99
return false;
1010
};
1111
const isSimpleString = (str: string) => /^[a-z0-9]+$/i.test(str);
1212

1313
export const toTree = (value: unknown, tab: string = ''): string => {
14-
if (value instanceof Array) {
14+
if (Array.isArray(value)) {
1515
if (value.length === 0) return '[]';
1616
return printTree(
1717
tab,

src/util/strCnt.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ export const strCnt = (needle: string, haystack: string, offset: number = 0): nu
22
let cnt = 0;
33
const needleLen = needle.length;
44
if (needleLen === 0) return 0;
5+
let currentOffset = offset;
56
while (true) {
6-
const index = haystack.indexOf(needle, offset);
7+
const index = haystack.indexOf(needle, currentOffset);
78
if (index < 0) return cnt;
89
cnt++;
9-
offset = index + needleLen;
10+
currentOffset = index + needleLen;
1011
}
1112
};

src/util/throttle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const throttle = <F extends (...args: any[]) => void>(fn: F, ms: number =
1212
if (timer) return;
1313
timer = setTimeout(() => {
1414
timer = 0;
15-
fn.apply(null, lastArgs!);
15+
if (lastArgs) {
16+
fn.apply(null, lastArgs);
17+
}
1618
}, ms);
1719
}) as F;
1820
return [out, stop];

0 commit comments

Comments
 (0)