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


##########
python/tvm_ffi/cython/function.pxi:
##########
@@ -674,6 +674,22 @@ cdef int TVMFFIPyArgSetterFloatProtocol_(
     return 0
 
 
+cdef int TVMFFIPyArgSetterFFIValueProtocol_(
+    TVMFFIPyArgSetter* handle, TVMFFIPyCallContext* ctx,
+    PyObject* py_arg, TVMFFIAny* out
+) except -1:
+    """Setter for class with __tvm_ffi_value__() method"""
+    cdef object arg = <object>py_arg
+    cdef object ffi_value_py_obj = arg.__tvm_ffi_value__()
+    cdef PyObject* ffi_value_py_obj_ptr = <PyObject*>ffi_value_py_obj
+    # keep alive the python object since this is a temporary object
+    # we must push to extra temp py objects stack to avoid overflow the temp 
py objects stack
+    TVMFFIPyPushExtraTempPyObject(ctx, ffi_value_py_obj_ptr)
+    return TVMFFIPySetArgumentGenericDispatcher(
+        TVMFFIPyArgSetterFactory_, ctx, ffi_value_py_obj_ptr, out
+    )

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The current implementation of `TVMFFIPyArgSetterFFIValueProtocol_` is 
vulnerable to infinite recursion if an object's `__tvm_ffi_value__` method 
returns `self`. This would cause a stack overflow. You should add a check to 
prevent this.
   
   ```
       cdef object ffi_value_py_obj = arg.__tvm_ffi_value__()
       if ffi_value_py_obj is arg:
           PyErr_SetString(PyExc_ValueError, "`__tvm_ffi_value__` cannot return 
self, this would cause infinite recursion.")
           return -1
       cdef PyObject* ffi_value_py_obj_ptr = <PyObject*>ffi_value_py_obj
       # keep alive the python object since this is a temporary object
       # we must push to extra temp py objects stack to avoid overflow the temp 
py objects stack
       TVMFFIPyPushExtraTempPyObject(ctx, ffi_value_py_obj_ptr)
       return TVMFFIPySetArgumentGenericDispatcher(
           TVMFFIPyArgSetterFactory_, ctx, ffi_value_py_obj_ptr, out
       )
   ```



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