Set lower limit for glob pattern resolution in test mode (#693)

Motivation:
Speed up the test that verifies enforcement of the limit for glob pattern resolution (invalidGlobImport6.pkl).

Changes:
- Set a lower limit if test mode is enabled.
- Change static field to static method to prevent compile-time evaluation by native-image.
This commit is contained in:
translatenix
2024-10-15 22:20:25 -07:00
committed by GitHub
parent d00c466843
commit 475f29c896
2 changed files with 11 additions and 4 deletions

View File

@@ -79,9 +79,16 @@ public final class GlobResolver {
* a complex glob pattern can starve CPU/memory on a host.
*
* <p>Glob limit value taken from <a
* href="https://github.com/openbsd/src/commit/46df4fe576b7">https://github.com/openbsd/src/commit/46df4fe576b7</a>
* href="https://github.com/openbsd/src/commit/46df4fe576b7">https://github.com/openbsd/src/commit/46df4fe576b7</a>.
*
* <p>If test mode is enabled, a smaller value is used. This greatly speeds up the test that
* verifies enforcement of the limit (invalidGlobImport6.pkl).
*
* <p>Not a static field to prevent compile-time evaluation by native-image.
*/
private static final int MAX_LIST_ELEMENTS = 16384;
private static int maxListElements() {
return IoUtils.isTestMode() ? 512 : 16384;
}
private static final Map<String, Pattern> patterns =
Collections.synchronizedMap(new WeakHashMap<>());
@@ -338,7 +345,7 @@ public final class GlobResolver {
List<ResolvedGlobElement> result)
throws IOException, SecurityManagerException, InvalidGlobPatternException {
if (listElementCallCount.getAndIncrement() > MAX_LIST_ELEMENTS) {
if (listElementCallCount.getAndIncrement() > maxListElements()) {
throw new InvalidGlobPatternException(ErrorMessages.create("invalidGlobTooComplex"));
}
var elements = reader.listElements(securityManager, baseUri);

View File

@@ -531,7 +531,7 @@ public final class IoUtils {
return ServiceLoader.load(serviceClass, IoUtils.class.getClassLoader());
}
// not a static property to avoid compile-time evaluation by native-image
// not a static field to avoid compile-time evaluation by native-image
public static boolean isTestMode() {
return Boolean.getBoolean("org.pkl.testMode");
}