harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r428634 - in /incubator/harmony/enhanced/classlib/trunk/modules/text/src: main/java/java/text/Bidi.java test/java/org/apache/harmony/text/tests/java/text/BidiTest.java
Date Fri, 04 Aug 2006 05:51:25 GMT
Author: pyang
Date: Thu Aug  3 22:51:24 2006
New Revision: 428634

URL: http://svn.apache.org/viewvc?rev=428634&view=rev
Log:
Fix for HARMONY-1031([classlib][text]compatibility: Bidi expected IAE) and HARMONY-1059([classlib][text]compatibility:
createLineBidi(-1, 1) expected IAE)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/text/src/main/java/java/text/Bidi.java
    incubator/harmony/enhanced/classlib/trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BidiTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/text/src/main/java/java/text/Bidi.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/text/src/main/java/java/text/Bidi.java?rev=428634&r1=428633&r2=428634&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/text/src/main/java/java/text/Bidi.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/text/src/main/java/java/text/Bidi.java
Thu Aug  3 22:51:24 2006
@@ -1,4 +1,4 @@
-/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 2005, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -132,6 +132,15 @@
 	 */
 	public Bidi(char[] text, int textStart, byte[] embeddings, int embStart,
 			int paragraphLength, int flags) {
+		if (textStart < 0) {
+			throw new IllegalArgumentException("Negative textStart value " + textStart);  
+		}
+		if (embStart < 0) {
+			throw new IllegalArgumentException("Negative embStart value " + embStart);  
+		}
+		if (paragraphLength < 0) {
+			throw new IllegalArgumentException("Negative paragraph length " + paragraphLength);  
+		}
 		long pBidi = createUBiDi(text, textStart, embeddings, embStart,
 				paragraphLength, flags);
 		readBidiInfo(pBidi);
@@ -167,29 +176,31 @@
 
 		byte[] realEmbeddings = null;
 
-		if (text == null || text.length < textStart + paragraphLength) {
+		if (text == null || text.length - textStart< paragraphLength) {
 			throw new IllegalArgumentException();
 		}
 		realText = new char[paragraphLength];
 		System.arraycopy(text, textStart, realText, 0, paragraphLength);
 
 		if (embeddings != null) {
-			if (embeddings.length < embStart + paragraphLength) {
+			if (embeddings.length - embStart < paragraphLength) {
 				throw new IllegalArgumentException();
 			}
-			Bidi temp = new Bidi(text, textStart, null, 0, paragraphLength,
-					flags);
-			realEmbeddings = new byte[paragraphLength];
-			System.arraycopy(temp.offsetLevel, 0, realEmbeddings, 0,
-					paragraphLength);
-			for (int i = 0; i < paragraphLength; i++) {
-				byte e = embeddings[i];
-				if (e < 0) {
-					realEmbeddings[i] = (byte) (BidiWrapper.UBIDI_LEVEL_OVERRIDE - e);
-				} else if (e > 0) {
-					realEmbeddings[i] = e;
-				} else {
-					realEmbeddings[i] |= (byte) BidiWrapper.UBIDI_LEVEL_OVERRIDE;
+			if (paragraphLength > 0) {
+				Bidi temp = new Bidi(text, textStart, null, 0, paragraphLength,
+						flags);
+				realEmbeddings = new byte[paragraphLength];
+				System.arraycopy(temp.offsetLevel, 0, realEmbeddings, 0,
+						paragraphLength);
+				for (int i = 0; i < paragraphLength; i++) {
+					byte e = embeddings[i];
+					if (e < 0) {
+						realEmbeddings[i] = (byte) (BidiWrapper.UBIDI_LEVEL_OVERRIDE - e);
+					} else if (e > 0) {
+						realEmbeddings[i] = e;
+					} else {
+						realEmbeddings[i] |= (byte) BidiWrapper.UBIDI_LEVEL_OVERRIDE;
+					}
 				}
 			}
 		}
@@ -267,6 +278,9 @@
 	 *         range from 0 to (limit - start - 1).
 	 */
 	public Bidi createLineBidi(int lineStart, int lineLimit) {
+		if (lineStart < 0 || lineLimit < 0 || lineLimit > length || lineStart > lineLimit)
{
+			throw new IllegalArgumentException("Invalid ranges (start=" + lineStart + ", limit=" +
lineLimit + ", length=" + length + ")");
+		}
 		char[] text = new char[this.length];
 		Arrays.fill(text, 'a');
 		byte[] embeddings = new byte[this.length];
@@ -440,19 +454,12 @@
 	 * @return true if the range of characters requires a Bidi object.
 	 */
 	public static boolean requiresBidi(char[] text, int start, int limit) {
-		if (limit < 0 || start >= limit) {
-			return false;
-		} else if (start < 0 || start > text.length || limit > text.length) {
-			throw new ArrayIndexOutOfBoundsException();
-		}
-
-		Bidi bidi = new Bidi(text, start, null, 0, limit - start, 0);
-
-		if (bidi.isLeftToRight()) {
-			return false;
-		}
-
-		return true;
+        int length = text.length;
+        if(limit < 0 || start < 0 || start > limit || limit > length){
+            throw new IllegalArgumentException();
+        }
+        Bidi bidi = new Bidi(text, start, null, 0, limit - start, 0);
+		return !bidi.isLeftToRight();
 	}
 
 	/**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BidiTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BidiTest.java?rev=428634&r1=428633&r2=428634&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BidiTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BidiTest.java
Thu Aug  3 22:51:24 2006
@@ -1,4 +1,4 @@
-/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 2005, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -100,6 +100,44 @@
 		} catch (IllegalArgumentException e) {
 			// expected
 		}
+
+        //regression for HARMONY-1031
+		try {
+			bd = new Bidi(new char[] { 't','t','t'}, -1, new byte[] { 2, 2 }, 1, 1, 1);
+			fail("should be IAE");					
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		
+		try {
+			bd = new Bidi(new char[] { 't','t','t'}, 1, new byte[] { 2, 2 }, -1, 1, 1);
+			fail("should be IAE");					
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		
+		try {
+			bd = new Bidi(new char[] { 't','t','t'}, 1, new byte[] { 2, 2 }, 1, -1, 1);
+			fail("should be IAE");					
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		try {
+			bd = new Bidi(new char[] {}, 5, new byte[] { 2, 2, 2, 2, 2, 2 }, 8, Integer.MAX_VALUE,
5);
+			fail("should be IAE");					
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+        try {
+            bd = new Bidi(null, 5, null, 8, Integer.MAX_VALUE, 5);
+            fail("should be IAE");                  
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        
+		bd = new Bidi(new char[] {'o'}, 0, new byte[] { 2, 2}, 2, 0, 2 );
 	}
 
 	public void testEmptyParagraph() {
@@ -598,7 +636,12 @@
 	}
 
 	public void testRequiresBidi() {
-		assertFalse(Bidi.requiresBidi(null, 0, 0));
+		try{
+		    Bidi.requiresBidi(null, 0, 0);
+		    fail("should throw NullPointerException");
+        }catch (NullPointerException e){
+            // expected
+        }
 
 		try {
 			assertFalse(Bidi.requiresBidi(null, 0, 1));
@@ -608,35 +651,60 @@
 		}
 		try {
 			assertFalse(Bidi.requiresBidi("".toCharArray(), 0, 1));
-			fail("should throw ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
-			// expected
-		}
-		try {
-			assertFalse(Bidi.requiresBidi("".toCharArray(), -1, 1));
-			fail("should throw ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
-			// expected
-		}
-		try {
-			assertFalse(Bidi.requiresBidi("".toCharArray(), 1, 2));
-			fail("should throw ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
 			// expected
 		}
 		try {
-			assertFalse(Bidi.requiresBidi("".toCharArray(), 4, 5));
-			fail("should throw ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
+			assertFalse(Bidi.requiresBidi("aaa".toCharArray(), -1, 1));
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
 			// expected
 		}
-		assertFalse(Bidi.requiresBidi("".toCharArray(), 7, 7));
-		assertFalse(Bidi.requiresBidi(" ".toCharArray(), 0, 1));
-		assertFalse(Bidi.requiresBidi("\u05D0".toCharArray(), 1, -1));
-		assertFalse(Bidi.requiresBidi("\u05D0".toCharArray(), -1, -2));
-		assertFalse(Bidi.requiresBidi("\u05D0".toCharArray(), 4, 4));
-		assertFalse(Bidi.requiresBidi("\u05D0".toCharArray(), 3, 1));
+        try {
+            assertFalse(Bidi.requiresBidi("aaa".toCharArray(), 1, -1));
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            assertFalse(Bidi.requiresBidi("\u05D0".toCharArray(), 1, -1));
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            assertFalse(Bidi.requiresBidi("aaa".toCharArray(), 1, 0));
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            assertFalse(Bidi.requiresBidi("aaa".toCharArray(), 7, 7));
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            assertFalse(Bidi.requiresBidi("aaa".toCharArray(), 1, Integer.MAX_VALUE));
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            assertFalse(Bidi.requiresBidi("aaa".toCharArray(), Integer.MAX_VALUE, 1));
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+		
+        assertFalse(Bidi.requiresBidi("".toCharArray(), 0, 0));
+        assertFalse(Bidi.requiresBidi("aaa".toCharArray(), 1, 1));
+        assertFalse(Bidi.requiresBidi("aaa".toCharArray(), 0, 2));
+		assertFalse(Bidi.requiresBidi("\u05D0".toCharArray(), 1, 1));
 		assertTrue(Bidi.requiresBidi("\u05D0".toCharArray(), 0, 1));
+		assertFalse(Bidi.requiresBidi("aa\u05D0a".toCharArray(), 0, 2));
+		assertTrue(Bidi.requiresBidi("aa\u05D0a".toCharArray(), 1, 3));
 	}
 
 	public void testHebrewOverrideEmbeddings() {
@@ -731,6 +799,47 @@
 		assertFalse(line.isRightToLeft());
 	}
 
+    public void testCreateLineBidiInvalid() {
+        //regression for HARMONY-1050
+        Bidi bidi = new Bidi("str", 1);
+        try {
+            bidi.createLineBidi(-1, 1);
+            fail("Expected IAE");           
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+        
+        try {
+            bidi.createLineBidi(1, -1);
+            fail("Expected IAE");           
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+        
+        try {
+            bidi.createLineBidi(-1, -1);
+            fail("Expected IAE");           
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        try {
+            bidi.createLineBidi(2, 1);
+            fail("Expected IAE");           
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+        
+        bidi.createLineBidi(2, 2);
+
+        try {
+            bidi.createLineBidi(2, 4);
+            fail("Expected IAE");           
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+    
 	public void testIncompatibleLineAlgorithm() {
 		// ICU treat a new line as in the same run, however RI does not
 		bd = new Bidi("aaaaa".toCharArray(), 0,



Mime
View raw message