Sanitise configuration values Make sure all the integer values are non-negative. If negative value is given, use the default. Change-Id: I5fd2547b7f0a87ba66ee356405bcdde36a23f276
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java b/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java index 270c5a4..ac844a2 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java +++ b/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
@@ -54,20 +54,21 @@ public CommitMessageLengthValidation(@GerritServerConfig Config gerritConfig) throws ConfigInvalidException, IOException { this.config = gerritConfig; - this.maxSubjectLength = config.getInt( - COMMIT_MESSAGE_SECTION, null, + this.maxSubjectLength = nonNegativeInt( MAX_SUBJECT_LENGTH_KEY, DEFAULT_MAX_SUBJECT_LENGTH); - this.maxLineLength = config.getInt( - COMMIT_MESSAGE_SECTION, null, + this.maxLineLength = nonNegativeInt( MAX_LINE_LENGTH_KEY, DEFAULT_MAX_LINE_LENGTH); this.rejectTooLong = config.getBoolean( - COMMIT_MESSAGE_SECTION, REJECT_TOO_LONG_KEY, - DEFAULT_REJECT_TOO_LONG); - this.longLinesThreshold = config.getInt( - COMMIT_MESSAGE_SECTION, null, + COMMIT_MESSAGE_SECTION, REJECT_TOO_LONG_KEY, DEFAULT_REJECT_TOO_LONG); + this.longLinesThreshold = nonNegativeInt( LONG_LINES_THRESHOLD_KEY, DEFAULT_LONG_LINES_THRESHOLD); } + private int nonNegativeInt(String name, int defaultValue) { + int value = this.config.getInt(COMMIT_MESSAGE_SECTION, null, name, defaultValue); + return value >= 0 ? value : defaultValue; + } + private void onLineTooLong(final AbbreviatedObjectId id, List<CommitValidationMessage> messagesList, final String errorMessage) throws CommitValidationException {
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md index 0bf0c9b..b33ce93 100644 --- a/src/main/resources/Documentation/config.md +++ b/src/main/resources/Documentation/config.md
@@ -5,17 +5,17 @@ configured in the standard Gerrit config file `gerrit.config`. commitmessage.maxSubjectLength -: Maximum length of the commit message's subject line. If - not specified, defaults to 65. +: Maximum length of the commit message's subject line. Defaults + to 65 if not specified or less than 0. commitmessage.maxLineLength -: Maximum length of a line in the commit message's body. If - not specified, defaults to 70. +: Maximum length of a line in the commit message's body. Defaults + to 70 if not specified or less than 0. commitmessage.longLinesThreshold : Percentage of commit message lines allowed to exceed the - maximum length before a warning or error is generated. If - not specified, defaults to 33. + maximum length before a warning or error is generated. Defaults + to 33 if not specified or less than 0. commitmessage.rejectTooLong : If set to `true`, reject commits whose subject or line