Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-60721

Improper use of choice parameter throws NPE

XMLWordPrintable

      On jenkins with pipeline you can specify a jenkinsfile such as

      properties([    parameters([        choice(description: "lol")    ])])
      

       

      This jenkinsfile will run but now when you click the build with parameters button and attempt to build you will be greeted with

      java.lang.NullPointerException
      	at hudson.model.ParametersDefinitionProperty.getParameterDefinition(ParametersDefinitionProperty.java:205)
      	at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinitionProperty.java:150)
      	at jenkins.model.ParameterizedJobMixIn.doBuild(ParameterizedJobMixIn.java:212)
      	at jenkins.model.ParameterizedJobMixIn$ParameterizedJob.doBuild(ParameterizedJobMixIn.java:408)
      	at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)
      	at org.kohsuke.stapler.Function$MethodFunction.invoke(Function.java:396)
      	at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:408)
      	at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:212)
      	at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:145)
      	at org.kohsuke.stapler.MetaClass$11.doDispatch(MetaClass.java:535)
      	at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)
      	at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:747)
      

      This behaviour seems to affect all parameters

      Furthermore the github branch source plugin when scanning repositories will throw

      java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
      	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
      	at java.util.ArrayList.get(ArrayList.java:433)
      	at hudson.model.ChoiceParameterDefinition.getDefaultParameterValue(ChoiceParameterDefinition.java:133)
      	at hudson.model.ChoiceParameterDefinition.getDefaultParameterValue(ChoiceParameterDefinition.java:25)
      	at jenkins.model.ParameterizedJobMixIn.getDefaultParametersValues(ParameterizedJobMixIn.java:172)
      	at jenkins.model.ParameterizedJobMixIn.scheduleBuild2(ParameterizedJobMixIn.java:154)
      	at jenkins.model.ParameterizedJobMixIn.scheduleBuild2(ParameterizedJobMixIn.java:145)
      	at jenkins.branch.MultiBranchProject.scheduleBuild(MultiBranchProject.java:691)
      	at jenkins.branch.MultiBranchProject.access$1200(MultiBranchProject.java:125)
      	at jenkins.branch.MultiBranchProject$SCMHeadObserverImpl.doAutomaticBuilds(MultiBranchProject.java:2213)
      	at jenkins.branch.MultiBranchProject$SCMHeadObserverImpl.observeExisting(MultiBranchProject.java:2038)
      	at jenkins.branch.MultiBranchProject$SCMHeadObserverImpl.observe(MultiBranchProject.java:1998)
      	at jenkins.scm.api.trait.SCMSourceRequest.process(SCMSourceRequest.java:357)
      	at jenkins.scm.api.trait.SCMSourceRequest.process(SCMSourceRequest.java:249)
      	at org.jenkinsci.plugins.github_branch_source.GitHubSCMSource.retrieve(GitHubSCMSource.java:919)
      

      This all seems to stem from the fact that choices is allowed to be an empty list with a null name but multiple parts assume that it has at least one item and a valid name.

      See:
      https://github.com/jenkinsci/jenkins/blob/ad1ca7101b9b180dc677eef914b1cbd8208d00c8/core/src/main/java/hudson/model/ParametersDefinitionProperty.java#L205
      https://github.com/jenkinsci/jenkins/blob/ad1ca7101b9b180dc677eef914b1cbd8208d00c8/core/src/main/java/hudson/model/ChoiceParameterDefinition.java#L133

            Unassigned Unassigned
            raihaan Raihaan Shouhell
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: