Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.rules; 16 : 17 : import org.eclipse.jgit.lib.Config; 18 : 19 : /** Provides utility methods for configuring and running Prolog rules inside Gerrit. */ 20 0 : public class RuleUtil { 21 : 22 : /** 23 : * Returns the reduction limit to be applied to the Prolog machine to prevent infinite loops and 24 : * other forms of computational overflow. 25 : */ 26 : public static int reductionLimit(Config gerritConfig) { 27 147 : int limit = gerritConfig.getInt("rules", null, "reductionLimit", 100000); 28 147 : return limit <= 0 ? Integer.MAX_VALUE : limit; 29 : } 30 : 31 : /** 32 : * Returns the compile reduction limit to be applied to the Prolog machine to prevent infinite 33 : * loops and other forms of computational overflow. The compiled reduction limit should be used 34 : * when user-provided Prolog code is compiled by the interpreter before the limit gets applied. 35 : */ 36 : public static int compileReductionLimit(Config gerritConfig) { 37 147 : int limit = 38 147 : gerritConfig.getInt( 39 : "rules", 40 : null, 41 : "compileReductionLimit", 42 147 : (int) Math.min(10L * reductionLimit(gerritConfig), Integer.MAX_VALUE)); 43 147 : return limit <= 0 ? Integer.MAX_VALUE : limit; 44 : } 45 : }