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


##########
python/tvm_ffi/container.py:
##########
@@ -337,6 +341,10 @@ def __len__(self) -> int:
         """Return the number of items in the map."""
         return _ffi_api.MapSize(self)
 
+    def __bool__(self) -> bool:
+        """Return True if the map is non-empty."""
+        return len(self) > 0

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   Similar to the `Array` container, this implementation for `Map` is not safe 
for objects that failed to initialize and have a null C handle. Calling 
`len(self)` will likely cause a crash. Please add a guard to check for a valid 
handle before calling `len()`, making an uninitialized `Map` falsy. The 
`__repr__` method provides an example of this safety check.
   
   ```suggestion
           return self.__chandle__() != 0 and len(self) > 0
   ```



##########
python/tvm_ffi/container.py:
##########
@@ -192,6 +192,10 @@ def __contains__(self, value: object) -> bool:
         """Check if the array contains a value."""
         return _ffi_api.ArrayContains(self, value)
 
+    def __bool__(self) -> bool:
+        """Return True if the array is non-empty."""
+        return len(self) > 0

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The current implementation can cause a crash if the `Array` object was not 
initialized correctly and has a null C handle. Calling `len(self)` on such an 
object can lead to a null pointer dereference in the FFI layer. To make this 
method safer, please add a check for the handle's validity before calling 
`len()`, similar to the existing pattern in `__repr__`. An uninitialized 
`Array` should be treated as falsy.
   
   ```suggestion
           return self.__chandle__() != 0 and len(self) > 0
   ```



-- 
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