Skip to content

Commit b1e5d7a

Browse files
committed
fix: check for potential null futures in CancellationSupport.execute()
1 parent b57aa2b commit b1e5d7a

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/CancellationSupport.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2303 Red Hat Inc. and others.
2+
* Copyright (c) 2023 Red Hat Inc. and others.
33
* This program and the accompanying materials are made
44
* available under the terms of the Eclipse Public License 2.0
55
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -11,11 +11,14 @@
1111
*******************************************************************************/
1212
package org.eclipse.lsp4e.internal;
1313

14+
import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNullable;
15+
1416
import java.util.ArrayList;
1517
import java.util.List;
1618
import java.util.concurrent.CancellationException;
1719
import java.util.concurrent.CompletableFuture;
1820

21+
import org.eclipse.lsp4e.LanguageServerPlugin;
1922
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
2023

2124
/**
@@ -38,7 +41,14 @@ public CancellationSupport() {
3841
}
3942

4043
public <T> CompletableFuture<T> execute(CompletableFuture<T> future) {
41-
this.futuresToCancel.add(future);
44+
if (castNullable(future) == null) {
45+
// Although 'future' is expected to be non-null per contract, we perform a null
46+
// check to catch potential issues in asynchronous code and eagerly log an error
47+
// if null is encountered.
48+
LanguageServerPlugin.logError(new NullPointerException("[future] must not be null")); //$NON-NLS-1$
49+
} else {
50+
this.futuresToCancel.add(future);
51+
}
4252
return future;
4353
}
4454

@@ -58,12 +68,10 @@ public void cancel() {
5868
@Override
5969
public void checkCanceled() {
6070
// When LSP requests are called (ex : 'textDocument/completion') the LSP
61-
// response
62-
// items are used to compose some UI item (ex : LSP CompletionItem are translate
63-
// to Eclipse ICompletionProposal).
71+
// response items are used to compose some UI item (ex : LSP CompletionItem are
72+
// translate to Eclipse ICompletionProposal).
6473
// If the cancel occurs after the call of those LSP requests, the component
65-
// which uses the LSP responses
66-
// can call checkCanceled to stop the UI creation.
74+
// which uses the LSP responses can call checkCanceled to stop the UI creation.
6775
if (cancelled) {
6876
throw new CancellationException();
6977
}

0 commit comments

Comments
 (0)