{"record":{"id":"5e4b62469ee8d79b","repo":"alibaba/COLA","slug":"size-must-be-greater-than-zero","errorCode":null,"errorMessage":"size must be greater than zero.","messagePattern":"size must be greater than zero\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"cola-components/cola-component-job/src/main/java/com/alibaba/cola/job/UuidGenerator.java","lineNumber":69,"sourceCode":"        public static final SecureRandom DEFAULT_NUMBER_GENERATOR = new SecureRandom();\n        public static final char[] DEFAULT_ALPHABET = \"_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\".toCharArray();\n        public static final int DEFAULT_SIZE = 21;\n\n        private NanoIdUtils() {\n        }\n\n        public static String randomNanoId() {\n            return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, 21);\n        }\n\n        public static String randomNanoId(Random random, char[] alphabet, int size) {\n            if (random == null) {\n                throw new IllegalArgumentException(\"random cannot be null.\");\n            } else if (alphabet == null) {\n                throw new IllegalArgumentException(\"alphabet cannot be null.\");\n            } else if (alphabet.length != 0 && alphabet.length < 256) {\n                if (size <= 0) {\n                    throw new IllegalArgumentException(\"size must be greater than zero.\");\n                } else {\n                    int mask = (2 << (int)Math.floor(Math.log((double)(alphabet.length - 1)) / Math.log(2.0D))) - 1;\n                    int step = (int)Math.ceil(1.6D * (double)mask * (double)size / (double)alphabet.length);\n                    StringBuilder idBuilder = new StringBuilder();\n\n                    while(true) {\n                        byte[] bytes = new byte[step];\n                        random.nextBytes(bytes);\n\n                        for(int i = 0; i < step; ++i) {\n                            int alphabetIndex = bytes[i] & mask;\n                            if (alphabetIndex < alphabet.length) {\n                                idBuilder.append(alphabet[alphabetIndex]);\n                                if (idBuilder.length() == size) {\n                                    return idBuilder.toString();\n                                }\n                            }\n                        }","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/alibaba/COLA/blob/352e1a867538d9cd40e1b11e4028fa652fc97557/cola-components/cola-component-job/src/main/java/com/alibaba/cola/job/UuidGenerator.java#L51-L87","documentation":"Argument-validation guard inside NanoIdUtils.randomNanoId: the requested id length (size parameter) was zero or negative, so the generator loop could never produce any characters. It fires when a caller passes a non-positive size to the full overload; the default path uses size 21 and cannot trigger it.","triggerScenarios":"Calling randomNanoId(random, alphabet, 0) or a negative size, often size coming from a computed or configured value.","commonSituations":"A config value of 0 treated as 'use default' by the caller but not by the library; int arithmetic yielding 0 or negative; passing length of an empty string.","solutions":["Pass size > 0 (the library default is 21)","Clamp or default invalid configured lengths before calling: size = size > 0 ? size : 21","Fix the computation that produced a non-positive size"],"exampleFix":"// before\nint size = config.getSize(); // could be 0\nString id = randomNanoId(random, alphabet, size);\n// after\nint size = Math.max(1, config.getSize() > 0 ? config.getSize() : 21);\nString id = randomNanoId(random, alphabet, size);","handlingStrategy":"validation","validationCode":"if (size <= 0) {\n    throw new IllegalArgumentException(\"size must be positive, got \" + size);\n}","typeGuard":"int sanitizedSize(int requested) {\n    return requested > 0 ? requested : 21;\n}","tryCatchPattern":"try {\n    id = UuidGenerator.randomNanoId(random, alphabet, size);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"size\")) {\n        id = UuidGenerator.randomNanoId(random, alphabet, 21);\n    } else throw e;\n}","preventionTips":["Validate configured id-length values at startup","Clamp computed sizes with Math.max(1, n)","Document that 0 is not a 'default' sentinel for this API"],"tags":["java","uuid","argument-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"352e1a867538d9cd40e1b11e4028fa652fc97557","analyzedAt":"2026-09-08T00:46:23.972Z","contentChangedAt":"2026-09-08T00:46:23.972Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}