From 097b158c3b03c275ccef27bdb35c91ccf8372a02 Mon Sep 17 00:00:00 2001 From: Ivan Frade Date: Tue, 11 Feb 2025 09:57:55 -0800 Subject: DefaultTypedConfigGetter: Box values to avoid infinite recursion Errorprone says: DefaultTypedConfigGetter.java:176: error: [InfiniteRecursion] This method always recurses, and will cause a StackOverflowError return getLong(config, section, subsection, name, defaultValue); [1] introduced new getters with boxed types to return a null when the config is not set. The getters of unboxed types should call to the boxed version, but, as the values are not explicitely boxed, they are calling to themselves. [1] https://gerrithub.io/c/eclipse-jgit/jgit/+/1207895 Change-Id: Ied45a199c8ef905e3774a17a04d91a656aa0e42b --- .../eclipse/jgit/lib/DefaultTypedConfigGetter.java | 40 +++++++++++++++------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'org.eclipse.jgit/src/org') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/DefaultTypedConfigGetter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/DefaultTypedConfigGetter.java index 65093987e8..3059f283fe 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/DefaultTypedConfigGetter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/DefaultTypedConfigGetter.java @@ -32,10 +32,12 @@ import org.eclipse.jgit.util.StringUtils; */ public class DefaultTypedConfigGetter implements TypedConfigGetter { + @SuppressWarnings("boxed") @Override public boolean getBoolean(Config config, String section, String subsection, String name, boolean defaultValue) { - return getBoolean(config, section, subsection, name, defaultValue); + return neverNull(getBoolean(config, section, subsection, name, + Boolean.valueOf(defaultValue))); } @Nullable @@ -116,7 +118,8 @@ public class DefaultTypedConfigGetter implements TypedConfigGetter { @Override public int getInt(Config config, String section, String subsection, String name, int defaultValue) { - return getInt(config, section, subsection, name, defaultValue); + return neverNull(getInt(config, section, subsection, name, + Integer.valueOf(defaultValue))); } @Nullable @@ -144,8 +147,8 @@ public class DefaultTypedConfigGetter implements TypedConfigGetter { @Override public int getIntInRange(Config config, String section, String subsection, String name, int minValue, int maxValue, int defaultValue) { - return getIntInRange(config, section, subsection, name, minValue, - maxValue, defaultValue); + return neverNull(getIntInRange(config, section, subsection, name, + minValue, maxValue, Integer.valueOf(defaultValue))); } @Override @@ -161,9 +164,9 @@ public class DefaultTypedConfigGetter implements TypedConfigGetter { return val; } if (subsection == null) { - throw new IllegalArgumentException(MessageFormat.format( - JGitText.get().integerValueNotInRange, section, name, - val, minValue, maxValue)); + throw new IllegalArgumentException( + MessageFormat.format(JGitText.get().integerValueNotInRange, + section, name, val, minValue, maxValue)); } throw new IllegalArgumentException(MessageFormat.format( JGitText.get().integerValueNotInRangeSubSection, section, @@ -173,7 +176,8 @@ public class DefaultTypedConfigGetter implements TypedConfigGetter { @Override public long getLong(Config config, String section, String subsection, String name, long defaultValue) { - return getLong(config, section, subsection, name, defaultValue); + return neverNull(getLong(config, section, subsection, name, + Long.valueOf(defaultValue))); } @Nullable @@ -190,8 +194,9 @@ public class DefaultTypedConfigGetter implements TypedConfigGetter { // Empty return defaultValue; } catch (NumberFormatException nfe) { - throw new IllegalArgumentException(MessageFormat.format( - JGitText.get().invalidIntegerValue, section, name, str), + throw new IllegalArgumentException( + MessageFormat.format(JGitText.get().invalidIntegerValue, + section, name, str), nfe); } } @@ -199,9 +204,8 @@ public class DefaultTypedConfigGetter implements TypedConfigGetter { @Override public long getTimeUnit(Config config, String section, String subsection, String name, long defaultValue, TimeUnit wantUnit) { - Long v = getTimeUnit(config, section, subsection, name, - Long.valueOf(defaultValue), wantUnit); - return v == null ? defaultValue : v.longValue(); + return neverNull(getTimeUnit(config, section, subsection, name, + Long.valueOf(defaultValue), wantUnit)); } @Override @@ -325,4 +329,14 @@ public class DefaultTypedConfigGetter implements TypedConfigGetter { } return result; } + + // Trick for the checkers. When we use this, one is never null, but + // they don't know. + @NonNull + private static T neverNull(T one) { + if (one == null) { + throw new IllegalArgumentException(); + } + return one; + } } -- cgit v1.2.3