GeezerVison is showing github.com — [java] HardCodedCryptoKey: NPE when constants from parent class are used by zbynek · Pull Request #6191 · pmd/pmd
Conversation
zbynek
commented
Oct 30, 2025
•
edited by adangel
Loading
Uh oh!
There was an error while loading. Please reload this page.
Describe the PR
Rule: HardCodedCryptoKey
Adds a null check in case the accessed field is not defined in the current class. When the null check fails, check if the field is a compile time constant.
The changed code is shared with InsecureCryptoIv, the fix applies to both rules.
For reference, the fixed NPE stacktrace
[WARNING] Exception applying rule HardCodedCryptoKey on file /home/mag/project/civitas-ng/civitas.externalcrypto/src/test/java/civitas/crypto/CryptoBaseTest.java, continuing with next rule
org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.NullPointerException: Cannot invoke "net.sourceforge.pmd.lang.java.ast.ASTVariableId.getInitializer()" because "varDecl" is null
Exception Context:
[1:Rule applied on node=!debug only! [ConstructorCall:82:28]new SecretKeySpec(SOMESTRING.getBytes(), SHARED_KEY_ALG)]
---------------------------------
at net.sourceforge.pmd.util.AssertionUtil.contexted (AssertionUtil.java:257)
at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex (RuleApplicator.java:79)
at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply (RuleApplicator.java:57)
at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply (RuleSets.java:183)
at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource (PmdRunnable.java:140)
at net.sourceforge.pmd.lang.impl.PmdRunnable.run (PmdRunnable.java:80)
at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:572)
at java.util.concurrent.FutureTask.run (FutureTask.java:317)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1144)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:642)
at java.lang.Thread.run (Thread.java:1583)
Caused by: java.lang.NullPointerException: Cannot invoke "net.sourceforge.pmd.lang.java.ast.ASTVariableId.getInitializer()" because "varDecl" is null
at net.sourceforge.pmd.lang.java.rule.security.AbstractHardCodedConstructorArgsVisitor.validateProperKeyArgument (AbstractHardCodedConstructorArgsVisitor.java:68)
at net.sourceforge.pmd.lang.java.rule.security.AbstractHardCodedConstructorArgsVisitor.visit (AbstractHardCodedConstructorArgsVisitor.java:35)
at net.sourceforge.pmd.lang.java.rule.security.HardCodedCryptoKeyRule.visit (HardCodedCryptoKeyRule.java:14)
at net.sourceforge.pmd.lang.java.ast.ASTConstructorCall.acceptVisitor (ASTConstructorCall.java:34)
at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.apply (AbstractJavaRule.java:30)
at net.sourceforge.pmd.lang.rule.RuleReference.apply (RuleReference.java:413)
at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex (RuleApplicator.java:77)
at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply (RuleApplicator.java:57)
at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply (RuleSets.java:183)
at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource (PmdRunnable.java:140)
at net.sourceforge.pmd.lang.impl.PmdRunnable.run (PmdRunnable.java:80)
at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:572)
at java.util.concurrent.FutureTask.run (FutureTask.java:317)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1144)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:642)
at java.lang.Thread.run (Thread.java:1583)
Related issues
reported in discussion #6186
Ready?
- Added unit tests for fixed bug/feature
- Passing all unit tests
- Complete build
./mvnw clean verify passes (checked automatically by github actions)
- Added (in-code) documentation (if needed)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
adangel
changed the title
[java] HardCodedCryptoKey: report constants from parent class
[java] HardCodedCryptoKey: NPE when constants from parent class are used
Oct 30, 2025
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Labels
a:bug
PMD crashes or fails to analyse a file.
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.