From 7fdf66df23fc8d48be3585e33f38ea7eb90e6ca7 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Tue, 7 Sep 2021 18:14:48 -0500 Subject: [PATCH] Detecting when a deserialized Map is immutable before changing it in IndexDiskUsageStats (#77219) (#77389) This commit prevents errors that can happen if an empty fields map is serialized and deserialized in IndexDiskUsageStats. --- .../diskusage/IndexDiskUsageStats.java | 2 +- .../diskusage/IndexDiskUsageStatsTests.java | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStatsTests.java diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStats.java b/server/src/main/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStats.java index e6abb6359084d..bf61d1d35d2de 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStats.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStats.java @@ -53,7 +53,7 @@ public IndexDiskUsageStats(long indexSizeInBytes) { } public IndexDiskUsageStats(StreamInput in) throws IOException { - this.fields = in.readMap(StreamInput::readString, PerFieldDiskUsage::new); + this.fields = new HashMap<>(in.readMap(StreamInput::readString, PerFieldDiskUsage::new)); this.indexSizeInBytes = in.readVLong(); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStatsTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStatsTests.java new file mode 100644 index 0000000000000..286a7f947607b --- /dev/null +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/diskusage/IndexDiskUsageStatsTests.java @@ -0,0 +1,37 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +package org.elasticsearch.action.admin.indices.diskusage; + +import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; + +public class IndexDiskUsageStatsTests extends ESTestCase { + + public void testEmptySerialization() throws IOException { + IndexDiskUsageStats emptyDndexDiskUsageStats = createEmptyDiskUsageStats(); + try (BytesStreamOutput out = new BytesStreamOutput()) { + emptyDndexDiskUsageStats.writeTo(out); + try (StreamInput in = out.bytes().streamInput()) { + IndexDiskUsageStats deserializedNodeStats = new IndexDiskUsageStats(in); + assertEquals(emptyDndexDiskUsageStats.getIndexSizeInBytes(), deserializedNodeStats.getIndexSizeInBytes()); + assertEquals(emptyDndexDiskUsageStats.getFields(), deserializedNodeStats.getFields()); + // Now just making sure that an exception doesn't get thrown here: + deserializedNodeStats.addStoredField(randomAlphaOfLength(10), randomNonNegativeLong()); + } + } + } + + private IndexDiskUsageStats createEmptyDiskUsageStats() { + return new IndexDiskUsageStats(randomNonNegativeLong()); + } + +}