Skip to content

Fix NullPointerException in SuffixMapping.getTargetFiles - #108

Open
JRVector9 wants to merge 1 commit into
apache:masterfrom
JRVector9:fix/suffixmapping-npe-null-source
Open

Fix NullPointerException in SuffixMapping.getTargetFiles#108
JRVector9 wants to merge 1 commit into
apache:masterfrom
JRVector9:fix/suffixmapping-npe-null-source

Conversation

@JRVector9

Copy link
Copy Markdown

SuffixMapping.getTargetFiles() calls source.endsWith(sourceSuffix) unconditionally. Both source and sourceSuffix can be null through the public constructor (SuffixMapping(String sourceSuffix, String targetSuffix) takes no null checks), and either one being null throws an NPE:

java.lang.NullPointerException: Cannot invoke "String.endsWith(String)" because "source" is null
	at org.apache.maven.shared.io.scan.mapping.SuffixMapping.getTargetFiles(SuffixMapping.java:59)

java.lang.NullPointerException: Cannot invoke "String.length()" because "suffix" is null
	at java.base/java.lang.String.endsWith(String.java:2331)
	at org.apache.maven.shared.io.scan.mapping.SuffixMapping.getTargetFiles(SuffixMapping.java:59)

Fixes #93

Changes

  • Guard the endsWith call so a null source or sourceSuffix is treated as a non-match (returns an empty target set), matching the existing behavior for a suffix mismatch.
  • Added two tests (shouldReturnNoTargetFilesWhenSourceSuffixIsNull, shouldReturnNoTargetFilesWhenSourceIsNull) that reproduce the NPE before the fix and pass after it.

mvn test -Dtest=SuffixMappingTest and mvn verify pass locally.

source.endsWith(sourceSuffix) throws NPE when either source or
sourceSuffix is null, both of which are reachable through the public
constructor. Guard the call and treat a null source or suffix as a
non-match, consistent with how other SourceMapping implementations
handle non-matching input.

Adds two tests that reproduce the NPE before the fix.

Fixes apache#93

@elharo elharo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the correct fix is to throw NullPointerExceptions from the constructor. What does a null suffix mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SuffixMapping: NPE from source.endsWith(null)

3 participants