From c0eba42f2945593c4b7c1ea41d51be385dd61809 Mon Sep 17 00:00:00 2001 From: Mykhailo Palahuta Date: Tue, 28 Jul 2020 18:57:29 +0300 Subject: [PATCH 1/2] [java] InvalidLogMessageFormatRule throws IndexOutOfBoundsException when only logging exception message --- .../InvalidLogMessageFormatRule.java | 9 +++++++-- .../xml/InvalidLogMessageFormat.xml | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java index ad45ebb133..64e16d7362 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java @@ -89,9 +89,14 @@ public class InvalidLogMessageFormatRule extends AbstractJavaRule { final List argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class) .getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class); - // ignore the first argument if it is a known non-string value, e.g. a slf4j-Marker if (argumentList.get(0).getType() != null && !argumentList.get(0).getType().equals(String.class)) { - argumentList.remove(0); + if (argumentList.size() == 1) { + // no need to check for message params in case no string and no params found + return data; + } else { + // ignore the first argument if it is a known non-string value, e.g. a slf4j-Marker + argumentList.remove(0); + } } // remove the message parameter diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml index 54161999c8..5f3665dd87 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml @@ -865,6 +865,25 @@ class InvalidLogMessageFormatTest { private static void foo(Consumer consumer) { consumer.accept("bar"); } +} + ]]> + + + + #2431 IndexOutOfBoundsException when only logging exception message + 0 + From ab044f2c3202a34c7b8fb71581e2b6dc2875dda6 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 31 Jul 2020 10:52:10 +0200 Subject: [PATCH 2/2] [doc] Update release notes, fixes #2431, refs #2679 --- docs/pages/release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index c095e78be5..792672e38f 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -34,6 +34,7 @@ This is a {{ site.pmd.release_type }} release. * [#2174](https://github.com/pmd/pmd/issues/2174): \[java] LawOfDemeter: False positive with 'this' pointer * [#2189](https://github.com/pmd/pmd/issues/2189): \[java] LawOfDemeter: False positive when casting to derived class * java-errorprone + * [#2431](https://github.com/pmd/pmd/issues/2431): \[java] InvalidLogMessageFormatRule throws IndexOutOfBoundsException when only logging exception message * [#2578](https://github.com/pmd/pmd/issues/2578): \[java] AvoidCallingFinalize detects some false positives * [#2634](https://github.com/pmd/pmd/issues/2634): \[java] NullPointerException in rule ProperCloneImplementation * java-performance @@ -60,6 +61,7 @@ This is a {{ site.pmd.release_type }} release. * [#2621](https://github.com/pmd/pmd/pull/2621): \[visualforce] add new safe resource for VfUnescapeEl - [Peter Chittum](https://github.com/pchittum) * [#2640](https://github.com/pmd/pmd/pull/2640): \[java] NullPointerException in rule ProperCloneImplementation - [Mykhailo Palahuta](https://github.com/Drofff) * [#2643](https://github.com/pmd/pmd/pull/2643): \[java] AvoidCallingFinalize detects some false positives (2578) - [Mykhailo Palahuta](https://github.com/Drofff) +* [#2679](https://github.com/pmd/pmd/pull/2679): \[java] InvalidLogMessageFormatRule throws IndexOutOfBoundsException when only logging exception message - [Mykhailo Palahuta](https://github.com/Drofff) {% endtocmaker %}