Skip to content

Commit 3cee96b

Browse files
Copilotstreamich
andcommitted
Remove duplicate methods from EjsonDecoder and fix Unicode handling in fuzzing tests
Co-authored-by: streamich <[email protected]>
1 parent 65e4aa1 commit 3cee96b

File tree

2 files changed

+5
-123
lines changed

2 files changed

+5
-123
lines changed

src/ejson2/EjsonDecoder.ts

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -61,119 +61,7 @@ export class EjsonDecoder extends JsonDecoder {
6161
}
6262
}
6363

64-
public skipWhitespace(): void {
65-
const reader = this.reader;
66-
const uint8 = reader.uint8;
67-
let x = reader.x;
68-
let char: number = 0;
69-
while (true) {
70-
char = uint8[x];
71-
switch (char) {
72-
case 32 /* <space> */:
73-
case 9 /* <tab> */:
74-
case 10 /* <line feed> */:
75-
case 13 /* <carriage return> */:
76-
x++;
77-
continue;
78-
default:
79-
reader.x = x;
80-
return;
81-
}
82-
}
83-
}
84-
85-
public readNull(): null {
86-
if (this.reader.u32() !== 0x6e756c6c /* null */) throw new Error('Invalid JSON');
87-
return null;
88-
}
89-
90-
public readTrue(): true {
91-
if (this.reader.u32() !== 0x74727565 /* true */) throw new Error('Invalid JSON');
92-
return true;
93-
}
94-
95-
public readFalse(): false {
96-
const reader = this.reader;
97-
if (reader.u8() !== 0x66 /* f */ || reader.u32() !== 0x616c7365 /* alse */) throw new Error('Invalid JSON');
98-
return false;
99-
}
100-
101-
public readNum(): number {
102-
const reader = this.reader;
103-
const uint8 = reader.uint8;
104-
let x = reader.x;
105-
let c = uint8[x++];
106-
const c1 = c;
107-
c = uint8[x++];
108-
if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
109-
reader.x = x - 1;
110-
const num = +String.fromCharCode(c1);
111-
if (num !== num) throw new Error('Invalid JSON');
112-
return num;
113-
}
114-
const c2 = c;
115-
c = uint8[x++];
116-
if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
117-
reader.x = x - 1;
118-
const num = +String.fromCharCode(c1, c2);
119-
if (num !== num) throw new Error('Invalid JSON');
120-
return num;
121-
}
122-
// Continue reading for longer numbers (simplified from JsonDecoder)
123-
const points: number[] = [c1, c2];
124-
while (c && ((c >= 45 && c <= 57) || c === 43 || c === 69 || c === 101)) {
125-
points.push(c);
126-
c = uint8[x++];
127-
}
128-
reader.x = x - 1;
129-
const num = +String.fromCharCode.apply(String, points);
130-
if (num !== num) throw new Error('Invalid JSON');
131-
return num;
132-
}
13364

134-
public readStr(): string {
135-
const reader = this.reader;
136-
const uint8 = reader.uint8;
137-
const char = uint8[reader.x++];
138-
if (char !== 0x22) throw new Error('Invalid JSON');
139-
const x0 = reader.x;
140-
let x1 = x0;
141-
142-
// Find ending quote (simplified version)
143-
while (x1 < uint8.length) {
144-
const c = uint8[x1];
145-
if (c === 0x22 /* " */ && uint8[x1 - 1] !== 0x5c /* \ */) {
146-
break;
147-
}
148-
x1++;
149-
}
150-
151-
if (x1 >= uint8.length) throw new Error('Invalid JSON');
152-
153-
// Decode UTF-8 string
154-
let str = '';
155-
for (let i = x0; i < x1; i++) {
156-
str += String.fromCharCode(uint8[i]);
157-
}
158-
159-
// Handle escaped characters (simplified)
160-
str = str.replace(/\\(b|f|n|r|t|"|\/|\\)/g, (match, char) => {
161-
switch (char) {
162-
case 'b': return '\b';
163-
case 'f': return '\f';
164-
case 'n': return '\n';
165-
case 'r': return '\r';
166-
case 't': return '\t';
167-
case '"': return '"';
168-
case '/': return '/';
169-
case '\\': return '\\';
170-
default: return match;
171-
}
172-
});
173-
174-
reader.x = x1 + 1;
175-
return str;
176-
}
17765

17866
public readArr(): unknown[] {
17967
const reader = this.reader;

src/ejson2/__tests__/fuzzing.spec.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@ const decoder = new EjsonDecoder();
99

1010
describe('fuzzing', () => {
1111
test('EjsonEncoder - Relaxed Mode (JSON compatibility)', () => {
12-
for (let i = 0; i < 100; i++) { // Reduced iterations to avoid Unicode issues in fuzzing
13-
const value = JSON.parse(JSON.stringify(RandomJson.generate()));
14-
try {
15-
const encoded = relaxedEncoder.encode(value);
16-
const decoded = decoder.decode(encoded);
17-
expect(decoded).toStrictEqual(value);
18-
} catch (err) {
19-
// Skip this iteration if there are Unicode or other encoding issues
20-
// This is expected behavior for a test suite - some random data may not round-trip perfectly
21-
continue;
22-
}
12+
for (let i = 0; i < 200; i++) {
13+
const value = RandomJson.generate();
14+
const encoded = relaxedEncoder.encode(value);
15+
const decoded = decoder.decode(encoded);
16+
expect(decoded).toStrictEqual(value);
2317
}
2418
});
2519
});

0 commit comments

Comments
 (0)