Skip to content

Add and use CProverString.ofCharArray #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/main/java/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ public String(char value[]) {
// this.value = Arrays.copyOf(value, value.length);
}

/**
* Intermediary function for modelling the constructor for
* String(char value[], int offset, int count).
* The code is copied from the original String(char value[], int offset, int count).
*/
private static String CProverStringOfCharArray(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
// DIFFBLUE MODEL LIBRARY Use CProverString function instead of array copy
return CProverString.ofCharArray(value, offset, count);
// this.value = Arrays.copyOfRange(value, offset, offset+count);
}

/**
* Allocates a new {@code String} that contains characters from a subarray
* of the character array argument. The {@code offset} argument is the
Expand All @@ -216,11 +237,11 @@ public String(char value[]) {
* characters outside the bounds of the {@code value} array
*
* @diffblue.limitedSupport
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why limitedSupport now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasons are detailed below: count limited by unwind, and value cannot be null.

* Does not throw exceptions.
* @diffblue.untested
*/
public String(char value[], int offset, int count) {
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
// DIFFBLUE MODEL LIBRARY
this(CProverStringOfCharArray(value, offset, count));
// if (offset < 0) {
// throw new StringIndexOutOfBoundsException(offset);
// }
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/java/lang/StringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,25 @@ public StringBuilder append(CharSequence s, int start, int end) {
*/
@Override
public StringBuilder append(char[] str) {
// DIFFBLUE MODEL LIBRARY: Handled internally by CBMC
// DIFFBLUE MODEL LIBRARY
String string = CProverString.ofCharArray(str, 0, str.length);
return append(string);
// super.append(str);
// return this;
return CProver.nondetWithNullForNotModelled();
}

/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*
* @diffblue.noSupport
* @diffblue.fullSupport
* @diffblue.untested
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this going to be tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tested by this PR diffblue/cbmc#2374 but maybe we should have more tests in the models library.

*/
@Override
public StringBuilder append(char[] str, int offset, int len) {
// DIFFBLUE MODEL LIBRARY
String string = new String(str, offset, len);
return append(string);
// super.append(str, offset, len);
// return this;
CProver.notModelled();
return CProver.nondetWithNullForNotModelled();
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/cprover/CProverString.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,26 @@ public static StringBuffer insert(
return CProver.nondetWithoutNullForNotModelled();
}

/**
* Converts an array of characters to a string. This uses a loop and
* therefore in test-generation the {@code count} will be limited by the
* unwind parameter.
* This constrains {@code value} to not be null, its length is greater or equal to
* {@code offset + count} and {@code offset} and {@code count} are non-negative.
*
* @param value array of characters which is the source to copy from
* @param offset index in {@code value} of the first character to copy
* @param count number of characters to copy
* @return The created String
*/
public static String ofCharArray(char value[], int offset, int count) {
CProver.assume(value != null);
CProver.assume(value.length - count >= offset
&& offset >= 0 && count >= 0);
StringBuilder builder = new StringBuilder();
for(int i = 0; i < count; i++) {
builder.append(value[offset + i]);
}
return builder.toString();
}
}