Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPMD-288 Fixed NullPointerException #11

Closed
wants to merge 2 commits into from
Closed

Conversation

wcarmon
Copy link
Contributor

@wcarmon wcarmon commented May 10, 2019

this line throws NullPointerException when File.list() returns null

See related javadoc

For me this occurs on jar files like ~/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar

this line throws NPE when file.list is null

https://docs.oracle.com/javase/8/docs/api/java/io/File.html#list--

For me this occurs on jar files like `~/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar`
@adangel
Copy link
Member

adangel commented May 11, 2019

Thanks for the PR!
This sounds like a bug - could you please create a corresponding JIRA issue at https://issues.apache.org/jira/browse/MPMD ?

Also I've added the pull request template after you opened the PR. Can you check the points listed there?

@@ -769,7 +769,7 @@ private void configureTypeResolution( PMDConfiguration configuration ) throws Ma
for ( String path : projectCompileClasspath )
{
File pathFile = new File( path );
if ( !pathFile.exists() || pathFile.list().length == 0 )
if ( !pathFile.exists() || pathFile.list() == null || pathFile.list().length == 0 )

Choose a reason for hiding this comment

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

This fix is not correct.
Because the second invocation may return null.
You have to assign the result of list() to a local variable.

Copy link
Contributor Author

@wcarmon wcarmon May 11, 2019

Choose a reason for hiding this comment

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

I'm not following/understanding your feedback.

If the second clause is true, the third clause never executes.

If the second clause is false, the third clause won't throw an NPE.

Can you suggest code that is better for this? (Or describe why a local variable is better?)

Copy link

@eolivelli eolivelli May 11, 2019

Choose a reason for hiding this comment

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

If the first call to list() returns a non-null value the jvm will evaluate the third condition but a new call to list() is not guaranteed to be null. (In real world it won't be null I know)

Tools like spotbugs usually find this kind of potential problems.

I am just nitpicking.

If you call list() only once you won't have problems

Copy link
Contributor Author

@wcarmon wcarmon May 11, 2019

Choose a reason for hiding this comment

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

@eolivelli

  • yes, you are correct.
  • See updates in c64e1f6

@adangel

  • See https://issues.apache.org/jira/browse/MPMD-288
  • I ran mvn clean verify and mvn -Prun-its clean verify (both completed successfully)
  • Note that contrary to your directions, github warns that my commit message subject is too long (if I use the subject matching the jira ticket)

Copy link

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

Awesome.
Thank you.

@eolivelli
Copy link

In order to merge your oatcg please amend the commit message including the JIRA id, do a 'git history' and follow the style of other commit messages

@eolivelli
Copy link

Just squash to the commit with the jira id

@adangel adangel changed the title Fixed NullPointerException MPMD-288 Fixed NullPointerException May 16, 2019
adangel pushed a commit that referenced this pull request May 16, 2019
Closes #11

Fixed NullPointerException
this line throws NPE when file.list is null
https://docs.oracle.com/javase/8/docs/api/java/io/File.html#list--
For me this occurs on jar files like `~/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar`

Contributed by: Wil Carmon
if ( !pathFile.exists() || pathFile.list().length == 0 )
String[] children = pathFile.list();

if ( !pathFile.exists() || children == null || children.length == 0 )
Copy link
Member

Choose a reason for hiding this comment

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

@wcarmon I think, the logic should actually be

Suggested change
if ( !pathFile.exists() || children == null || children.length == 0 )
if ( !pathFile.exists() || ( children != null && children.length == 0 ) )

However, I'm wondering how a jar file can end up in this place. I expected here something like the paths "project-a/target/classes" or "project-a/target/generated-classes" or so.

You've mentioned, you get the null pointer when path is ~/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar. Do you use log4j-api as a normal dependency or is there something special in your project?

I've for now squashed and pushed your change to a new branch, let's see what Jenkins says: https://builds.apache.org/view/M-R/view/Maven/job/maven-box/job/maven-pmd-plugin/job/MPMD-288/

Copy link
Member

Choose a reason for hiding this comment

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

I've changed it to only consider non-existing files or empty directories - Jenkins is still happy: https://builds.apache.org/view/M-R/view/Maven/job/maven-box/job/maven-pmd-plugin/job/MPMD-288/2/

So, I'll merge this PR, thanks again!

adangel pushed a commit that referenced this pull request May 20, 2019
Closes #11

Fixed NullPointerException
this line throws NPE when file.list is null
https://docs.oracle.com/javase/8/docs/api/java/io/File.html#list--
For me this occurs on jar files like `~/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar`

Contributed by: Wil Carmon
@adangel adangel closed this in 6c1f0a2 May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants