{"record":{"id":"a68e652846af6e7b","repo":"theonedev/onedev","slug":"max-count-must-be-non-negative","errorCode":null,"errorMessage":"Max count must be non-negative.","messagePattern":"Max count must be non-negative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/org/eclipse/jgit/revwalk/filter/MaxCountRevFilter.java","lineNumber":39,"sourceCode":"/**\n * Limits the number of commits output.\n */\npublic class MaxCountRevFilter extends RevFilter {\n\n\tprivate int maxCount;\n\n\tprivate int count;\n\n\t/**\n\t * Create a new max count filter.\n\t *\n\t * @param maxCount\n\t *            the limit\n\t * @return a new filter\n\t */\n\tpublic static RevFilter create(int maxCount) {\n\t\tif (maxCount < 0)\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\tJGitText.get().maxCountMustBeNonNegative);\n\t\treturn new MaxCountRevFilter(maxCount);\n\t}\n\n\tprivate MaxCountRevFilter(int maxCount) {\n\t\tthis.count = 0;\n\t\tthis.maxCount = maxCount;\n\t}\n\n\t@Override\n\tpublic boolean include(RevWalk walker, RevCommit cmit)\n\t\t\tthrows StopWalkException, MissingObjectException,\n\t\t\tIncorrectObjectTypeException, IOException {\n\t\tcount++;\n\t\tif (count > maxCount)\n\t\t\tthrow StopWalkException.INSTANCE;\n\t\treturn true;\n\t}","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/org/eclipse/jgit/revwalk/filter/MaxCountRevFilter.java#L21-L57","documentation":"MaxCountRevFilter.create(int) throws IllegalArgumentException when maxCount is negative. A negative limit has no meaning for 'stop after N commits', so JGit validates the argument before constructing the filter.","triggerScenarios":"Calling MaxCountRevFilter.create(-1) or any negative value — usually from an unparsed/unvalidated limit parameter or an off-by-one sentinel value.","commonSituations":"CLI/config limits parsed with Integer.parseInt without range checking; sentinel -1 meaning 'unlimited' passed where a non-negative count is expected (use a different mechanism for unlimited).","solutions":["Clamp or validate the limit before calling create: reject or coerce negatives to 0/unlimited semantics","Parse numeric config with explicit range checks (>= 0)","Handle 'unlimited' as absence of the filter rather than a negative count"],"exampleFix":"// before\nRevFilter f = MaxCountRevFilter.create(userLimit);\n// after\nint limit = Math.max(0, userLimit); // or validate and reject negatives\nRevFilter f = MaxCountRevFilter.create(limit);","handlingStrategy":"validation","validationCode":"if (maxCount < 0) {\n    throw new IllegalArgumentException(\"maxCount must be >= 0, got \" + maxCount);\n}","typeGuard":"boolean validMaxCount(int n) { return n >= 0; }","tryCatchPattern":"try {\n    RevFilter f = MaxCountRevFilter.create(maxCount);\n} catch (IllegalArgumentException e) {\n    RevFilter f = RevFilter.NONE; // or coerce negative to 0 / unlimited handling\n}","preventionTips":["Range-check parsed numeric limits (>= 0) at parse time","Use Optional/absence for 'unlimited' instead of negative sentinels like -1","Add unit tests for limit values -1, 0, and large values"],"tags":["jgit","revfilter","illegal-argument","range-check"],"backgroundTag":"argument-out-of-range","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}