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

Fix missing null checks in DataProviderRegistrator #3522

Merged
merged 1 commit into from
Oct 9, 2021

Conversation

Lignium
Copy link
Contributor

@Lignium Lignium commented Sep 4, 2021

Fixes a NullPointerException when trying to write read-only keys. For example, when trying to merge data from one block state to another, an exception occurs when trying to merge in my case Key.IS_PASSABLE.

Example code:

@Listener
public void onStartedServer(StartedEngineEvent<Server> event) {
    Objects.requireNonNull(event, "event");

    BlockState firstState = BlockTypes.OAK_LOG.get().defaultState().with(Keys.AXIS, Axis.Z).get();
    BlockState secondState = firstState.with(Keys.AXIS, Axis.Y).get();

    System.out.println(firstState);
    System.out.println(secondState);
    System.out.println(firstState.mergeWith(secondState)); // NPE
}

And exception:

java.lang.NullPointerException: null
	at org.spongepowered.common.data.provider.DataProviderRegistrator$ImmutableRegistrationBase$1.set(DataProviderRegistrator.java:501) ~[DataProviderRegistrator$ImmutableRegistrationBase$1.class:1.16.5-8.0.0-RC0]
	at org.spongepowered.common.data.provider.GenericImmutableDataProviderBase.with(GenericImmutableDataProviderBase.java:138) ~[GenericImmutableDataProviderBase.class:1.16.5-8.0.0-RC0]
	at org.spongepowered.common.data.holder.SpongeImmutableDataHolder.with(SpongeImmutableDataHolder.java:58) ~[SpongeImmutableDataHolder.class:1.16.5-8.0.0-RC0]
	at org.spongepowered.common.data.holder.SpongeImmutableDataHolder.mergeWith(SpongeImmutableDataHolder.java:72) ~[SpongeImmutableDataHolder.class:1.16.5-8.0.0-RC0]
	at org.spongepowered.api.data.DataHolder$Immutable.mergeWith(DataHolder.java:448) ~[DataHolder$Immutable.class:1.16.5-8.0.0-RC0]
	...

@@ -338,6 +338,9 @@ public R supports(final Function<H, Boolean> supports) {

@Override
protected Optional<E> getFrom(final H dataHolder) {
if (registration.get == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any time where this is actually going to be null? I can't imagine there is a use case for a write-only key within Sponge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, it is, but who knows, maybe someday it will be. Given that the get field is declared with @Nullable annotation, I think the check should be.

@dualspiral dualspiral merged commit dc76a33 into SpongePowered:api-8 Oct 9, 2021
@Lignium Lignium deleted the fix-missing-null-checks branch October 9, 2021 16:19
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.

2 participants