{"record":{"id":"733b44e9683a0663","repo":"oracle/graal","slug":"componentindex-must-be-non-negative-componentind","errorCode":null,"errorMessage":"componentIndex must be non-negative: <componentIndex>","messagePattern":"componentIndex must be non-negative: <componentIndex>","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdk/src/org.graalvm.home/src/org/graalvm/home/Version.java","lineNumber":362,"sourceCode":"     * Version components are indexed as follows:\n     * <ul>\n     * <li>{@code componentIndex = 0} represents the major version</li>\n     * <li>{@code componentIndex = 1} represents the minor version</li>\n     * <li>{@code componentIndex = 2} represents the update version</li>\n     * </ul>\n     * If the specified {@code componentIndex} exceeds the number of available components, the\n     * method returns {@code 0}. For example, a version {@code 23.1} will return {@code 0} for\n     * {@code componentIndex = 2}.\n     * </p>\n     *\n     * @param componentIndex the index of the version component to retrieve; must be non-negative\n     * @throws IllegalArgumentException if {@code componentIndex} is negative\n     * @since 24.2\n     * @see Version#create(int...)\n     */\n    public int getComponent(int componentIndex) {\n        if (componentIndex < 0) {\n            throw new IllegalArgumentException(\"componentIndex must be non-negative: \" + componentIndex);\n        }\n        return componentIndex < versions.length ? versions[componentIndex] : 0;\n    }\n\n    /**\n     * Parses a GraalVM version from its String raw format. Throws {@link IllegalArgumentException}\n     * if the passed string is not a valid GraalVM version.\n     *\n     * @since 19.3\n     */\n    public static Version parse(String versionString) throws IllegalArgumentException {\n        Objects.requireNonNull(versionString);\n        return new Version(versionString);\n    }\n\n    /**\n     * Constructs a new GraalVM version from a list of version numbers. The versions must not be\n     * <code>null</code> and none of the version numbers must be negative. At least one version","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/sdk/src/org.graalvm.home/src/org/graalvm/home/Version.java#L344-L380","documentation":"Version.getComponent(int componentIndex) validates its argument: a negative index throws IllegalArgumentException('componentIndex must be non-negative: <n>'). Indexes are 0-based over the version components (0 = major, 1 = minor, 2 = revision, ...); an index beyond the available components is legal and returns 0. Only negative values are rejected.","triggerScenarios":"Calling version.getComponent(-1) directly, or getComponent(i) in a loop where i starts at -1 or is decremented past zero; passing a component index parsed from user input or config without validation.","commonSituations":"Reverse iteration over version components; 'find last non-zero component' loops that walk downward and overshoot; exposing version component indices in a CLI/config API where users type -1 meaning 'last' (java.util.List semantics do not apply here).","solutions":["Clamp or validate the index before the call: if (i >= 0) v = version.getComponent(i).","For 'last component' semantics, compute it from version.toString()/compareTo data, not negative indices.","Validate externally supplied indices at the API boundary with a clear error message."],"exampleFix":"// before\nfor (int i = n; i >= -1; i--) {\n    int comp = version.getComponent(i); // i == -1 -> throws\n}\n\n// after\nfor (int i = n; i >= 0; i--) {\n    int comp = version.getComponent(i);\n}","handlingStrategy":"validation","validationCode":"static int component(Version v, int idx) {\n    return v.getComponent(Math.max(0, idx));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Loop over components with i >= 0 conditions only.","Validate user-supplied indices before calling getComponent.","Remember: no negative-index 'from end' semantics here."],"tags":["graalvm","home","version","validation"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}