Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8216422

ArrayIndexOutOfBoundsException on some arrays when implementing merge sort.

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      A DESCRIPTION OF THE PROBLEM :
        hello,guys. I found a bug that when I test my code.
         case one: int [] arr={3, 2, 4,5, 5},this don't cause ArrayIndexOutOfBoundsException.
         case tow: int [] arr={3, 2, 4,4, 5},when running below code this will cause ArrayIndexOutOfBoundsException.
         case tow: int [] arr={3, 2, 4,3, 5},when running below code this will cause ArrayIndexOutOfBoundsException.
         case three: int [] arr={3, 2, 4,2, 5},when running below code this will cause ArrayIndexOutOfBoundsException.
         .........

        I think there are many cases will cause ArrayIndexOutOfBoundsException.

      REGRESSION : Last worked in version 11.0.1

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
         case one: int [] arr={3, 2, 4,5, 5},this don't cause ArrayIndexOutOfBoundsException.
         case tow: int [] arr={3, 2, 4,4, 5},when running below code this will cause ArrayIndexOutOfBoundsException.
         case tow: int [] arr={3, 2, 4,3, 5},when running below code this will cause ArrayIndexOutOfBoundsException.
         case three: int [] arr={3, 2, 4,2, 5},when running below code this will cause ArrayIndexOutOfBoundsException.
         .........
        case three: int [] arr={3, 2, 4,Integer.MIN_VALUE, 5},when running below code this will cause ArrayIndexOutOfBoundsException.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      There should not comes with a ArrayIndexOutOfBoundsException at least.
      ACTUAL -
      There comes a ArrayIndexOutOfBoundsException.

      ---------- BEGIN SOURCE ----------
      public class A {
         
          public void mergeSort(int[] arr,int left,int right,int[] tmp){
              if(left==right)return;
              int m=left+(right-left)/2;
              mergeSort(arr,left,m,tmp);
              mergeSort(arr,m+1,right,tmp);
              merge(arr,left,m,right,tmp);
          }

          public void merge(int[] arr,int left,int mid,int right,int[] tmp){
              int i=left,j=mid+1,k=0;
              while(i<j&&j<=right){
                if(arr[i]<arr[j]){
                  tmp[k++]=arr[i++];
                }else{
                  tmp[k++]=arr[j++];
                }
              }
              while(i<j){
                tmp[k++]=arr[i++];
              }
              while(j<=right){
                tmp[k++]=arr[j++];
              }
              k=0;
              while(left<=right){
                arr[left++]=tmp[k++];
              }
          }

          public static void main(String[] args){
               //case one:
              int [] arr={3, 2, 4,5, 5};
              //case tow:
              //int [] arr={3, 2, 4,3, 5};

              //case three:
              //int [] arr={3, 2, 4,2, 5};

             //....
             
              int left=0;
              int right=arr.length-1;
              new A().mergeSort(arr,left,right,new int[arr.length]);
              System.out.println(Arrays.toString(arr));
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I think it is jdk implemention problem,this can be fixed with bottom layer code.

      FREQUENCY : often


        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: