Files
MTranServer/tests/cld-fix-validation.test.ts
xxnuo 88010e9aa7 fix(cld2): stack overflow - increase WASM stack size
The CLD2 WASM module has a stack size of only 8KB (TOTAL_STACK=8192), leading to stack overflow crashes when processing long texts.

Error Manifestation:
- Abort when processing 282-character mixed English and Chinese text
- Error message: `RuntimeError: Aborted()`
- Crash location: `detectLanguageWithCLD` calling CLD2

- **Makefile**: Increase TOTAL_STACK from 8192 (8KB) ​​to 65536 (64KB)
- An 8-fold increase is sufficient for language detection of complex texts.
- The WASM module size remains unchanged at 1.1MB.

- Input sanitization: Remove null bytes and control characters
- UTF-8 byte boundary truncation: 512-byte limit
- Enhanced error logging: Record crash context
- API request limit: 10MB JSON parsing limit

- `detectLanguageWithLength` method accepts explicit length
- WebIDL interface definition completed
- Temporarily disabled; using the original interface with input sanitization

fix: detectMultipleLanguages

fix: detectMultipleLanguages
2026-01-01 17:10:36 +08:00

54 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, test, expect } from 'bun:test';
import { detectLanguage } from '@/services/detector';
describe('CLD2 Memory Safety Tests', () => {
test('包含 null 字节的字符串', async () => {
const text = 'Hello\0World';
const result = await detectLanguage(text);
expect(result).toBeDefined();
expect(typeof result).toBe('string');
});
test('超长文本1MB', async () => {
const text = 'A'.repeat(1024 * 1024);
const result = await detectLanguage(text);
expect(result).toBeDefined();
});
test('混合 UTF-8 多字节字符', async () => {
const text = '你好世界🌍Hello'.repeat(1000);
const result = await detectLanguage(text);
expect(result).toBeDefined();
});
test('控制字符', async () => {
const text = 'Test\x01\x02\x03Text';
const result = await detectLanguage(text);
expect(result).toBeDefined();
});
test('连续多次检测不崩溃', async () => {
for (let i = 0; i < 100; i++) {
const text = `Test ${i} with special chars 你好\0\x01`;
await detectLanguage(text);
}
});
test('空文本', async () => {
const result = await detectLanguage('');
expect(result).toBe('');
});
test('纯空白字符', async () => {
const text = ' \n\t ';
const result = await detectLanguage(text);
expect(result).toBeDefined();
});
test('emoji 表情符号', async () => {
const text = '🎉🎊🎈🎁🎀';
const result = await detectLanguage(text);
expect(result).toBeDefined();
});
});