Yuta HIGUCHI

copy VersionedValue before returning to protect internal state

Change-Id: If56c512488b90bbfbabe32434e567b463d4acf1a
......@@ -38,6 +38,28 @@ public class VersionedValue {
return version;
}
/**
* Creates a copy of given VersionedValue.
*
* @param original VersionedValue to create a copy
* @return same as original if original or it's value is null,
* otherwise creates a copy.
*/
public static VersionedValue copy(VersionedValue original) {
if (original == null) {
return null;
}
if (original.value == null) {
// immutable, no need to copy
return original;
} else {
return new VersionedValue(
Arrays.copyOf(original.value,
original.value.length),
original.version);
}
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
......
......@@ -76,7 +76,7 @@ public class DatabaseStateMachine implements StateMachine {
results.add(new InternalReadResult(InternalReadResult.Status.NO_SUCH_TABLE, null));
continue;
}
VersionedValue value = table.get(request.key());
VersionedValue value = VersionedValue.copy(table.get(request.key()));
results.add(new InternalReadResult(
InternalReadResult.Status.OK,
new ReadResult(
......