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

TextComponent.select(int, int) works wrong with out of bounds parameters

    Details

    • Subcomponent:
    • Resolved In Build:
      1.2beta4
    • CPU:
      sparc
    • OS:
      solaris_2.5
    • Verification:
      Verified

      Description



      Name: dsC58869 Date: 03/10/98



      The method TextComponent.select(int, int) works wrong with inconsistent
      or out of bounds parameters.
      The JDK1.2 API Docs says about this method:
      *************
      public void select(int selectionStart,
                         int selectionEnd)

           Selects the text between the specified start and end positions.

           This method sets the start and end positions of the selected text, enforcing the
           restriction that the end position must be greater than or equal to the start
           position. The start position must be greater than zero, and the end position must
           be less that or equal to the length of the text component's text. If the caller
            ^^^^^^^^^^^^^
           supplies values that are inconsistent or out of bounds, the method enforces
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           these constraints silently, and without failure.
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      *************
      But in some test cases it makes wrong coercion of this values.
      As result the method TextComponent.getSelectedText() throws StringIndexOutOfBoundsException.

      ==== Here is the test demonstrating the bug ====
      import java.awt.*;

      public class TestTextComp001 {

          public static void main(String[] args){
           TextComponent tc = new TextField("ASDFASDFASDF");

      int[][] index = {
      {5, 7}, // 0 < selectionStart < selectionEnd < textLength
      {-50, 7}, // selectionStart < 0 < selectionEnd < textLength
      {-50, 50}, // selectionStart < 0 < textLength < selectionEnd
      {5, 50}, // 0 < selectionStart < textLength < selectionEnd
      {40, 50}, // 0 < textLength < selectionStart < selectionEnd
      {-50, -40}, // selectionStart < selectionEnd < 0 < textLength
      {7, 5}, // 0 < selectionEnd < selectionStart < textLength
      {7, -50}, // selectionEnd < 0 < selectionStart < textLength
      {50, -50}, // selectionEnd < 0 < textLength < selectionStart
      {50, 5}, // 0 < selectionEnd < textLength < selectionStart
      {50, 40}, // 0 < textLength < selectionEnd < selectionStart
      {-40, -50}, // selectionEnd < selectionStart < 0 < textLength
      };

      for (int i = 0;i < 12;i++) {
      try{
           tc.select(index[i][0], index[i][1]);
           String str1 = tc.getSelectedText();
           System.out.println("Test Case " + (i + 1) + " OKAY: " + str1);
           } catch (Exception e1) {
           System.out.println("Test Case " + (i + 1) + " failed: " + e1);
           }
           }

          }
      }

      ==== Here is the output of the test ====
      %java TestTextComp001
      Test Case 1 OKAY: SD
      Test Case 2 OKAY: ASDFASD
      Test Case 3 OKAY: ASDFASDFASDF
      Test Case 4 OKAY: SDFASDF
      Test Case 5 failed: java.lang.StringIndexOutOfBoundsException: String index out of range: 40
      Test Case 6 OKAY:
      Test Case 7 OKAY:
      Test Case 8 OKAY:
      Test Case 9 failed: java.lang.StringIndexOutOfBoundsException: String index out of range: 50
      Test Case 10 failed: java.lang.StringIndexOutOfBoundsException: String index out of range: 50
      Test Case 11 failed: java.lang.StringIndexOutOfBoundsException: String index out of range: 50
      Test Case 12 OKAY:
      ^C
      ======================================================================
       Justification:
      The method should work properly

      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ehawkessunw Eric Hawkes (Inactive)
                Reporter:
                dsilaev Dmitri Silaev
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: