From b13fb6878939271f89ed30d05b34c444efce708a Mon Sep 17 00:00:00 2001
From: Geoff Martin <geoff.martin@zettascale.tech>
Date: Wed, 11 Sep 2024 09:24:04 +0100
Subject: [PATCH] Addressed NonNull::new_unchecked violation when accessing
 certain QoS values. (#23)

---
 src/qos.rs | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/src/qos.rs b/src/qos.rs
index 1583486..4f43728 100644
--- a/src/qos.rs
+++ b/src/qos.rs
@@ -522,12 +522,13 @@ unsafe fn user_data_from_qos_native(qos: *const dds_qos_t) -> Option<Vec<u8>> {
     let mut sz: usize = 0;
     let mut value: *mut ::std::os::raw::c_void = std::ptr::null_mut();
     if dds_qget_userdata(qos, &mut value, &mut sz) {
-        // Cyclone DDS returns a copy of the value so okay to take ownership
-        to_option(Vec::from_raw_parts(
-            value as *mut ::std::os::raw::c_uchar,
-            sz,
-            sz,
-        ))
+        let vec = if value.is_null() {
+            Vec::with_capacity(0)
+        } else {
+            // Cyclone DDS returns a copy of the value so okay to take ownership
+            Vec::from_raw_parts(value as *mut ::std::os::raw::c_uchar, sz, sz)
+        };
+        to_option(vec)
     } else {
         None
     }
@@ -545,12 +546,13 @@ unsafe fn topic_data_from_qos_native(qos: *const dds_qos_t) -> Option<Vec<u8>> {
     let mut sz: usize = 0;
     let mut value: *mut ::std::os::raw::c_void = std::ptr::null_mut();
     if dds_qget_topicdata(qos, &mut value, &mut sz) {
-        // Cyclone DDS returns a copy of the value so okay to take ownership
-        to_option(Vec::from_raw_parts(
-            value as *mut ::std::os::raw::c_uchar,
-            sz,
-            sz,
-        ))
+        let vec = if value.is_null() {
+            Vec::with_capacity(0)
+        } else {
+            // Cyclone DDS returns a copy of the value so okay to take ownership
+            Vec::from_raw_parts(value as *mut ::std::os::raw::c_uchar, sz, sz)
+        };
+        to_option(vec)
     } else {
         None
     }
@@ -568,12 +570,13 @@ unsafe fn group_data_from_qos_native(qos: *const dds_qos_t) -> Option<Vec<u8>> {
     let mut sz: usize = 0;
     let mut value: *mut ::std::os::raw::c_void = std::ptr::null_mut();
     if dds_qget_groupdata(qos, &mut value, &mut sz) {
-        // Cyclone DDS returns a copy of the value so okay to take ownership
-        to_option(Vec::from_raw_parts(
-            value as *mut ::std::os::raw::c_uchar,
-            sz,
-            sz,
-        ))
+        let vec = if value.is_null() {
+            Vec::with_capacity(0)
+        } else {
+            // Cyclone DDS returns a copy of the value so okay to take ownership
+            Vec::from_raw_parts(value as *mut ::std::os::raw::c_uchar, sz, sz)
+        };
+        to_option(vec)
     } else {
         None
     }