From 27e600c1ac83edad8d47c4b60ebfd09dd176da9c Mon Sep 17 00:00:00 2001 From: nattthebear Date: Mon, 12 Oct 2020 11:34:18 -0400 Subject: [PATCH] Fix some confusions in no-dirty-detection mode This caused some awful crashes, but doesn't affect any real release builds of waterboxhost --- src/BizHawk.Common/SpanStream.cs | 6 ++++++ waterbox/waterboxhost/src/memory_block/mod.rs | 20 ++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/BizHawk.Common/SpanStream.cs b/src/BizHawk.Common/SpanStream.cs index a05bb35721..08401cdca2 100644 --- a/src/BizHawk.Common/SpanStream.cs +++ b/src/BizHawk.Common/SpanStream.cs @@ -34,6 +34,9 @@ namespace BizHawk.Common private readonly Stream _stream; public unsafe int Read(Span buffer) { + if (buffer.Length == 0) + return 0; + if (buffer.Length > _buffer.Length) { _buffer = new byte[buffer.Length]; @@ -48,6 +51,9 @@ namespace BizHawk.Common public unsafe void Write(ReadOnlySpan buffer) { + if (buffer.Length == 0) + return; + if (buffer.Length > _buffer.Length) { _buffer = new byte[buffer.Length]; diff --git a/waterbox/waterboxhost/src/memory_block/mod.rs b/waterbox/waterboxhost/src/memory_block/mod.rs index 44ed2ebf73..04fdd2cbc7 100644 --- a/waterbox/waterboxhost/src/memory_block/mod.rs +++ b/waterbox/waterboxhost/src/memory_block/mod.rs @@ -817,13 +817,14 @@ impl MemoryBlock { self.refresh_all_protections(); self.sealed = true; - #[cfg(not(feature = "no-dirty-detection"))] - { - use sha2::{Sha256, Digest}; - self.hash = { - let mut hasher = Sha256::new(); - bin::write(&mut hasher, &self.addr).unwrap(); + use sha2::{Sha256, Digest}; + self.hash = { + let mut hasher = Sha256::new(); + bin::write(&mut hasher, &self.addr).unwrap(); + + #[cfg(not(feature = "no-dirty-detection"))] + { for p in self.pages.iter() { match &p.snapshot { Snapshot::None => bin::writeval(&mut hasher, 1).unwrap(), @@ -831,9 +832,10 @@ impl MemoryBlock { Snapshot::Data(d) => { hasher.write(d.slice()).unwrap(); }, } } - hasher.finalize()[..].to_owned() - }; - } + } + + hasher.finalize()[..].to_owned() + }; Ok(()) }