json-rust icon indicating copy to clipboard operation
json-rust copied to clipboard

Array length bug after trying to take out of bounds element

Open roignpar opened this issue 5 years ago • 1 comments

Hello!

First, thank you for this great crate!

I believe that I found a bug when working with arrays. It seems that after trying to take a value with an out of bounds index from an array JsonValue, the len of the array changes. Minimal code to illustrate:

fn main() {
    let mut array = json::parse(&create_data(10)).unwrap();

    assert_eq!(array.len(), 10);

    array[9].take();

    assert_eq!(array.len(), 10);
    println!("len correct after taking element from index 9");

    array[10].take();

    assert_eq!(array.len(), 10);
}

fn create_data(len: usize) -> String {
    let mut data = String::from("[");

    for i in 0..len {
        data.push_str(r#"{"hello": 1}"#);

        if i != len - 1 {
            data.push(',');
        }
    }

    data.push(']');

    data
}

Outputs:

len correct after taking element from index 9
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `11`,
 right: `10`', src/main.rs:13:5

roignpar avatar Jul 01 '20 16:07 roignpar

the len of the array is 10, so array[10] is out of bound, and will push a null

zyctree avatar Jul 06 '20 12:07 zyctree