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

Replacing Arrays.asList with List.of can introduce NullPointerException #642

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Object sanitizeArrayAndRecordMutation(@NonNull Object[] objects, @CheckForNull E
this.isUnmodifiedBySanitization = false;
return NotStoredReason.OVERSIZE_VALUE;
}
List inputList = List.of(objects);
List<Object> inputList = Arrays.asList(objects);
jtnord marked this conversation as resolved.
Show resolved Hide resolved
Object sanitized = sanitizeListAndRecordMutation(inputList, variables);
if (sanitized == inputList) { // Works because if not mutated, we return original input instance
return objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static <E> Iterator<E> iterator(Set<E> set) {

public static <E> Iterator<E> iterator(E[] array) {
// TODO as above
return new Itr<>(List.of(array));
return new Itr<>(Arrays.asList(array));
}

public static <E> Iterator<E> iterator(Deque deque) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,18 @@ public class IteratorHackTest {
r.assertBuildStatusSuccess(r.waitForCompletion(b));
});
}

@Issue("https://github.com/jenkinsci/workflow-cps-plugin/pull/627#issuecomment-1352869571")
@Test public void arrayIterator() throws Exception {
rr.then(r -> {
WorkflowJob p = r.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition(
"Object[] arr = [1, null, 2]\n" +
"for (def elt : arr) {\n" +
" echo(/iterating on $elt/)\n" +
"}\n", false));
rr.j.assertLogContains("iterating on null", rr.j.buildAndAssertSuccess(p));
Copy link
Member Author

Choose a reason for hiding this comment

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

reproduced as

java.lang.AssertionError: 
unexpected build status; build log was:
------
Started
[Pipeline] Start of Pipeline
[Pipeline] End of Pipeline
java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:221)
	at java.base/java.util.ImmutableCollections$ListN.<init>(ImmutableCollections.java:432)
	at java.base/java.util.List.of(List.java:1040)
	at org.jenkinsci.plugins.workflow.cps.persistence.IteratorHack.iterator(IteratorHack.java:106)
	at …
	at WorkflowScript.run(WorkflowScript:2)
	at …
Finished: FAILURE

------

Expected: is <SUCCESS>
     but: was <FAILURE>
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.jvnet.hudson.test.JenkinsRule.assertBuildStatus(JenkinsRule.java:1438)
	at org.jvnet.hudson.test.JenkinsRule.assertBuildStatus(JenkinsRule.java:1444)
	at org.jvnet.hudson.test.JenkinsRule.buildAndAssertStatus(JenkinsRule.java:1491)
	at org.jvnet.hudson.test.JenkinsRule.buildAndAssertSuccess(JenkinsRule.java:1477)
	at org.jenkinsci.plugins.workflow.cps.persistence.IteratorHackTest.lambda$arrayIterator$4(IteratorHackTest.java:269)

});
}

}