copy VersionedValue before returning to protect internal state
Change-Id: If56c512488b90bbfbabe32434e567b463d4acf1a
Showing
2 changed files
with
23 additions
and
1 deletions
| ... | @@ -38,6 +38,28 @@ public class VersionedValue { | ... | @@ -38,6 +38,28 @@ public class VersionedValue { |
| 38 | return version; | 38 | return version; |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | + /** | ||
| 42 | + * Creates a copy of given VersionedValue. | ||
| 43 | + * | ||
| 44 | + * @param original VersionedValue to create a copy | ||
| 45 | + * @return same as original if original or it's value is null, | ||
| 46 | + * otherwise creates a copy. | ||
| 47 | + */ | ||
| 48 | + public static VersionedValue copy(VersionedValue original) { | ||
| 49 | + if (original == null) { | ||
| 50 | + return null; | ||
| 51 | + } | ||
| 52 | + if (original.value == null) { | ||
| 53 | + // immutable, no need to copy | ||
| 54 | + return original; | ||
| 55 | + } else { | ||
| 56 | + return new VersionedValue( | ||
| 57 | + Arrays.copyOf(original.value, | ||
| 58 | + original.value.length), | ||
| 59 | + original.version); | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 41 | @Override | 63 | @Override |
| 42 | public String toString() { | 64 | public String toString() { |
| 43 | return MoreObjects.toStringHelper(getClass()) | 65 | return MoreObjects.toStringHelper(getClass()) | ... | ... |
| ... | @@ -76,7 +76,7 @@ public class DatabaseStateMachine implements StateMachine { | ... | @@ -76,7 +76,7 @@ public class DatabaseStateMachine implements StateMachine { |
| 76 | results.add(new InternalReadResult(InternalReadResult.Status.NO_SUCH_TABLE, null)); | 76 | results.add(new InternalReadResult(InternalReadResult.Status.NO_SUCH_TABLE, null)); |
| 77 | continue; | 77 | continue; |
| 78 | } | 78 | } |
| 79 | - VersionedValue value = table.get(request.key()); | 79 | + VersionedValue value = VersionedValue.copy(table.get(request.key())); |
| 80 | results.add(new InternalReadResult( | 80 | results.add(new InternalReadResult( |
| 81 | InternalReadResult.Status.OK, | 81 | InternalReadResult.Status.OK, |
| 82 | new ReadResult( | 82 | new ReadResult( | ... | ... |
-
Please register or login to post a comment