{"record":{"id":"7fc22d442c1acb84","repo":"apache/cassandra","slug":"argument-for-top-must-be-a-positive-integer","errorCode":null,"errorMessage":"argument for top must be a positive integer.","messagePattern":"argument for top must be a positive integer\\.","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/tools/nodetool/TableStats.java","lineNumber":107,"sourceCode":"        if (!outputFormat.isEmpty() && !\"json\".equals(outputFormat) && !\"yaml\".equals(outputFormat))\n        {\n            throw new IllegalArgumentException(\"arguments for -F are json,yaml only.\");\n        }\n\n        if (!sortKey.isEmpty() && !Arrays.asList(StatsTableComparator.supportedSortKeys).contains(sortKey))\n        {\n            throw new IllegalArgumentException(String.format(\"argument for sort must be one of: %s\",\n                                               String.join(\", \", StatsTableComparator.supportedSortKeys)));\n        }\n\n        if (top > 0 && sortKey.isEmpty())\n        {\n            throw new IllegalArgumentException(\"cannot filter top K tables without specifying a sort key.\");\n        }\n\n        if (top < 0)\n        {\n            throw new IllegalArgumentException(\"argument for top must be a positive integer.\");\n        }\n\n        StatsHolder holder = new TableStatsHolder(probe, humanReadable, ignore, tableNames, sortKey, top, locationCheck);\n        // print out the keyspace and table statistics\n        StatsPrinter printer = TableStatsPrinter.from(outputFormat, !sortKey.isEmpty());\n        printer.print(holder, probe.output().out);\n    }\n\n}\n","sourceCodeStart":89,"sourceCodeEnd":117,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/tools/nodetool/TableStats.java#L89-L117","documentation":"TableStats validates that the --top/-t argument is not negative; a negative value throws IllegalArgumentException saying it must be a positive integer. (Note the check is `top < 0`, so zero means 'no limit' and is accepted.)","triggerScenarios":"`nodetool tablestats -t -3` or scripts interpolating an unvalidated negative variable into the --top flag.","commonSituations":"Shell scripts computing a limit from upstream data that can be negative; fat-fingered CLI invocations; misuse of 0/-1 as 'unlimited' sentinel where only 0 is valid here.","solutions":["Pass a positive integer, or omit --top entirely.","Use --top 0 if the tooling requires an explicit 'no limit' value.","Sanitize the variable in the calling script before interpolation."],"exampleFix":"// before\nnodetool tablestats -s read_latency_ms -t -1\n// after\nnodetool tablestats -s read_latency_ms -t 10","handlingStrategy":"validation","validationCode":"if (top < 0) throw new IllegalArgumentException(\"top must be >= 0\");","typeGuard":null,"tryCatchPattern":"try { runNodetool(\"tablestats\", \"-t\", String.valueOf(top)); }\ncatch (IllegalArgumentException e) { if (e.getMessage().contains(\"top must be a positive\")) top = 0; else throw e; }","preventionTips":["Clamp or sanitize numeric CLI inputs: top = Math.max(0, requestedTop).","Avoid -1 as an 'unlimited' sentinel; use 0 or omit the flag.","Validate variables interpolated into shell commands before execution."],"tags":["nodetool","cli","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-17T15:17:12.973Z"}