From 7e5acbf5576cafcf49ff5bd8f346088436ea1abc Mon Sep 17 00:00:00 2001 From: Christian Scott Date: Sun, 17 Dec 2023 15:26:19 +1100 Subject: [PATCH 1/2] avoid throwing on invalid digest fn name --- .../build/lib/remote/util/DigestUtil.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java b/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java index df905ff4f4f645..c4d3c1481f2c9c 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java +++ b/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java @@ -30,6 +30,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.HashSet; +import java.util.Set; /** Utility methods to work with {@link Digest}. */ public class DigestUtil { @@ -43,13 +45,20 @@ public DigestUtil(XattrProvider xattrProvider, DigestHashFunction hashFn) { this.digestFunction = getDigestFunctionFromHashFunction(hashFn); } + private static final Set digestHashFunctionNames; + static { + digestHashFunctionNames = new HashSet<>(); + for (var value : DigestFunction.Value.values()) { + digestHashFunctionNames.add(value.name()); + } + } + private static DigestFunction.Value getDigestFunctionFromHashFunction(DigestHashFunction hashFn) { for (String name : hashFn.getNames()) { - try { - return DigestFunction.Value.valueOf(name); - } catch (IllegalArgumentException e) { - // continue. + if (!digestHashFunctionNames.contains(name)) { + continue; } + return DigestFunction.Value.valueOf(name); } return DigestFunction.Value.UNKNOWN; } From ab9f2c6a9188e6fbff32fe7dba8c9cba12ea9bca Mon Sep 17 00:00:00 2001 From: Christian Scott Date: Mon, 18 Dec 2023 09:03:57 +1100 Subject: [PATCH 2/2] address feedback from review --- .../build/lib/remote/util/DigestUtil.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java b/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java index c4d3c1481f2c9c..77e67f80241b6c 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java +++ b/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java @@ -13,11 +13,13 @@ // limitations under the License. package com.google.devtools.build.lib.remote.util; +import static com.google.common.collect.ImmutableSet.toImmutableSet; import static java.nio.charset.StandardCharsets.UTF_8; import build.bazel.remote.execution.v2.Action; import build.bazel.remote.execution.v2.Digest; import build.bazel.remote.execution.v2.DigestFunction; +import com.google.common.collect.ImmutableSet; import com.google.common.hash.HashCode; import com.google.common.io.BaseEncoding; import com.google.devtools.build.lib.actions.cache.VirtualActionInput; @@ -30,8 +32,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.util.HashSet; -import java.util.Set; +import java.util.Arrays; /** Utility methods to work with {@link Digest}. */ public class DigestUtil { @@ -45,20 +46,14 @@ public DigestUtil(XattrProvider xattrProvider, DigestHashFunction hashFn) { this.digestFunction = getDigestFunctionFromHashFunction(hashFn); } - private static final Set digestHashFunctionNames; - static { - digestHashFunctionNames = new HashSet<>(); - for (var value : DigestFunction.Value.values()) { - digestHashFunctionNames.add(value.name()); - } - } + private static final ImmutableSet DIGEST_FUNCTION_NAMES = + Arrays.stream(DigestFunction.Value.values()).map(v -> v.name()).collect(toImmutableSet()); private static DigestFunction.Value getDigestFunctionFromHashFunction(DigestHashFunction hashFn) { for (String name : hashFn.getNames()) { - if (!digestHashFunctionNames.contains(name)) { - continue; + if (DIGEST_FUNCTION_NAMES.contains(name)) { + return DigestFunction.Value.valueOf(name); } - return DigestFunction.Value.valueOf(name); } return DigestFunction.Value.UNKNOWN; }