gemini-code-assist[bot] commented on code in PR #665:
URL: https://github.com/apache/tvm-ffi/pull/665#discussion_r3581312607


##########
include/tvm/ffi/object.h:
##########
@@ -529,6 +529,35 @@ class ObjectPtr {
   friend struct tvm::ffi::details::ObjectUnsafe;
 };
 
+namespace details {
+
+/*!
+ * \brief Normalize an unqualified Object subclass to its owning pointer type.
+ * \tparam T The type to normalize.
+ *
+ * Generic FFI containers use this trait for their public value types, so that
+ * `Container<TObject>` is shorthand for storing `ObjectPtr<TObject>`. Types
+ * that are not unqualified Object subclasses are left unchanged.
+ */
+template <typename T, typename = void>
+inline constexpr bool is_object_subclass_v = false;
+
+template <typename T>
+inline constexpr bool
+    is_object_subclass_v<T, 
std::void_t<decltype(static_cast<Object*>(std::declval<T*>()))>> = true;

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The current implementation of `is_object_subclass_v` uses 
`static_cast<Object*>(std::declval<T*>())` via `std::void_t` to detect if `T` 
is a subclass of `Object`. However, this has a loophole: `void*` can be 
explicitly cast to any pointer-to-object type (including `Object*`) using 
`static_cast`. As a result, `is_object_subclass_v<void>` incorrectly evaluates 
to `true`.
   
   Using `std::is_base_of_v<Object, T>` is the standard, safer, and much 
simpler way to check if `T` is `Object` or derived from `Object`, and it 
correctly evaluates to `false` for `void`.
   
   ```c
   template <typename T>
   inline constexpr bool is_object_subclass_v = std::is_base_of_v<Object, T>;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to