String_overflow.java 2.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
class String_overflow
{
  static void getChars()
  {
    String source = "abcdefg";
    char[] dest = new char [3];

    try
      {
	source.getChars (0, 5,	// Source
			 dest, (1<<31) - 1);
	Fail ("getChars", "Should not have succeeded");
      }
    catch (Throwable e)
      {
	ExpectArrayIndex ("getChars", e);
      }
  }

    /* How do I stop a compiler warning causing a test to fail?
  static void getBytes()
  {
    String source = "abcdefg";
    byte[] dest = new byte[3];

    try
      {
	source.getBytes (0, 5, dest, (1<<31) - 1);
	Fail ("getBytes", "Should not have succeeded");
      }
    catch (Throwable e)
      {
	ExpectArrayIndex ("getBytes", e);
      }
  }
    */

  static void regionMatches()
  {
    if ("abcdefg".regionMatches (4, "abcdefg", 4, -1))
      {
	Fail ("regionMatches", "Should not return true");
      }

    try
      {
	if ("abcdefg".regionMatches (4, "abcdefg", 4, (1<<31)-1))
	  {
	    Fail ("regionMatches (2nd)", "Should not return true");
	  }
      }
    catch (Throwable e)
      {
	Fail ("regionMatches (2nd)", e);
      }
  }

  static void regionMatchesCase()
  {
    if ("abcdefg".regionMatches (true, 4, "abcdefg", 4, -1))
      {
	Fail ("regionMatchesCase", "Should not return true");
      }

    try
      {
	if ("abcdefg".regionMatches (true, 4, "abcdefg", 4, (1<<31)-1))
	  {
	    Fail ("regionMatchesCase (2nd)", "Should not return true");
	  }
      }
    catch (Throwable e)
      {
	Fail ("regionMatchesCase (2nd)", e);
      }
  }

  static void startsWith()
  {
    // We make the arg pretty big to try and cause a segfault.
    String s = new String ("abcdef");
    StringBuffer b = new StringBuffer (1000000);
    b.setLength (1000000);
    String arg = new String (b);

    try
      {
	s.startsWith (arg, (1<<31) - 1000000);
      }
    catch (Throwable e)
      {
	Fail ("startsWith", e);
      }
  }

  static void valueOf()
  {
    char[] array = new char[] {'a', 'b', 'c', 'd', 'e'};
    try
      {
	String.valueOf (array, 4, (1<<31)-1);
	Fail ("valueOf", "should not succeed");
      }
    catch (Throwable e)
      {
	ExpectArrayIndex ("valueOf", e);
      }
  }

  public static void main (String[] args) throws Throwable
  {
    getChars();
    //    getBytes();
    regionMatches();
    regionMatchesCase();
    startsWith();
    valueOf();

    if (tests_failed == 0)
      System.out.println ("ok");
  }

  static void ExpectArrayIndex (String test, Throwable e)
  {
    if (e instanceof ArrayIndexOutOfBoundsException)
      return;

    Fail (test, e);
  }

  static void Fail (String test, Object problem)
  {
    ++tests_failed;
    System.err.print (test);
    System.err.print ('\t');
    System.err.println (problem);
  }

  static int tests_failed;
}