{"record":{"id":"ff3d1b5dbf2f0911","repo":"apache/cassandra","slug":"invalid-duration-value-must-be-non-negative","errorCode":null,"errorMessage":"Invalid duration: value must be non-negative","messagePattern":"Invalid duration: value must be non-negative","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/config/DurationSpec.java","lineNumber":120,"sourceCode":"    private static String acceptedUnits(TimeUnit minUnit)\n    {\n        TimeUnit[] units = TimeUnit.values();\n        return Arrays.toString(Arrays.copyOfRange(units, minUnit.ordinal(), units.length));\n    }\n\n    private static void validateQuantity(String value, long quantity, TimeUnit sourceUnit, TimeUnit minUnit, long max)\n    {\n        // no need to validate for negatives as they are not allowed at first place from the regex\n\n        if (minUnit.convert(quantity, sourceUnit) >= max)\n            throw new IllegalArgumentException(\"Invalid duration: \" + value + \". It shouldn't be more than \" +\n                                             (max - 1) + \" in \" + toLowerCaseLocalized(minUnit.name()));\n    }\n\n    private static void validateQuantity(long quantity, TimeUnit sourceUnit, TimeUnit minUnit, long max)\n    {\n        if (quantity < 0)\n            throw new IllegalArgumentException(\"Invalid duration: value must be non-negative\");\n\n        if (minUnit.convert(quantity, sourceUnit) >= max)\n            throw new IllegalArgumentException(String.format(\"Invalid duration: %d %s. It shouldn't be more than %d in %s\",\n                                                           quantity, toLowerCaseLocalized(sourceUnit.name()),\n                                                           max - 1, toLowerCaseLocalized(minUnit.name())));\n    }\n\n    // get vs no-get prefix is not consistent in the code base, but for classes involved with config parsing, it is\n    // imporant to be explicit about get/set as this changes how parsing is done; this class is a data-type, so is\n    // not nested, having get/set can confuse parsing thinking this is a nested type\n    public long quantity()\n    {\n        return quantity;\n    }\n\n    public TimeUnit unit()\n    {\n        return unit;","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/config/DurationSpec.java#L102-L138","documentation":"The numeric validateQuantity overload rejects negative quantities for duration specs. Durations in Cassandra config must be non-negative; -1 is not a valid disable marker here. Thrown as IllegalArgumentException.","triggerScenarios":"Calling a DurationSpec subclass constructor or setter with a negative long, e.g. new SomeDurationSpec(-1, TimeUnit.SECONDS) or programmatically assigning a negative value via a config mutation API.","commonSituations":"Using -1 to mean 'infinite/disabled' in code that programmatically builds config; sign errors when computing durations dynamically.","solutions":["Replace negative values with 0 (or the property's documented disable value)","Clamp computed durations with Math.max(0, value) before assignment","Use the property's documented mechanism for disabling instead of a negative duration"],"exampleFix":"// before\nspec.set(retryDelaySeconds);\n// after\nspec.set(Math.max(0, retryDelaySeconds));","handlingStrategy":"validation","validationCode":"static long requireNonNegative(long v) {\n    if (v < 0) throw new IllegalArgumentException(\"Duration must be non-negative\");\n    return v;\n}","typeGuard":null,"tryCatchPattern":"try {\n    spec.set(value);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"non-negative\"))\n        logger.error(\"Negative duration passed: {}\", value);\n}","preventionTips":["Never use -1 as a duration; use documented disable values","Clamp computed durations with Math.max(0, x)","Fuzz/CI test programmatic config builders for sign errors"],"tags":["configuration","duration","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}