Fix invalid data sharing in Metadata.merge()

This commit is contained in:
Eric Anderson 2015-07-19 09:45:56 -07:00
parent bd8987af1a
commit 5698ccaee6
1 changed files with 15 additions and 1 deletions

View File

@ -225,7 +225,11 @@ public abstract class Metadata {
"Cannot merge non-serializable metadata into serializable metadata without keys"); "Cannot merge non-serializable metadata into serializable metadata without keys");
} }
} }
store.putAll(other.store); for (MetadataEntry entry : other.store.values()) {
// Must copy the MetadataEntries since they are mutated. If the two Metadata objects are used
// from different threads it would cause thread-safety issues.
store.put(entry.key.name(), new MetadataEntry(entry));
}
} }
/** /**
@ -546,6 +550,16 @@ public abstract class Metadata {
this.isBinary = isBinary; this.isBinary = isBinary;
} }
/**
* Copy constructor.
*/
private MetadataEntry(MetadataEntry entry) {
this.parsed = entry.parsed;
this.key = entry.key;
this.isBinary = entry.isBinary;
this.serializedBinary = entry.serializedBinary;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getParsed(Key<T> key) { public <T> T getParsed(Key<T> key) {
T value = (T) parsed; T value = (T) parsed;