This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new af0a0256222 No String.format when varargs is empty to avoid possible 
formating error (#32084)
af0a0256222 is described below

commit af0a0256222b05c26a384b2829b03d82b69023d8
Author: Hongsheng Zhong <[email protected]>
AuthorDate: Fri Jul 12 20:06:56 2024 +0800

    No String.format when varargs is empty to avoid possible formating error 
(#32084)
---
 .../external/sql/ShardingSphereSQLException.java   | 14 ++++++--
 .../internal/ShardingSphereInternalException.java  | 12 ++++++-
 .../core/sql/ShardingSphereSQLExceptionTest.java   | 41 ++++++++++++++++++++++
 3 files changed, 64 insertions(+), 3 deletions(-)

diff --git 
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
 
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
index c67d55857dd..8e7d3c64a62 100644
--- 
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
+++ 
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
@@ -43,11 +43,11 @@ public abstract class ShardingSphereSQLException extends 
ShardingSphereExternalE
     private final Exception cause;
     
     protected ShardingSphereSQLException(final SQLState sqlState, final int 
typeOffset, final int errorCode, final String reason, final Object... 
messageArgs) {
-        this(sqlState.getValue(), typeOffset, errorCode, null == reason ? null 
: String.format(reason, formatMessageArguments(messageArgs)), null);
+        this(sqlState.getValue(), typeOffset, errorCode, formatMessage(reason, 
messageArgs), null);
     }
     
     protected ShardingSphereSQLException(final SQLState sqlState, final int 
typeOffset, final int errorCode, final Exception cause, final String reason, 
final Object... messageArgs) {
-        this(sqlState.getValue(), typeOffset, errorCode, null == reason ? null 
: String.format(reason, formatMessageArguments(messageArgs)), cause);
+        this(sqlState.getValue(), typeOffset, errorCode, formatMessage(reason, 
messageArgs), cause);
     }
     
     protected ShardingSphereSQLException(final String sqlState, final int 
typeOffset, final int errorCode, final String reason, final Exception cause) {
@@ -60,6 +60,16 @@ public abstract class ShardingSphereSQLException extends 
ShardingSphereExternalE
         this.cause = cause;
     }
     
+    private static String formatMessage(final String reason, final Object[] 
messageArgs) {
+        if (null == reason) {
+            return null;
+        }
+        if (0 == messageArgs.length) {
+            return reason;
+        }
+        return String.format(reason, formatMessageArguments(messageArgs));
+    }
+    
     private static Object[] formatMessageArguments(final Object... 
messageArgs) {
         return Arrays.stream(messageArgs)
                 .map(each -> each instanceof Collection ? ((Collection<?>) 
each).stream().map(Object::toString).collect(Collectors.joining(", ")) : 
each).toArray(Object[]::new);
diff --git 
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/internal/ShardingSphereInternalException.java
 
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/internal/ShardingSphereInternalException.java
index 23d595a9827..c7ee3ecc555 100644
--- 
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/internal/ShardingSphereInternalException.java
+++ 
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/internal/ShardingSphereInternalException.java
@@ -29,7 +29,7 @@ public abstract class ShardingSphereInternalException extends 
Exception {
     private static final long serialVersionUID = -8238061892944243621L;
     
     protected ShardingSphereInternalException(final String errorMessage, final 
Object... args) {
-        super(String.format(errorMessage, args));
+        super(formatMessage(errorMessage, args));
     }
     
     protected ShardingSphereInternalException(final Exception cause) {
@@ -39,4 +39,14 @@ public abstract class ShardingSphereInternalException 
extends Exception {
     protected ShardingSphereInternalException(final String message, final 
Exception cause) {
         super(message, cause);
     }
+    
+    private static String formatMessage(final String reason, final Object[] 
messageArgs) {
+        if (null == reason) {
+            return null;
+        }
+        if (0 == messageArgs.length) {
+            return reason;
+        }
+        return String.format(reason, messageArgs);
+    }
 }
diff --git 
a/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/sql/ShardingSphereSQLExceptionTest.java
 
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/sql/ShardingSphereSQLExceptionTest.java
new file mode 100644
index 00000000000..029708edae9
--- /dev/null
+++ 
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/sql/ShardingSphereSQLExceptionTest.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.exception.core.sql;
+
+import 
org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException;
+import 
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+class ShardingSphereSQLExceptionTest {
+    
+    @Test
+    void assertNoStringFormatErrorWhenNoArgs() {
+        assertNotNull(new MalformedFormatMessageSQLException().getMessage());
+    }
+    
+    private static class MalformedFormatMessageSQLException extends 
ShardingSphereSQLException {
+        
+        private static final long serialVersionUID = 139616805450096292L;
+        
+        MalformedFormatMessageSQLException() {
+            super(XOpenSQLState.GENERAL_ERROR, 0, 1, "Fixture %2");
+        }
+    }
+}

Reply via email to