Skip to content

JSON deserialization of empty HashMap results corrupted object #5998

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

Closed
alexbers opened this issue Apr 22, 2013 · 1 comment
Closed

JSON deserialization of empty HashMap results corrupted object #5998

alexbers opened this issue Apr 22, 2013 · 1 comment

Comments

@alexbers
Copy link

This code(saving an emtpy map as json file and loading it):

extern mod std;

use core::path::Path;
use core::hashmap::HashMap;

use std::serialize::{Encodable, Decodable};


fn save_db(queues: &HashMap<~str,~str>) {
    match io::buffered_file_writer(&Path("db.json")) {
        Ok(wr) => {
            let enc = ~std::json::Encoder(wr);
            queues.encode(enc);
        }
        Err(e) => {
            println(e);
        }
    }
}

fn load_db() -> HashMap<~str,~str> {
    match io::file_reader(&Path("db.json")) {
        Ok(rd) => {
            match std::json::from_reader(rd) {
                Ok(json) => {
                    let dec = ~std::json::Decoder(json);
                    return Decodable::decode(dec);
                },
                Err(_) => {
                    println("Db file is bad, using empty db");
                    return HashMap::new();
                }
            }
        }
        Err(e) => {
            println(e);
            return HashMap::new();
        }
    }
}

fn main() {
    let mut map : HashMap<~str,~str> = HashMap::new();

    save_db(&map);
    let mut map = load_db();

    map.insert(~"key", ~"val");
}

Gives on insert operation:

rust: task failed at 'modulo zero', json.rs:1

I think it is because the internal structure of map is wrong after loading.

bors added a commit that referenced this issue Jun 1, 2013
Closes #5090 by using the excellent new generic deriving code

Promotes the unreachable code attribute to a lint attribute (instead of always being a warning)

Fixes some edge cases when creating hashmaps/hashsets and also when consuming them. (fixes #5998)
@alexcrichton
Copy link
Member

Closed as part of #6851

flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 24, 2020
Add map_err_ignore lint

In a large code base a lot of times errors are ignored by using something like:

```rust
foo.map_err(|_| Some::Enum)?;
```

This drops the original error in favor of a enum that will not have the original error's context. This lint helps catch throwing away the original error in favor of an enum without its context.

---

*Please keep the line below*
changelog: Added map_err_ignore lint
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