hnwyllmm opened a new issue, #759:
URL: https://github.com/apache/arrow-java/issues/759

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   If `Consumer` throws exception, `TryCopyLastError` would copy exception 
message to C buffer. `TryCopyLastError` assume the string(byte[]) returned by 
`lastError` is '\0' terminated and test the length of the string by `strlen` 
which result in invalid memory access.
   
   The code of `TryCopyLastError`:
   
   ```c
     jobject error_data =
         env->GetObjectField(private_data->j_private_data_, 
kPrivateDataLastErrorField);
     ...
     auto arr = reinterpret_cast<jbyteArray>(error_data);
     jbyte* error_bytes = env->GetByteArrayElements(arr, nullptr);
     if (!error_bytes) {
       private_data->last_error_.clear();
       return;
     }
   
     char* error_str = reinterpret_cast<char*>(error_bytes);
     private_data->last_error_ = std::string(error_str, std::strlen(error_str));
   ```
   
   The code below shows how `lastError` be setted:
   ```java
   private int setLastError(Throwable err) {
         // Do not let exceptions propagate up to JNI
         try {
           StringWriter buf = new StringWriter();
           PrintWriter writer = new PrintWriter(buf);
           err.printStackTrace(writer);
           lastError = buf.toString().getBytes(StandardCharsets.UTF_8);
         } catch (Throwable e) {
           // Bail out of setting the error message - we'll still return an 
error code
           lastError = null;
         }
         return 5; // = EIO
       }
   ```


-- 
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: issues-unsubscr...@arrow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to