kdb_codec 1.1.0

Kdb+ IPC codec library for handling kdb+ wire protocol data with Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Security Tests: Deserialization Vulnerabilities
//!
//! Tests for integer overflow in list allocations, invalid UTF-8, and unbounded recursion

use kdb_codec::*;

#[test]
fn test_large_list_allocation_i64() {
    // Ensure list-size limits reject oversized lists without allocating.
    let size = (MAX_LIST_SIZE as u32) + 1;
    let bytes = vec![
        qtype::LONG_LIST as u8, // Type: long list
        0x00,                   // Attribute: none
        size as u8,
        (size >> 8) as u8,
        (size >> 16) as u8,
        (size >> 24) as u8, // Size: MAX_LIST_SIZE + 1
    ];

    let err = K::q_ipc_decode(&bytes, 1).expect_err("should reject oversized list");
    assert!(
        matches!(err, Error::ListTooLarge { .. }),
        "expected ListTooLarge, got: {err:?}"
    );
}

#[test]
fn test_large_list_allocation_guid() {
    // Ensure list-size limits reject oversized lists without allocating.
    let size = (MAX_LIST_SIZE as u32) + 1;
    let bytes = vec![
        qtype::GUID_LIST as u8, // Type: GUID list
        0x00,                   // Attribute: none
        size as u8,
        (size >> 8) as u8,
        (size >> 16) as u8,
        (size >> 24) as u8, // Size: MAX_LIST_SIZE + 1
    ];

    let err = K::q_ipc_decode(&bytes, 1).expect_err("should reject oversized list");
    assert!(
        matches!(err, Error::ListTooLarge { .. }),
        "expected ListTooLarge, got: {err:?}"
    );
}

#[test]
fn test_integer_overflow_in_size_calculation() {
    // The deserializer reads list lengths as u32 and checks MAX_LIST_SIZE before
    // any byte-count multiplication. This test ensures we still fail fast.
    let size = (MAX_LIST_SIZE as u32) + 1;
    let bytes = vec![
        qtype::LONG_LIST as u8,
        0x00,
        size as u8,
        (size >> 8) as u8,
        (size >> 16) as u8,
        (size >> 24) as u8,
    ];

    let err = K::q_ipc_decode(&bytes, 1).expect_err("should reject oversized list");
    assert!(matches!(err, Error::ListTooLarge { .. }));
}

#[test]
fn test_symbol_without_null_terminator() {
    // Test symbol deserialization without null terminator
    let bytes = vec![
        qtype::SYMBOL_ATOM as u8, // Type: symbol
        b'h',
        b'e',
        b'l',
        b'l',
        b'o', // "hello" without null terminator
    ];

    let err = K::q_ipc_decode(&bytes, 1).expect_err("should require null terminator");
    assert!(matches!(err, Error::MissingNullTerminator));
}

#[test]
fn test_symbol_with_invalid_utf8() {
    // Test symbol with invalid UTF-8 sequence
    let bytes = vec![
        qtype::SYMBOL_ATOM as u8, // Type: symbol
        0xFF,
        0xFE,
        0xFD, // Invalid UTF-8 bytes
        0x00, // Null terminator
    ];

    let err = K::q_ipc_decode(&bytes, 1).expect_err("should reject invalid UTF-8");
    assert!(matches!(err, Error::InvalidUtf8));
}

#[test]
fn test_string_with_invalid_utf8() {
    // Test string (char list) with invalid UTF-8
    let bytes = vec![
        qtype::STRING as u8, // Type: string
        0x00,                // Attribute: none
        0x05,
        0x00,
        0x00,
        0x00, // Size: 5 bytes
        0xFF,
        0xFE,
        0xFD,
        0xFC,
        0xFB, // Invalid UTF-8 sequence
    ];

    let err = K::q_ipc_decode(&bytes, 1).expect_err("should reject invalid UTF-8");
    assert!(matches!(err, Error::InvalidUtf8));
}

#[test]
fn test_symbol_list_with_invalid_utf8() {
    // Test symbol list with one invalid UTF-8 symbol
    let bytes = vec![
        qtype::SYMBOL_LIST as u8, // Type: symbol list
        0x00,                     // Attribute: none
        0x02,
        0x00,
        0x00,
        0x00, // Size: 2 symbols
        b'o',
        b'k',
        0x00, // First symbol: "ok"
        0xFF,
        0xFE,
        0x00, // Second symbol: invalid UTF-8
    ];

    let err = K::q_ipc_decode(&bytes, 1).expect_err("should reject invalid UTF-8");
    assert!(matches!(err, Error::InvalidUtf8));
}

#[test]
fn test_deeply_nested_compound_list() {
    // Test deeply nested structure - should hit recursion depth limit
    // MAX_RECURSION_DEPTH is 100 by default
    // Note: We need to use a larger stack for this test because debug builds
    // have larger stack frames and test threads have smaller default stacks
    let nesting_depth = 110; // Exceeds the limit

    let handle = std::thread::Builder::new()
        .stack_size(4 * 1024 * 1024) // 4MB stack
        .spawn(move || {
            let mut bytes = Vec::new();

            // Build nested compound lists
            for _ in 0..nesting_depth {
                bytes.push(qtype::COMPOUND_LIST as u8); // Type: compound list
                bytes.push(0x00); // Attribute: none
                bytes.extend_from_slice(&[0x01, 0x00, 0x00, 0x00]); // Size: 1 element
            }

            // End with simple integer
            bytes.push(qtype::INT_ATOM as u8);
            bytes.extend_from_slice(&[0x2A, 0x00, 0x00, 0x00]); // Value: 42

            match K::q_ipc_decode(&bytes, 1) {
                Ok(_) => panic!("Should have returned MaxDepthExceeded error"),
                Err(e) => assert!(matches!(e, Error::MaxDepthExceeded { .. })),
            }
        })
        .unwrap();

    handle.join().unwrap();
}

#[test]
fn test_extremely_deep_nesting() {
    // Test with extreme nesting - should also hit recursion depth limit
    let nesting_depth = 150; // Way over the 100 limit

    let handle = std::thread::Builder::new()
        .stack_size(4 * 1024 * 1024) // 4MB stack
        .spawn(move || {
            let mut bytes = Vec::new();

            for _ in 0..nesting_depth {
                bytes.push(qtype::COMPOUND_LIST as u8);
                bytes.push(0x00);
                bytes.extend_from_slice(&[0x01, 0x00, 0x00, 0x00]);
            }

            bytes.push(qtype::INT_ATOM as u8);
            bytes.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);

            println!(
                "Testing extremely deep nesting (depth: {})...",
                nesting_depth
            );
            match K::q_ipc_decode(&bytes, 1) {
                Ok(_) => panic!("Should have returned MaxDepthExceeded error"),
                Err(e) => assert!(matches!(e, Error::MaxDepthExceeded { .. })),
            }
        })
        .unwrap();

    handle.join().unwrap();
}

#[test]
fn test_nested_table_in_list() {
    // Test nested structures with tables - should also hit recursion limit
    let nesting_depth = 110; // Over the 100 limit

    let handle = std::thread::Builder::new()
        .stack_size(4 * 1024 * 1024) // 4MB stack
        .spawn(move || {
            let mut bytes = Vec::new();

            for _ in 0..nesting_depth {
                bytes.push(qtype::COMPOUND_LIST as u8);
                bytes.push(0x00);
                bytes.extend_from_slice(&[0x01, 0x00, 0x00, 0x00]);
            }

            // End with a table
            bytes.push(qtype::TABLE as u8);
            bytes.push(0x00); // Attribute
            bytes.push(qtype::DICTIONARY as u8);
            // Simple empty table structure
            bytes.push(qtype::SYMBOL_LIST as u8); // keys
            bytes.push(0x00);
            bytes.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
            bytes.push(qtype::COMPOUND_LIST as u8); // values
            bytes.push(0x00);
            bytes.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);

            match K::q_ipc_decode(&bytes, 1) {
                Ok(_) => panic!("Should have returned MaxDepthExceeded error"),
                Err(e) => assert!(matches!(e, Error::MaxDepthExceeded { .. })),
            }
        })
        .unwrap();

    handle.join().unwrap();
}

#[test]
fn test_reasonable_list_sizes() {
    // Test with reasonable list sizes that should work
    let bytes = vec![
        qtype::LONG_LIST as u8, // Type: long list
        0x00,                   // Attribute: none
        0x0A,
        0x00,
        0x00,
        0x00, // Size: 10 elements
        // 10 * 8 = 80 bytes of data
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 1
        0x02,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 2
        0x03,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 3
        0x04,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 4
        0x05,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 5
        0x06,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 6
        0x07,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 7
        0x08,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 8
        0x09,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 9
        0x0A,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00, // 10
    ];

    let k = K::q_ipc_decode(&bytes, 1).unwrap();
    let list = k.as_vec::<i64>().unwrap();

    assert_eq!(list.len(), 10);
    assert_eq!(list[0], 1);
    assert_eq!(list[9], 10);
}

#[test]
fn test_valid_symbol_list() {
    // Test with valid symbol list
    let bytes = vec![
        qtype::SYMBOL_LIST as u8, // Type: symbol list
        0x00,                     // Attribute: none
        0x03,
        0x00,
        0x00,
        0x00, // Size: 3 symbols
        b'o',
        b'n',
        b'e',
        0x00, // "one"
        b't',
        b'w',
        b'o',
        0x00, // "two"
        b't',
        b'h',
        b'r',
        b'e',
        b'e',
        0x00, // "three"
    ];

    let k = K::q_ipc_decode(&bytes, 1).unwrap();
    let list = k.as_vec::<String>().unwrap();

    assert_eq!(list.len(), 3);
    assert_eq!(list[0], "one");
    assert_eq!(list[1], "two");
    assert_eq!(list[2], "three");
}

#[test]
fn test_moderate_nesting_depth() {
    // Test with moderate nesting that should work
    let nesting_depth = 10;
    let mut bytes = Vec::new();

    for _ in 0..nesting_depth {
        bytes.push(qtype::COMPOUND_LIST as u8);
        bytes.push(0x00);
        bytes.extend_from_slice(&[0x01, 0x00, 0x00, 0x00]);
    }

    bytes.push(qtype::INT_ATOM as u8);
    bytes.extend_from_slice(&[0x2A, 0x00, 0x00, 0x00]); // 42

    let k = K::q_ipc_decode(&bytes, 1).unwrap();

    // Navigate through nesting to verify
    let mut current = &k;
    for _ in 0..nesting_depth {
        let list = current.as_vec::<K>().unwrap();
        assert_eq!(list.len(), 1);
        current = &list[0];
    }

    assert_eq!(current.get_int().unwrap(), 42);
}