Explorar o código

Use Arrays.hashCode() for arrays in Objects.hash() calls (#105175)

When an array is passed to Objects.hash() it needs to be wrapped with Arrays.hashCode() for calculating the hash of the array content rather than using the array instance "identity hash code"
Dmitry Cherniachenko hai 1 ano
pai
achega
ba4d2f5843
Modificáronse 14 ficheiros con 47 adicións e 21 borrados
  1. 12 1
      server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java
  2. 1 1
      server/src/main/java/org/elasticsearch/action/resync/ResyncReplicationRequest.java
  3. 2 1
      server/src/main/java/org/elasticsearch/indices/breaker/HierarchyCircuitBreakerService.java
  4. 4 3
      x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopySettingsStep.java
  5. 3 2
      x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteExpiredDataAction.java
  6. 1 1
      x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java
  7. 1 1
      x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/InvalidateApiKeyRequest.java
  8. 3 1
      x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/UserAndPassword.java
  9. 2 2
      x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/formatter/SimpleFormatter.java
  10. 11 1
      x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplate.java
  11. 4 4
      x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Attachment.java
  12. 1 1
      x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessage.java
  13. 1 1
      x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageDefaults.java
  14. 1 1
      x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequest.java

+ 12 - 1
server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java

@@ -582,7 +582,18 @@ public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesReq
 
         @Override
         public int hashCode() {
-            return Objects.hash(type, indices, aliases, filter, routing, indexRouting, searchRouting, writeIndex, isHidden, mustExist);
+            return Objects.hash(
+                type,
+                Arrays.hashCode(indices),
+                Arrays.hashCode(aliases),
+                filter,
+                routing,
+                indexRouting,
+                searchRouting,
+                writeIndex,
+                isHidden,
+                mustExist
+            );
         }
     }
 

+ 1 - 1
server/src/main/java/org/elasticsearch/action/resync/ResyncReplicationRequest.java

@@ -80,7 +80,7 @@ public final class ResyncReplicationRequest extends ReplicatedWriteRequest<Resyn
 
     @Override
     public int hashCode() {
-        return Objects.hash(trimAboveSeqNo, maxSeenAutoIdTimestampOnPrimary, operations);
+        return Objects.hash(trimAboveSeqNo, maxSeenAutoIdTimestampOnPrimary, Arrays.hashCode(operations));
     }
 
     @Override

+ 2 - 1
server/src/main/java/org/elasticsearch/indices/breaker/HierarchyCircuitBreakerService.java

@@ -601,7 +601,7 @@ public class HierarchyCircuitBreakerService extends CircuitBreakerService {
         }
 
         static long fallbackRegionSize(JvmInfo jvmInfo) {
-            // mimick JDK calculation based on JDK 14 source:
+            // mimic JDK calculation based on JDK 14 source:
             // https://hg.openjdk.java.net/jdk/jdk14/file/6c954123ee8d/src/hotspot/share/gc/g1/heapRegion.cpp#l65
             // notice that newer JDKs will have a slight variant only considering max-heap:
             // https://hg.openjdk.java.net/jdk/jdk/file/e7d0ec2d06e8/src/hotspot/share/gc/g1/heapRegion.cpp#l67
@@ -739,6 +739,7 @@ public class HierarchyCircuitBreakerService extends CircuitBreakerService {
                     if (initialCollectionCount != gcCountSupplier.getAsLong()) {
                         break;
                     }
+                    // noinspection ArrayHashCode - prevent array allocation from being optimized away
                     localBlackHole += new byte[allocationSize].hashCode();
                 }
 

+ 4 - 3
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopySettingsStep.java

@@ -15,6 +15,7 @@ import org.elasticsearch.cluster.metadata.Metadata;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.index.Index;
 
+import java.util.Arrays;
 import java.util.Locale;
 import java.util.Objects;
 import java.util.function.BiFunction;
@@ -58,7 +59,7 @@ public class CopySettingsStep extends ClusterStateActionStep {
 
     BiFunction<String, LifecycleExecutionState, String> getTargetIndexNameSupplier() {
         return targetIndexNameSupplier;
-    };
+    }
 
     @Override
     public ClusterState performAction(Index index, ClusterState clusterState) {
@@ -115,11 +116,11 @@ public class CopySettingsStep extends ClusterStateActionStep {
         CopySettingsStep that = (CopySettingsStep) o;
         return super.equals(o)
             && Objects.equals(targetIndexNameSupplier, that.targetIndexNameSupplier)
-            && Objects.equals(settingsKeys, that.settingsKeys);
+            && Arrays.equals(settingsKeys, that.settingsKeys);
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(super.hashCode(), targetIndexNameSupplier, settingsKeys);
+        return Objects.hash(super.hashCode(), targetIndexNameSupplier, Arrays.hashCode(settingsKeys));
     }
 }

+ 3 - 2
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteExpiredDataAction.java

@@ -21,6 +21,7 @@ import org.elasticsearch.xcontent.XContentParser;
 import org.elasticsearch.xpack.core.ml.job.config.Job;
 
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.Objects;
 
 public class DeleteExpiredDataAction extends ActionType<DeleteExpiredDataAction.Response> {
@@ -130,13 +131,13 @@ public class DeleteExpiredDataAction extends ActionType<DeleteExpiredDataAction.
             Request request = (Request) o;
             return Objects.equals(requestsPerSecond, request.requestsPerSecond)
                 && Objects.equals(jobId, request.jobId)
-                && Objects.equals(expandedJobIds, request.expandedJobIds)
+                && Arrays.equals(expandedJobIds, request.expandedJobIds)
                 && Objects.equals(timeout, request.timeout);
         }
 
         @Override
         public int hashCode() {
-            return Objects.hash(requestsPerSecond, timeout, jobId, expandedJobIds);
+            return Objects.hash(requestsPerSecond, timeout, jobId, Arrays.hashCode(expandedJobIds));
         }
 
         @Override

+ 1 - 1
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java

@@ -56,7 +56,7 @@ public class RawInferenceResults implements InferenceResults {
 
     @Override
     public int hashCode() {
-        return Objects.hash(Arrays.hashCode(value), featureImportance);
+        return Objects.hash(Arrays.hashCode(value), Arrays.deepHashCode(featureImportance));
     }
 
     @Override

+ 1 - 1
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/InvalidateApiKeyRequest.java

@@ -246,7 +246,7 @@ public final class InvalidateApiKeyRequest extends ActionRequest {
 
     @Override
     public int hashCode() {
-        return Objects.hash(realmName, userName, ids, name, ownedByAuthenticatedUser);
+        return Objects.hash(realmName, userName, Arrays.hashCode(ids), name, ownedByAuthenticatedUser);
     }
 
     private static void validateIds(@Nullable String[] idsToValidate) {

+ 3 - 1
x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/UserAndPassword.java

@@ -10,6 +10,8 @@ import org.elasticsearch.common.settings.SecureString;
 import org.elasticsearch.xpack.core.security.authc.support.Hasher;
 import org.elasticsearch.xpack.core.security.user.User;
 
+import java.util.Arrays;
+
 /**
  * Like User, but includes the hashed password
  *
@@ -51,7 +53,7 @@ class UserAndPassword {
     @Override
     public int hashCode() {
         int result = this.user.hashCode();
-        result = 31 * result + passwordHash().hashCode();
+        result = 31 * result + Arrays.hashCode(passwordHash());
         return result;
     }
 

+ 2 - 2
x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/formatter/SimpleFormatter.java

@@ -138,7 +138,7 @@ public class SimpleFormatter {
                     }
                 } else {
                     // Trim
-                    sb.append(string.substring(0, width[i] - 1));
+                    sb.append(string, 0, width[i] - 1);
                     sb.append('~');
                 }
             }
@@ -175,6 +175,6 @@ public class SimpleFormatter {
 
     @Override
     public int hashCode() {
-        return Objects.hash(width, formatOption);
+        return Objects.hash(Arrays.hashCode(width), formatOption);
     }
 }

+ 11 - 1
x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplate.java

@@ -196,7 +196,17 @@ public class EmailTemplate implements ToXContentObject {
 
     @Override
     public int hashCode() {
-        return Objects.hash(from, replyTo, priority, to, cc, bcc, subject, textBody, htmlBody);
+        return Objects.hash(
+            from,
+            Arrays.hashCode(replyTo),
+            priority,
+            Arrays.hashCode(to),
+            Arrays.hashCode(cc),
+            Arrays.hashCode(bcc),
+            subject,
+            textBody,
+            htmlBody
+        );
     }
 
     @Override

+ 4 - 4
x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Attachment.java

@@ -105,10 +105,10 @@ public class Attachment implements MessageElement {
             title,
             titleLink,
             text,
-            fields,
+            Arrays.hashCode(fields),
             imageUrl,
             thumbUrl,
-            markdownSupportedFields,
+            Arrays.hashCode(markdownSupportedFields),
             actions
         );
     }
@@ -311,10 +311,10 @@ public class Attachment implements MessageElement {
                 title,
                 titleLink,
                 text,
-                fields,
+                Arrays.hashCode(fields),
                 imageUrl,
                 thumbUrl,
-                markdownSupportedFields,
+                Arrays.hashCode(markdownSupportedFields),
                 actions
             );
         }

+ 1 - 1
x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessage.java

@@ -184,7 +184,7 @@ public class SlackMessage implements MessageElement {
 
         @Override
         public int hashCode() {
-            return Objects.hash(from, to, text, icon, attachments, dynamicAttachments);
+            return Objects.hash(from, Arrays.hashCode(to), text, icon, Arrays.hashCode(attachments), dynamicAttachments);
         }
 
         public SlackMessage render(

+ 1 - 1
x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageDefaults.java

@@ -52,7 +52,7 @@ public class SlackMessageDefaults {
 
     @Override
     public int hashCode() {
-        return Objects.hash(to, icon, text, attachment);
+        return Objects.hash(Arrays.hashCode(to), icon, text, attachment);
     }
 
     static class AttachmentDefaults {

+ 1 - 1
x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequest.java

@@ -285,7 +285,7 @@ public class WatcherSearchTemplateRequest implements ToXContentObject {
 
     @Override
     public int hashCode() {
-        return Objects.hash(indices, searchType, indicesOptions, searchSource, template, restTotalHitsAsInt);
+        return Objects.hash(Arrays.hashCode(indices), searchType, indicesOptions, searchSource, template, restTotalHitsAsInt);
     }
 
     private static final ParseField INDICES_FIELD = new ParseField("indices");