From dbbaab144314d9d8635a2fdf06a9c2369e21cd56 Mon Sep 17 00:00:00 2001 From: Alan Liddell Date: Wed, 10 Jan 2024 17:47:21 -0500 Subject: [PATCH] Handle null pointers returned from `map_read()` (#158) Fix for the error Micah ran into the other day. --- Cargo.toml | 2 +- src/runtime.rs | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3580714..43fc626 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "acquire-imaging" authors = ["Nathan Clack "] -version = "0.3.0-rc1" +version = "0.3.0-rc2" edition = "2021" [lib] diff --git a/src/runtime.rs b/src/runtime.rs index 664fe4d..62a7ef5 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -338,7 +338,11 @@ impl AvailableDataContext { } = self; let stream_id = *stream_id; let (beg, end) = inner.map_read(stream_id)?; - let nbytes = unsafe { byte_offset_from(beg, end) }; + let nbytes = if beg.is_null() || end.is_null() { + 0 + } else { + unsafe { byte_offset_from(beg, end) } + }; log::trace!( "[stream {}] ACQUIRED {:p}-{:p}:{} bytes", @@ -351,13 +355,17 @@ impl AvailableDataContext { Py::new( py, AvailableData { - inner: Arc::new(Mutex::new(Some(RawAvailableData { - runtime: self.inner.clone(), - beg: NonNull::new(beg).ok_or(anyhow!("Expected non-null buffer"))?, - end: NonNull::new(end).ok_or(anyhow!("Expected non-null buffer"))?, - stream_id, - consumed_bytes: None, - }))), + inner: Arc::new(Mutex::new(if nbytes > 0 { + Some(RawAvailableData { + runtime: self.inner.clone(), + beg: NonNull::new(beg).ok_or(anyhow!("Expected non-null buffer"))?, + end: NonNull::new(end).ok_or(anyhow!("Expected non-null buffer"))?, + stream_id, + consumed_bytes: None, + }) + } else { + None + })), }, ) })?;