Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6179

Sublist can be modified by parent list operations

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.8.9, 2.0.8, 2.1.3
    • Fix Version/s: 2.2.0-beta-2
    • Component/s: groovy-jdk
    • Labels:
      None

      Description

      List listA = [0, 1, 2] // .withDefault{ 42 }
      def listB = listA[1..2]
      listA[1] = 5
      assert listB == [5, 2]
      

      The last line shows the sublist being modified by the assignment to index 1 of the parent list.

      This is caused by the internal use of the ArrayList#subList method. The JavaDoc states that

      The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                asteingress Andre Steingress
                Reporter:
                asteingress Andre Steingress
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: