Copilot commented on code in PR #15874:
URL: https://github.com/apache/dubbo/pull/15874#discussion_r2730186388


##########
dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java:
##########
@@ -367,12 +367,11 @@ private String generatePrivatePemKey(KeyPair keyPair) 
throws IOException {
      */
     private String generatePemKey(String type, byte[] content) throws 
IOException {
         PemObject pemObject = new PemObject(type, content);
-        StringWriter str = new StringWriter();
-        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str);
-        jcaPEMWriter.writeObject(pemObject);
-        jcaPEMWriter.close();
-        str.close();
-        return str.toString();
+        try (StringWriter str = new StringWriter();
+                JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str)) {
+            jcaPEMWriter.writeObject(pemObject);
+            return str.toString();
+        }

Review Comment:
   In try-with-resources, resources are closed *after* the return expression is 
evaluated. Returning `str.toString()` inside the try block means the string is 
captured before `jcaPEMWriter.close()` runs; if `JcaPEMWriter` buffers/flushes 
on close, this can produce truncated PEM output (and it also changes behavior 
vs the previous implementation that closed before reading the string). Please 
move the `return` outside the try-with-resources (e.g., keep `StringWriter` 
outside and only TWR `JcaPEMWriter`), or explicitly `flush()` before 
constructing the return value.
   ```suggestion
           StringWriter str = new StringWriter();
           try (JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str)) {
               jcaPEMWriter.writeObject(pemObject);
           }
           return str.toString();
   ```



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