summaryrefslogtreecommitdiff
path: root/libjava/testsuite/libjava.lang/StringBuffer_overflow.java
blob: 68e18da2f961b2ba8b292c3bce852c11032a8ae9 (plain)
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
/* This tests some corner cases of arithmetic in StringBuffer.  */

/* These tests can all be run on a 32 bit machine with modest amounts
 * of memory.  */

/* The symptom of the problem is that ArrayIndexOutOfBoundsException
 * gets thrown, while the documentation says that
 * StringIndexOutOfBoundsException should be thrown.  */

class StringBuffer_overflow
{
  /* Test correct exception on getChars.  */
  static void getChars()
  {
    StringBuffer b = new StringBuffer ("x");
    char[] s = new char [1];
    try
      {
	// The substring we are attempting to obtain is invalid,
	// so we should get a StringIndexOutOfBoundsException.
	b.getChars (1, -1 << 31, s, 0);
	Fail ("getChars", "no exception");
      }
    catch (Throwable e)
      {
	ExpectStringIndex ("getChars()", e);
      }
  }

  /* Test correct exception on append with bogus count. */
  static void append()
  {
    StringBuffer s = new StringBuffer("a");
    try
      {
	s.append ("".toCharArray(), 1, (1<<31)-1);
	Fail ("append", "no exception");
      }
    catch (Throwable e)
      {
	ExpectStringIndex ("append", e);
      }
  }

  // Check that append still more or less works.
  static void appendbasic()
  {
    StringBuffer s = new StringBuffer();

    try
      {
	if (!new StringBuffer().append ("abcdefg".toCharArray())
	    .toString().equals ("abcdefg"))
	  {
	    Fail ("appendbasic", "append gives incorrect result");
	  }
      }
    catch (Throwable e)
      {
	Fail ("appendbasic", e);
      }
  }

  /* Test correct expception on substring with bogus indexes.  */
  static void substring()
  {
    StringBuffer s = new StringBuffer ("abc");
    try
      {
	// end - begin == -2 - ((1<<31)-1) == (1<<31) - 1 > 0.  */
	s.substring ((1<<31)-1, -2);
	Fail ("substring", "no exception");
      }
    catch (Throwable e)
      {
	ExpectStringIndex ("substring", e);
      }
  }

  static void insert()
  {
    StringBuffer s = new StringBuffer ("");
    try
      {
	s.insert (0, "abcd".toCharArray(), (1<<31)-1, 1);
	Fail ("insert", "no exception");
      }
    catch (Throwable e)
      {
	ExpectStringIndex ("insert", e);
      }
  }


  public static void main (String[] unused)
  {
    getChars();
    append();
    appendbasic();
    substring();
    insert();

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

  static int tests_failed = 0;

  static void ExpectStringIndex (String name, Throwable exception)
  {
    if (! (exception instanceof StringIndexOutOfBoundsException))
      {
	Fail (name, exception);
      }
  }
  static void Fail (String name, Object why)
  {
    ++tests_failed;

    System.err.print (name);
    System.err.print ('\t');
    System.err.println (why);
  }
}