From 4215e2a398d7065f0fc212785498587307093e21 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 17 Sep 2021 10:49:47 -0500 Subject: [PATCH] Handle null memory addresses in serdes --- src/coffi/ffi.clj | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/coffi/ffi.clj b/src/coffi/ffi.clj index 1ce7b66..d6d21a0 100644 --- a/src/coffi/ffi.clj +++ b/src/coffi/ffi.clj @@ -259,11 +259,12 @@ (defmethod serialize* ::pointer [obj type scope] - (if (sequential? type) - (let [segment (alloc-instance (second type) scope)] - (serialize-into obj (second type) segment scope) - (address-of segment)) - obj)) + (when-not (null? obj) + (if (sequential? type) + (let [segment (alloc-instance (second type) scope)] + (serialize-into obj (second type) segment scope) + (address-of segment)) + obj))) (defmulti serialize-into "Writes a serialized version of the `obj` to the given `segment`. @@ -419,10 +420,11 @@ (defmethod deserialize* ::pointer [addr type] - (if (sequential? type) - (deserialize (slice-global addr (size-of (second type))) - (second type)) - addr)) + (when-not (null? addr) + (if (sequential? type) + (deserialize-from (slice-global addr (size-of (second type))) + (second type)) + addr))) (defn deserialize "Deserializes an arbitrary type. @@ -445,11 +447,14 @@ (defmethod serialize* ::c-string [obj _type scope] - (address-of (CLinker/toCString (str obj) ^ResourceScope scope))) + (if obj + (address-of (CLinker/toCString (str obj) ^ResourceScope scope)) + (MemoryAddress/NULL))) (defmethod deserialize* ::c-string [addr _type] - (CLinker/toJavaString ^MemoryAddress addr)) + (when-not (null? addr) + (CLinker/toJavaString ^MemoryAddress addr))) #_(defn seq-of "Constructs a lazy sequence of `type` elements deserialized from `segment`."