Skip to content

Commit

Permalink
add tests to ron and json indicating array deser leak
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish committed Sep 23, 2023
1 parent fa4135b commit 4ae5509
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn collections() {
}

#[test]
fn leak_test() {
fn array_leak_test() {
static TOGGLED_ON_DROP: AtomicBool = AtomicBool::new(false);

#[derive(Default, Clone, SerBin, DeBin)]
Expand Down
28 changes: 27 additions & 1 deletion tests/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nanoserde::{DeJson, SerJson};

use std::collections::{BTreeSet, HashMap, HashSet, LinkedList};
use std::{collections::{BTreeSet, HashMap, HashSet, LinkedList}, sync::atomic::AtomicBool};

#[test]
fn de() {
Expand Down Expand Up @@ -805,3 +805,29 @@ fn ser_str() {
SerJson::serialize_json(&a_str)
);
}

#[test]
fn array_leak_test() {
static TOGGLED_ON_DROP: AtomicBool = AtomicBool::new(false);

#[derive(Default, Clone, SerJson, DeJson)]
struct IncrementOnDrop {
inner: u64
}

impl Drop for IncrementOnDrop {
fn drop(&mut self) {
TOGGLED_ON_DROP.store(true, std::sync::atomic::Ordering::SeqCst);
}
}

let items: [_;2] = core::array::from_fn(|_| IncrementOnDrop::default());
let serialized = nanoserde::SerJson::serialize_json(&items);
let corrupted_serialized = &serialized[..serialized.len() - 1];

if let Ok(_) = <[IncrementOnDrop;2] as nanoserde::DeJson>::deserialize_json(corrupted_serialized) {
panic!("Unexpected success")
}

assert!(TOGGLED_ON_DROP.load(std::sync::atomic::Ordering::SeqCst))
}
28 changes: 27 additions & 1 deletion tests/ron.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nanoserde::{DeRon, SerRon};

use std::collections::{BTreeSet, HashMap, HashSet, LinkedList};
use std::{collections::{BTreeSet, HashMap, HashSet, LinkedList}, sync::atomic::AtomicBool};

#[test]
fn ron_de() {
Expand Down Expand Up @@ -421,3 +421,29 @@ fn tuple_struct() {

assert!(test == test_deserialized);
}

#[test]
fn array_leak_test() {
static TOGGLED_ON_DROP: AtomicBool = AtomicBool::new(false);

#[derive(Default, Clone, DeRon, SerRon)]
struct IncrementOnDrop {
inner: u64
}

impl Drop for IncrementOnDrop {
fn drop(&mut self) {
TOGGLED_ON_DROP.store(true, std::sync::atomic::Ordering::SeqCst);
}
}

let items: [_;2] = core::array::from_fn(|_| IncrementOnDrop::default());
let serialized = nanoserde::SerRon::serialize_ron(&items);
let corrupted_serialized = &serialized[..serialized.len() - 1];

if let Ok(_) = <[IncrementOnDrop;2] as nanoserde::DeRon>::deserialize_ron(corrupted_serialized) {
panic!("Unexpected success")
}

assert!(TOGGLED_ON_DROP.load(std::sync::atomic::Ordering::SeqCst))
}

0 comments on commit 4ae5509

Please sign in to comment.