Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1286

RandomStringUtils random method can overflow and return characters outside of specified range

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.5
    • 3.6
    • lang.*
    • None

    Description

      RandomStringUtils.random() can overflow and return characters that are outside the range specified by the start and end parameters. This is because it casts a random integer in the range [start,end) to a character, without checking if this will overflow.

      Example failing test case:

      @Test
      public void testCharOverflow() throws Exception {
          int start = 65535;
          int end = Integer.MAX_VALUE;
          
          @SuppressWarnings("serial")
          Random fixedRandom = new Random() {
              @Override
              public int nextInt(int n) {
                  // Prevents selection of 'start' as the character
                  return 1;
              }
          };
          
          String result = RandomStringUtils.random(1, start, end, false, false, null, fixedRandom);
          char c = result.charAt(0);
          assertTrue(c >= start && c < end);
      }
      

      Attachments

        Activity

          People

            dmjones500 Duncan Jones
            dmjones500 Duncan Jones
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: