Senrian opened a new pull request, #10227:
URL: https://github.com/apache/rocketmq/pull/10227

   ## Summary
   
   Fixes issue #10208 - `IOTinyUtils.copyFile()` has a resource leak bug where 
if the `FileInputStream` constructor throws an exception after the 
`FileOutputStream` has been created, the `FileOutputStream` and its underlying 
file descriptor are leaked.
   
   ## Changes
   
   Convert the `copyFile` method from manual try-finally resource management to 
try-with-resources pattern:
   
   **Before (buggy):**
   ```java
   FileChannel sc = null;
   FileChannel tc = null;
   try {
       tc = new FileOutputStream(tf).getChannel();  // ← If next line throws, 
this leaks
       sc = new FileInputStream(sf).getChannel();
       sc.transferTo(0, sc.size(), tc);
   } finally {
       if (null != sc) { sc.close(); }
       if (null != tc) { tc.close(); }
   }
   ```
   
   **After (fixed):**
   ```java
   try (FileInputStream fis = new FileInputStream(sf);
        FileOutputStream fos = new FileOutputStream(tf);
        FileChannel sc = fis.getChannel();
        FileChannel tc = fos.getChannel()) {
       sc.transferTo(0, sc.size(), tc);
   }
   ```
   
   ## Why This Matters
   
   1. **Resource safety**: If `FileInputStream` constructor throws (e.g., 
source file is locked), the `FileOutputStream` created on the previous line 
would leak
   2. **Static analysis**: This pattern is flagged by SpotBugs, SonarQube, and 
other tools
   3. **Modern Java**: Try-with-resources is the idiomatic pattern since Java 7
   
   ## Testing
   
   The existing unit tests should pass as the functional behavior is unchanged. 
The fix only improves resource management.
   
   Fixes #10208


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

Reply via email to