From 4ae5509d7feac37837cfb93ff27170f3ead3bd6a Mon Sep 17 00:00:00 2001 From: kirk Date: Sat, 23 Sep 2023 18:19:34 +0000 Subject: [PATCH] add tests to ron and json indicating array deser leak --- tests/bin.rs | 2 +- tests/json.rs | 28 +++++++++++++++++++++++++++- tests/ron.rs | 28 +++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/tests/bin.rs b/tests/bin.rs index 2e0bee6..70eabc7 100644 --- a/tests/bin.rs +++ b/tests/bin.rs @@ -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)] diff --git a/tests/json.rs b/tests/json.rs index 51f17c6..0cc1708 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -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() { @@ -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)) +} diff --git a/tests/ron.rs b/tests/ron.rs index f9ac9d2..e8207cf 100644 --- a/tests/ron.rs +++ b/tests/ron.rs @@ -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() { @@ -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)) +} \ No newline at end of file