Skip to content

Commit 843c72a

Browse files
committed
JENKINS-70066: Fix null check of parameters
The `@DataBoundConstructor` for `ScriptlerBuilder` takes a list of parameters for the given script. It was annotated as `@NotNull`, but `Descriptor.bindJSON` could occasionally pass `null` for the parameters list. Fix this by annotating the parameter as `@CheckForNull` and performing the appropriate null checks. (cherry picked from commit adce718)
1 parent 04064ad commit 843c72a

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/main/java/org/jenkinsci/plugins/scriptler/builder/ScriptlerBuilder.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,24 @@ public class ScriptlerBuilder extends Builder implements Serializable {
7777
/**
7878
* @deprecated as of 3.5; use {@link #ScriptlerBuilder(String, String, boolean, List)}
7979
*/
80-
@Deprecated
81-
public ScriptlerBuilder(String builderId, String scriptId, boolean propagateParams, Parameter[] parameters) {
82-
this(builderId, scriptId, propagateParams, Arrays.asList(Objects.requireNonNull(parameters)));
80+
@Deprecated(since = "3.5")
81+
public ScriptlerBuilder(
82+
@CheckForNull String builderId,
83+
@CheckForNull String scriptId,
84+
boolean propagateParams,
85+
@CheckForNull Parameter[] parameters) {
86+
this(builderId, scriptId, propagateParams, parameters == null ? List.of() : List.of(parameters));
8387
}
8488

8589
@DataBoundConstructor
86-
public ScriptlerBuilder(String builderId, String scriptId, boolean propagateParams, @NonNull List<Parameter> parameters) {
90+
public ScriptlerBuilder(
91+
@CheckForNull String builderId,
92+
@CheckForNull String scriptId,
93+
boolean propagateParams,
94+
@CheckForNull List<Parameter> parameters) {
8795
this.builderId = builderId;
8896
this.scriptId = scriptId;
89-
this.parameters = new ArrayList<>(parameters);
97+
this.parameters = parameters == null ? List.of() : List.copyOf(parameters);
9098
this.propagateParams = propagateParams;
9199
}
92100

@@ -180,7 +188,7 @@ public Parameter[] getParameters() {
180188

181189
@NonNull
182190
public List<Parameter> getParametersList() {
183-
return Collections.unmodifiableList(parameters);
191+
return parameters;
184192
}
185193

186194
public String getBuilderId() {
@@ -227,8 +235,9 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
227235

228236
try {
229237

230-
// expand the parameters before passing these to the execution, this is to allow any token macro to resolve parameter values
231-
List<Parameter> expandedParams = new LinkedList<>();
238+
// expand the parameters before passing these to the execution, this is to allow any token macro to resolve
239+
// parameter values
240+
List<Parameter> expandedParams = new ArrayList<>();
232241

233242
if (propagateParams) {
234243
final ParametersAction paramsAction = build.getAction(ParametersAction.class);
@@ -365,17 +374,16 @@ public ScriptlerBuilder newInstance(StaplerRequest req, JSONObject formData) {
365374
}
366375

367376
if (builder == null) {
368-
builder = new ScriptlerBuilder(builderId, null, false, Collections.emptyList());
377+
builder = new ScriptlerBuilder(builderId, null, false, List.of());
369378
}
370379

371380
return builder.recreateBuilderWithBuilderIdIfRequired();
372381
}
373382

374383
public List<Script> getScripts() {
375384
// TODO currently only script for RUN_SCRIPT permissions are returned?
376-
Set<Script> scripts = getConfig().getScripts();
377385
List<Script> scriptsForBuilder = new ArrayList<>();
378-
for (Script script : scripts) {
386+
for (Script script : getConfig().getScripts()) {
379387
if (script.nonAdministerUsing) {
380388
scriptsForBuilder.add(script);
381389
}

src/main/java/org/jenkinsci/plugins/scriptler/config/Script.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.jenkinsci.plugins.scriptler.config;
2525

2626
import edu.umd.cs.findbugs.annotations.NonNull;
27+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2728

2829
import java.util.*;
2930

@@ -118,6 +119,7 @@ public void setAvailable(boolean available) {
118119
this.available = available;
119120
}
120121

122+
@SuppressFBWarnings("PA_PUBLIC_PRIMITIVE_ATTRIBUTE")
121123
public void setScript(String script) {
122124
this.script = script;
123125
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
java.util.ImmutableCollections$ListN
2+
java.util.ImmutableCollections$List12

0 commit comments

Comments
 (0)