Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array length bug after trying to take out of bounds element #187

Open
roignpar opened this issue Jul 1, 2020 · 1 comment
Open

Array length bug after trying to take out of bounds element #187

roignpar opened this issue Jul 1, 2020 · 1 comment

Comments

@roignpar
Copy link

roignpar commented Jul 1, 2020

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
@zyctree
Copy link

zyctree commented Jul 6, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants