1
//! Tests for `write_debug_string_gc` (moved out of `debug_values.rs` per
2
//! CLAUDE.md's file-size guideline).
3

            
4
use super::*;
5
use crate::compiler::emit::FunctionEmitter;
6
use crate::compiler::layout::GcLocals;
7

            
8
const LOCAL_GC_ARR_FOR_TEST: u32 = 0;
9
const LOCAL_IDX_FOR_TEST: u32 = 1;
10

            
11
/// Carried-in fix (ADR-0033 Phase 3): `write_debug_string_gc` used to
12
/// compute `DEBUG_VALUE_DATA_SIZE as u32 + len` and `DEBUG_ENTITY_BYTES +
13
/// len` with plain `+`, which wraps silently in release on overflow. A
14
/// string length near `u32::MAX` (unreachable from a real script byte
15
/// buffer, but the guest supplies `len` as a plain i32) must now error
16
/// rather than compute a wrapped, wrong entity size.
17
#[test]
18
1
fn write_debug_string_gc_errors_on_a_length_that_would_overflow() {
19
1
    let mut serializer = OutputSerializer::new(0);
20
1
    let mut emit = FunctionEmitter::new();
21
1
    let gc = GcLocals {
22
1
        type_idx: 0,
23
1
        arr: LOCAL_GC_ARR_FOR_TEST,
24
1
        idx: LOCAL_IDX_FOR_TEST,
25
1
    };
26
1
    let err = serializer
27
1
        .write_debug_string_gc(&mut emit, ValueType::String, 0, u32::MAX, &gc)
28
1
        .expect_err("a length this close to u32::MAX must overflow the debug entity size");
29
1
    assert!(err.to_string().contains("overflows a debug entity"));
30
1
}