Skip to content

Commit

Permalink
Merge pull request #15979 from mikezhang1234567890/string
Browse files Browse the repository at this point in the history
Add missing bounds check to deleteCharAt
  • Loading branch information
keithc-ca committed Sep 27, 2022
2 parents cb13645 + dd4d6e7 commit 4e055dc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
12 changes: 7 additions & 5 deletions jcl/src/java.base/share/classes/java/lang/StringBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,16 @@ public synchronized char charAt(int index) {
}

/**
* Deletes a range of characters.
* Deletes a range of characters starting from offset {@code start} to offset
* {@code end - 1}. If {@code end} is beyond the last character, then this
* method deletes up to the end of the StringBuffer.
*
* @param start the offset of the first character
* @param end the offset one past the last character
* @return this StringBuffer
*
* @exception StringIndexOutOfBoundsException when {@code start < 0, start > end} or
* {@code end > length()}
* @exception StringIndexOutOfBoundsException when {@code start < 0}, {@code start > end} or
* {@code start > length()}
*/
public synchronized StringBuffer delete(int start, int end) {
int currentLength = lengthInternalUnsynchronized();
Expand Down Expand Up @@ -754,8 +756,8 @@ public synchronized StringBuffer delete(int start, int end) {
public synchronized StringBuffer deleteCharAt(int location) {
int currentLength = lengthInternalUnsynchronized();

if (currentLength != 0) {
return delete (location, location + 1);
if ((0 <= location) && (location < currentLength)) {
return delete(location, location + 1);
} else {
throw new StringIndexOutOfBoundsException ();
}
Expand Down
12 changes: 7 additions & 5 deletions jcl/src/java.base/share/classes/java/lang/StringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -656,14 +656,16 @@ public char charAt(int index) {
}

/**
* Deletes a range of characters.
* Deletes a range of characters starting from offset {@code start} to offset
* {@code end - 1}. If {@code end} is beyond the last character, then this
* method deletes up to the end of the StringBuilder.
*
* @param start the offset of the first character
* @param end the offset one past the last character
* @return this StringBuilder
*
* @exception StringIndexOutOfBoundsException when {@code start < 0, start > end} or
* {@code end > length()}
* @exception StringIndexOutOfBoundsException when {@code start < 0}, {@code start > end} or
* {@code start > length()}
*/
public StringBuilder delete(int start, int end) {
int currentLength = lengthInternal();
Expand Down Expand Up @@ -753,8 +755,8 @@ public StringBuilder delete(int start, int end) {
public StringBuilder deleteCharAt(int location) {
int currentLength = lengthInternal();

if (currentLength != 0) {
return delete (location, location + 1);
if ((0 <= location) && (location < currentLength)) {
return delete(location, location + 1);
} else {
throw new StringIndexOutOfBoundsException ();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1998, 2018 IBM Corp. and others
* Copyright (c) 1998, 2022 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -307,8 +307,22 @@ public void test_delete() {
*/
@Test
public void test_deleteCharAt() {
testBuffer.deleteCharAt(3);
AssertJUnit.assertTrue("Deleted incorrect char", testBuffer.toString().equals("Thi is a test buffer"));
testBuffer.deleteCharAt(0);
AssertJUnit.assertTrue("Deleted incorrect char", testBuffer.toString().equals("his is a test buffer"));
testBuffer.deleteCharAt(2);
AssertJUnit.assertTrue("Deleted incorrect char", testBuffer.toString().equals("hi is a test buffer"));
testBuffer.deleteCharAt(testBuffer.length() - 1);
AssertJUnit.assertTrue("Deleted incorrect char", testBuffer.toString().equals("hi is a test buffe"));
try {
testBuffer.deleteCharAt(-1);
AssertJUnit.fail("Index less than zero should have failed");
} catch (IndexOutOfBoundsException e) {
}
try {
testBuffer.deleteCharAt(testBuffer.length());
AssertJUnit.fail("Index >= buffer length should have failed");
} catch (IndexOutOfBoundsException e) {
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1998, 2018 IBM Corp. and others
* Copyright (c) 1998, 2022 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -310,6 +310,29 @@ public void test_delete() {
AssertJUnit.assertTrue("Wrong contents 2", testBuffer.toString().equals("more stuff"));
}

/**
* @tests java.lang.StringBuilder#deleteCharAt(int)
*/
@Test
public void test_deleteCharAt() {
testBuffer.deleteCharAt(0);
AssertJUnit.assertTrue("Deleted incorrect char", testBuffer.toString().equals("his is a test buffer"));
testBuffer.deleteCharAt(2);
AssertJUnit.assertTrue("Deleted incorrect char", testBuffer.toString().equals("hi is a test buffer"));
testBuffer.deleteCharAt(testBuffer.length() - 1);
AssertJUnit.assertTrue("Deleted incorrect char", testBuffer.toString().equals("hi is a test buffe"));
try {
testBuffer.deleteCharAt(-1);
AssertJUnit.fail("Index less than zero should have failed");
} catch (IndexOutOfBoundsException e) {
}
try {
testBuffer.deleteCharAt(testBuffer.length());
AssertJUnit.fail("Index >= buffer length should have failed");
} catch (IndexOutOfBoundsException e) {
}
}

/**
* @tests java.lang.StringBuilder#setLength(int)
*/
Expand Down

0 comments on commit 4e055dc

Please sign in to comment.