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

Protection against NullPointerException #65

Closed
wants to merge 1 commit into from

Conversation

@amuniz
Copy link

commented Aug 29, 2016

canConvert calls accept null as parameter but most of the converters are not checking for it, so it leads to NullPointerExceptions.

I've not been able to diagnose what is the root cause of the NPE I'm seeing (under what conditions canConvert(null) is called on a converter other than NullConverter, but this defensive check suppress it.

@joehni

This comment has been minimized.

Copy link
Member

commented Sep 27, 2016

Hi, thanks for the request, but this will not work. XStream does not prevent you from adding an own converter handling null. Your PR would ignore any custom converter.

@amuniz

This comment has been minimized.

Copy link
Author

commented Sep 29, 2016

It will work if any custom converter for null extends NullConverter, which seems reasonable to me.

@amuniz

This comment has been minimized.

Copy link
Author

commented Sep 29, 2016

The problem is that there are a lot of converters that does not check for null on canConvert calls, so it leads to NPE.

An alternative solution could be:

  1. Mark canConvert(Class<?>) parameter as @CheckForNull and
  2. try-catch on NullPointerException here and skip that converter (as its canConvert method could not determine if it can convert or not.

Looks better?

@joehni

This comment has been minimized.

Copy link
Member

commented Sep 29, 2016

We can protect canConvert against RuntimeExceptions...

@joehni joehni added this to the 1.4.x milestone Sep 29, 2016
@joehni joehni closed this in a4cb0af May 4, 2017
joehni added a commit that referenced this pull request May 4, 2017
@joehni joehni modified the milestones: 1.4.x, 1.4.10 Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.