From 693ffdb46ad6e6fd523976629892c1a127f3f35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Tue, 16 Apr 2024 17:52:11 -0300 Subject: [PATCH 1/2] Avoid NPE on CloseResourceRule - Fixes #4751 --- .../java/rule/errorprone/CloseResourceRule.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java index ac75cb0f90..ab9829d6cd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloseResourceRule.java @@ -51,6 +51,7 @@ import net.sourceforge.pmd.lang.java.ast.internal.PrettyPrintingUtil; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; import net.sourceforge.pmd.lang.java.rule.internal.JavaRuleUtil; import net.sourceforge.pmd.lang.java.symbols.JTypeDeclSymbol; +import net.sourceforge.pmd.lang.java.symbols.JVariableSymbol; import net.sourceforge.pmd.lang.java.types.InvocationMatcher; import net.sourceforge.pmd.lang.java.types.TypeTestUtil; import net.sourceforge.pmd.properties.PropertyDescriptor; @@ -287,12 +288,15 @@ public class CloseResourceRule extends AbstractJavaRule { private boolean isWrappingResourceSpecifiedInTry(ASTVariableId var) { ASTVariableAccess wrappedVarName = getWrappedVariableName(var); if (wrappedVarName != null) { - ASTVariableId referencedVar = wrappedVarName.getReferencedSym().tryGetNode(); - if (referencedVar != null) { - List tryContainers = referencedVar.ancestors(ASTTryStatement.class).toList(); - for (ASTTryStatement tryContainer : tryContainers) { - if (isTryWithResourceSpecifyingVariable(tryContainer, referencedVar)) { - return true; + JVariableSymbol referencedSym = wrappedVarName.getReferencedSym(); + if (referencedSym != null) { + ASTVariableId referencedVar = referencedSym.tryGetNode(); + if (referencedVar != null) { + List tryContainers = referencedVar.ancestors(ASTTryStatement.class).toList(); + for (ASTTryStatement tryContainer : tryContainers) { + if (isTryWithResourceSpecifyingVariable(tryContainer, referencedVar)) { + return true; + } } } } From 5b5ac5c07d43bb04fc27eb3e868e15d6bce6d79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Tue, 16 Apr 2024 17:53:10 -0300 Subject: [PATCH 2/2] Update changelog, refs #4751 --- docs/pages/release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 2df014843f..04f163838a 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -51,6 +51,7 @@ This is a {{ site.pmd.release_type }} release. * [#4873](https://github.com/pmd/pmd/issues/4873): \[java] AvoidCatchingGenericException: Can no longer suppress on the exception itself * java-errorprone * [#2056](https://github.com/pmd/pmd/issues/2056): \[java] CloseResource false-positive with URLClassLoader in cast expression + * [#4751](https://github.com/pmd/pmd/issues/4751): \[java] PMD crashes when analyzing CloseResource Rule * [#4928](https://github.com/pmd/pmd/issues/4928): \[java] EmptyCatchBlock false negative when allowCommentedBlocks=true * java-performance * [#3845](https://github.com/pmd/pmd/issues/3845): \[java] InsufficientStringBufferDeclaration should consider literal expression