ICU-1533 catch bounds violations

X-SVN-Rev: 6988
This commit is contained in:
Alan Liu 2001-11-19 19:27:51 +00:00
parent 039dbe8ff1
commit 06c701ce89
5 changed files with 68 additions and 19 deletions

View file

@ -150,10 +150,15 @@ Transliterator& Transliterator::operator=(const Transliterator& other) {
* <= limit</code>.
* @param limit the ending index, exclusive; <code>start <= limit
* <= text.length()</code>.
* @return the new limit index
* @return the new limit index, or -1
*/
int32_t Transliterator::transliterate(Replaceable& text,
int32_t start, int32_t limit) const {
if (start < 0 ||
limit < start ||
text.length() < limit) {
return -1;
}
UTransPosition offsets;
offsets.contextStart= start;
@ -295,6 +300,14 @@ void Transliterator::transliterate(Replaceable& text,
*/
void Transliterator::finishTransliteration(Replaceable& text,
UTransPosition& index) const {
if (index.contextStart < 0 ||
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
return;
}
filteredTransliterate(text, index, FALSE);
}

View file

@ -384,15 +384,11 @@ public:
* <= limit</code>.
* @param limit the ending index, exclusive; <code>start <= limit
* <= text.length()</code>.
* @param filter the filter. Any character for which
* <tt>filter.contains()</tt> returns <tt>false</tt> will not be
* altered by this transliterator. If <tt>filter</tt> is
* <tt>null</tt> then no filtering is applied.
* @return The new limit index. The text previously occupying <code>[start,
* limit)</code> has been transliterated, possibly to a string of a different
* length, at <code>[start, </code><em>new-limit</em><code>)</code>, where
* <em>new-limit</em> is the return value. If the input offsets are out of bounds,
* the returned value will be 0 and the input string remains unchanged.
* the returned value is -1 and the input string remains unchanged.
* @stable
*/
virtual int32_t transliterate(Replaceable& text,

View file

@ -69,15 +69,15 @@ void TransliteratorErrorTest::TestTransliteratorErrors() {
}
len = testString.length();
stoppedAt = t->transliterate(testString, 0, 100);
if (stoppedAt != 0) {
errln("FAIL: Out of bounds check failed.");
if (stoppedAt != -1) {
errln("FAIL: Out of bounds check failed (1).");
} else if (testString.length() != len) {
testString="A quick fox jumped over the lazy dog.";
errln("FAIL: Transliterate fails and the target string was modified.");
}
stoppedAt = t->transliterate(testString, 100, testString.length()-1);
if (stoppedAt != 0)
errln("FAIL: Out of bounds check failed.");
if (stoppedAt != -1)
errln("FAIL: Out of bounds check failed (2).");
else if (testString.length() != len) {
testString="A quick fox jumped over the lazy dog.";
errln("FAIL: Transliterate fails and the target string was modified.");
@ -126,7 +126,7 @@ void TransliteratorErrorTest::TestTransliteratorErrors() {
pos.start = 5;
t->transliterate(testString, pos, insertString, status);
if (U_SUCCESS(status)) {
errln("FAIL: Out of bounds check failed.");
errln("FAIL: Out of bounds check failed (3).");
if (testString.length() != len)
errln("FAIL: The input string was modified though the offsets were out of bounds.");
}

View file

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Transliterator.java,v $
* $Date: 2001/11/17 20:58:34 $
* $Revision: 1.59 $
* $Date: 2001/11/19 19:27:51 $
* $Revision: 1.60 $
*
*****************************************************************************************
*/
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
*
* @author Alan Liu
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.59 $ $Date: 2001/11/17 20:58:34 $
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.60 $ $Date: 2001/11/19 19:27:51 $
*/
public abstract class Transliterator {
/**
@ -427,9 +427,16 @@ public abstract class Transliterator {
* @return The new limit index. The text previously occupying <code>[start,
* limit)</code> has been transliterated, possibly to a string of a different
* length, at <code>[start, </code><em>new-limit</em><code>)</code>, where
* <em>new-limit</em> is the return value.
* <em>new-limit</em> is the return value. If the input offsets are out of bounds,
* the returned value is -1 and the input string remains unchanged.
*/
public final int transliterate(Replaceable text, int start, int limit) {
if (start < 0 ||
limit < start ||
text.length() < limit) {
return -1;
}
Position pos = new Position(start, limit, start);
filteredTransliterate(text, pos, false);
return pos.limit;
@ -589,6 +596,19 @@ public abstract class Transliterator {
*/
public final void finishTransliteration(Replaceable text,
Position index) {
if (index.contextStart < 0 ||
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
throw new IllegalArgumentException("Invalid index {" +
index.contextStart + ", " +
index.start + ", " +
index.limit + ", " +
index.contextLimit + "}, len=" +
text.length());
}
filteredTransliterate(text, index, false);
}

View file

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/Transliterator.java,v $
* $Date: 2001/11/17 20:58:34 $
* $Revision: 1.59 $
* $Date: 2001/11/19 19:27:51 $
* $Revision: 1.60 $
*
*****************************************************************************************
*/
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
*
* @author Alan Liu
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.59 $ $Date: 2001/11/17 20:58:34 $
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.60 $ $Date: 2001/11/19 19:27:51 $
*/
public abstract class Transliterator {
/**
@ -427,9 +427,16 @@ public abstract class Transliterator {
* @return The new limit index. The text previously occupying <code>[start,
* limit)</code> has been transliterated, possibly to a string of a different
* length, at <code>[start, </code><em>new-limit</em><code>)</code>, where
* <em>new-limit</em> is the return value.
* <em>new-limit</em> is the return value. If the input offsets are out of bounds,
* the returned value is -1 and the input string remains unchanged.
*/
public final int transliterate(Replaceable text, int start, int limit) {
if (start < 0 ||
limit < start ||
text.length() < limit) {
return -1;
}
Position pos = new Position(start, limit, start);
filteredTransliterate(text, pos, false);
return pos.limit;
@ -589,6 +596,19 @@ public abstract class Transliterator {
*/
public final void finishTransliteration(Replaceable text,
Position index) {
if (index.contextStart < 0 ||
index.start < index.contextStart ||
index.limit < index.start ||
index.contextLimit < index.limit ||
text.length() < index.contextLimit) {
throw new IllegalArgumentException("Invalid index {" +
index.contextStart + ", " +
index.start + ", " +
index.limit + ", " +
index.contextLimit + "}, len=" +
text.length());
}
filteredTransliterate(text, index, false);
}