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

Improper use of choice parameter throws NPE

    XMLWordPrintable

Details

    Description

      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

      Attachments

        Activity

          raihaan Raihaan Shouhell created issue -
          raihaan Raihaan Shouhell made changes -
          Field Original Value New Value
          Description On jenkins with pipeline you can specify a jenkinsfile such as
          {code:java}
          properties([ parameters([ choice(description: "lol") ])])
          {code}
           

          This jenkinsfile will run but now when you click the build with parameters button and attempt to build you will be greeted with
          {code:java}
          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)
          {code}
          Furthermore the github branch source plugin when scanning repositories will throw
          {code:java}
          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)
          {code}

          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
          On jenkins with pipeline you can specify a jenkinsfile such as
          {code:java}
          properties([ parameters([ choice(description: "lol") ])])
          {code}
           

          This jenkinsfile will run but now when you click the build with parameters button and attempt to build you will be greeted with
          {code:java}
          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)
          {code}
          This behaviour seems to affect all parameters


          Furthermore the github branch source plugin when scanning repositories will throw
          {code:java}
          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)
          {code}

          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
          oleg_nenashev Oleg Nenashev added a comment -

          This part of the Jenkins API has not been annotated yet, but from what I see the code does not expect null parameter names. My suggestion would be to...

          • Annotate the Core API to be explicit about the value expectation.
          • Maybe: Add readResolve() method to replace nulls with something like "UNDEFINED"
          • Fix the bug in the Choice plugin
          oleg_nenashev Oleg Nenashev added a comment - This part of the Jenkins API has not been annotated yet, but from what I see the code does not expect null parameter names. My suggestion would be to... Annotate the Core API to be explicit about the value expectation. Maybe: Add readResolve() method to replace nulls with something like "UNDEFINED" Fix the bug in the Choice plugin

          Not sure how to link the PR's to jira so pasting it here for reference.

          raihaan Raihaan Shouhell added a comment - Not sure how to link the PR's to jira so pasting it here for reference.
          raihaan Raihaan Shouhell made changes -
          Released As https://jenkins.io/changelog/#v2.220
          Resolution Fixed [ 1 ]
          Status Open [ 1 ] Resolved [ 5 ]

          People

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

            Dates

              Created:
              Updated:
              Resolved: