Fix convertToFenceHandle()

... to not return address to a local stack variable.

Noticed by dustingreen@

Bug: b/287536665
Test: cvd start && vts -m VtsHalGraphicsMapperV4_0Target
Change-Id: Iba486270a7ae3f8fbe50b41e3ea4c32aa0c4acad
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/minigbm/+/4615434
Reviewed-by: Yiwei Zhang <zzyiwei@chromium.org>
Auto-Submit: Jason Macnak <natsu@google.com>
Tested-by: Jason Macnak <natsu@google.com>
Commit-Queue: Jason Macnak <natsu@google.com>
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc
index 90d9506..0053052 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc
+++ b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc
@@ -315,15 +315,8 @@
         return Void();
     }
 
-    hidl_handle releaseFenceHandle;
-    ret = convertToFenceHandle(releaseFenceFd, &releaseFenceHandle);
-    if (ret) {
-        ALOGE("Failed to unlock. Failed to convert release fence to handle.");
-        hidlCb(Error::BAD_BUFFER, nullptr);
-        return Void();
-    }
-
-    hidlCb(Error::NONE, releaseFenceHandle);
+    NATIVE_HANDLE_DECLARE_STORAGE(releaseFenceHandleStorage, 1, 0);
+    hidlCb(Error::NONE, convertToFenceHandle(releaseFenceFd, releaseFenceHandleStorage));
     return Void();
 }
 
@@ -355,15 +348,8 @@
         return Void();
     }
 
-    hidl_handle releaseFenceHandle;
-    ret = convertToFenceHandle(releaseFenceFd, &releaseFenceHandle);
-    if (ret) {
-        ALOGE("Failed to flushLockedBuffer. Failed to convert release fence to handle.");
-        hidlCb(Error::BAD_BUFFER, nullptr);
-        return Void();
-    }
-
-    hidlCb(Error::NONE, releaseFenceHandle);
+    NATIVE_HANDLE_DECLARE_STORAGE(releaseFenceHandleStorage, 1, 0);
+    hidlCb(Error::NONE, convertToFenceHandle(releaseFenceFd, releaseFenceHandleStorage));
     return Void();
 }
 
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Utils.cc b/cros_gralloc/gralloc4/CrosGralloc4Utils.cc
index 515269a..25b42b5 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Utils.cc
+++ b/cros_gralloc/gralloc4/CrosGralloc4Utils.cc
@@ -99,20 +99,13 @@
     return 0;
 }
 
-int convertToFenceHandle(int fenceFd, hidl_handle* outFenceHandle) {
-    if (!outFenceHandle) {
-        return -EINVAL;
+hidl_handle convertToFenceHandle(int fenceFd, char* nativeHandleStorage) {
+    native_handle_t* nativeHandle = nullptr;
+    if (fenceFd >= 0) {
+        nativeHandle = native_handle_init(nativeHandleStorage, 1, 0);
+        nativeHandle->data[0] = fenceFd;
     }
-    if (fenceFd < 0) {
-        return 0;
-    }
-
-    NATIVE_HANDLE_DECLARE_STORAGE(handleStorage, 1, 0);
-    auto fenceHandle = native_handle_init(handleStorage, 1, 0);
-    fenceHandle->data[0] = fenceFd;
-
-    *outFenceHandle = fenceHandle;
-    return 0;
+    return hidl_handle(nativeHandle);
 }
 
 const std::unordered_map<uint32_t, std::vector<PlaneLayout>>& GetPlaneLayoutsMap() {
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Utils.h b/cros_gralloc/gralloc4/CrosGralloc4Utils.h
index fa096ef..d0320f2 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Utils.h
+++ b/cros_gralloc/gralloc4/CrosGralloc4Utils.h
@@ -10,6 +10,7 @@
 #include <aidl/android/hardware/graphics/common/PlaneLayout.h>
 #include <android/hardware/graphics/common/1.2/types.h>
 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
+#include <hidl/HidlSupport.h>
 
 struct cros_gralloc_buffer_descriptor;
 
@@ -32,7 +33,7 @@
 
 int convertToFenceFd(const android::hardware::hidl_handle& fence_handle, int* out_fence_fd);
 
-int convertToFenceHandle(int fence_fd, android::hardware::hidl_handle* out_fence_handle);
+android::hardware::hidl_handle convertToFenceHandle(int fence_fd, char* native_handle_storage);
 
 int getPlaneLayouts(
         uint32_t drm_format,