{"record":{"id":"b351a322bfcd1205","repo":"apache/cassandra","slug":"maxdevs-must-be-greater-than-or-equal-to-zero","errorCode":null,"errorMessage":"maxdevs must be greater than or equal to zero","messagePattern":"maxdevs must be greater than or equal to zero","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/HistogramBuilder.java","lineNumber":73,"sourceCode":"    {\n        return buildWithStdevRangesAroundMean(3);\n    }\n\n    /**\n     * Calculate the min, mean, max and standard deviation of the items in the builder, and\n     * generate an EstimatedHistogram with upto <code>maxdev</code> stdev size ranges  either\n     * side of the mean, until min/max are hit; if either min/max are not reached a further range is\n     * inserted at the relevant ends. e.g., with a <code>maxdevs</code> of 3, there may be <i>up to</i> 8 ranges\n     * (between 9 boundaries, the middle being the mean); the middle 6 will have the same size (stdev)\n     * with the outermost two stretching out to min and max.\n     *\n     * @param maxdevs\n     * @return\n     */\n    public EstimatedHistogram buildWithStdevRangesAroundMean(int maxdevs)\n    {\n        if (maxdevs < 0)\n            throw new IllegalArgumentException(\"maxdevs must be greater than or equal to zero\");\n\n        final int count = this.count;\n        final long[] values = this.values;\n\n        if (count == 0)\n            return new EstimatedHistogram(EMPTY_LONG_ARRAY, ZERO);\n\n        long min = Long.MAX_VALUE, max = Long.MIN_VALUE;\n        double sum = 0, sumsq = 0;\n        for (int i = 0 ; i < count ; i++)\n        {\n            final long value = values[i];\n            sum += value;\n            sumsq += value * value;\n            if (value < min)\n                min = value;\n            if (value > max)\n                max = value;","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/HistogramBuilder.java#L55-L91","documentation":"HistogramBuilder.buildWithStdevRangesAroundMean(maxdevs) builds an EstimatedHistogram covering up to maxdevs standard deviations around the mean. A negative maxdevs is meaningless, so it throws IllegalArgumentException immediately.","triggerScenarios":"Calling buildWithStdevRangesAroundMean with a negative int, typically from an unvalidated config value or a computed value that can go negative.","commonSituations":"Configured deviation count read from properties/yaml without validation; arithmetic like (max - min) gone negative; off-by-sign bugs in tooling code.","solutions":["Pass a non-negative maxdevs (usually a small positive integer like 2 or 3)","Clamp negative values with Math.max(0, maxdevs)","Validate the configuration value where it is parsed"],"exampleFix":"// before\nEstimatedHistogram h = builder.buildWithStdevRangesAroundMean(cfg.deviationCount);\n// after\nEstimatedHistogram h = builder.buildWithStdevRangesAroundMean(Math.max(0, cfg.deviationCount));","handlingStrategy":"validation","validationCode":"if (maxdevs < 0) throw new IllegalArgumentException(\"maxdevs must be >= 0\");","typeGuard":null,"tryCatchPattern":"try { h = builder.buildWithStdevRangesAroundMean(maxdevs); } catch (IllegalArgumentException e) { h = builder.buildWithStdevRangesAroundMean(0); }","preventionTips":["Validate numeric config values where they are parsed, not where used","Clamp computed range values with Math.max(0, x)","Document expected ranges for histogram tuning parameters"],"tags":["histogram","argument-validation","illegal-argument"],"backgroundTag":"argument-out-of-range","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}