[Mesa-dev] [PATCH] clover: always return number of devices in clGetDeviceIDs

Giuseppe Bilotta giuseppe.bilotta at gmail.com
Mon Mar 2 07:52:13 PST 2015


When no devices of the requested type are available, clGetDeviceIDs()
returns a CL_DEVICE_NOT_FOUND, as per the specification.

The specification does not mention explictly wether the number of
devices (return parameter) should be set to 0 in this case, but some
applications will crash (segfault) if not, due to use of uninitialized
memory.

Although it could be argued that such issues are bugs in the
applications (failing to check for return values where appropriate)
it could also be argued that there is no reason to not set the return
parameter anyway, so let's just give those unsafe applications a hand
and always set the parameter.

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta at gmail.com>

---
 src/gallium/state_trackers/clover/api/device.cpp | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)


I came across this issue with LibreOffice (the LO side of the issue is now
fixed on master, BTW), but I know it's not the only application misbehaving
in this sense. It might make sense to consider the patch for the stable branch
as well.


diff --git a/src/gallium/state_trackers/clover/api/device.cpp b/src/gallium/state_trackers/clover/api/device.cpp
index e825468..7e492ab 100644
--- a/src/gallium/state_trackers/clover/api/device.cpp
+++ b/src/gallium/state_trackers/clover/api/device.cpp
@@ -45,12 +45,15 @@ clGetDeviceIDs(cl_platform_id d_platform, cl_device_type device_type,
          d_devs.push_back(desc(dev));
    }
 
-   if (d_devs.empty())
-      throw error(CL_DEVICE_NOT_FOUND);
-
-   // ...and return the requested data.
+   // always return the number of devices, even when
+   // no devices are found
    if (rnum_devices)
       *rnum_devices = d_devs.size();
+
+   if (d_devs.empty())
+      throw error(CL_DEVICE_NOT_FOUND);
+
+   // ...and return the rest of the requested data.
    if (rd_devices)
       copy(range(d_devs.begin(),
                  std::min((unsigned)d_devs.size(), num_entries)),
-- 
2.1.2.766.gaa23a90



More information about the mesa-dev mailing list