{"record":{"id":"ace20d5a597f5416","repo":"iBotPeaches/Apktool","slug":"attribute-index-out-of-range-index-s-length-s","errorCode":null,"errorMessage":"Attribute index out of range: index=%s, length=%s","messagePattern":"Attribute index out of range: index=(.+?), length=(.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/BinaryXmlResourceParser.java","lineNumber":585,"sourceCode":"    public int nextTag() throws XmlPullParserException, IOException {\n        int eventType = next();\n        if (eventType == TEXT && isWhitespace()) {\n            eventType = next();\n        }\n        if (eventType != START_TAG && eventType != END_TAG) {\n            throw new XmlPullParserException(\"Expected start or end tag.\", this, null);\n        }\n        return eventType;\n    }\n\n    // Utility methods\n\n    private Attribute getAttribute(int index) {\n        if (mEventType != START_TAG) {\n            throw new IndexOutOfBoundsException(\"Parser must be on START_TAG to get attributes.\");\n        }\n        if (mAttributes == null || index < 0 || index >= mAttributes.length) {\n            throw new IndexOutOfBoundsException(\n                String.format(\"Attribute index out of range: index=%s, length=%s\",\n                    index, mAttributes != null ? mAttributes.length : 0));\n        }\n        return mAttributes[index];\n    }\n\n    private ResId getAttributeNameResourceId(int index) {\n        if (mResourceMap == null) {\n            return ResId.NULL;\n        }\n        Attribute attr = getAttribute(index);\n        if (attr == null || attr.name < 0 || attr.name >= mResourceMap.length) {\n            return ResId.NULL;\n        }\n        return mResourceMap[attr.name];\n    }\n\n    private void reset() {","sourceCodeStart":567,"sourceCodeEnd":603,"githubUrl":"https://github.com/iBotPeaches/Apktool/blob/79b63384d7d7e22917e6ea8b453272da7012515b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/BinaryXmlResourceParser.java#L567-L603","documentation":"getAttribute(int) throws IndexOutOfBoundsException when attribute lookup happens off a START_TAG event, when the parser has no attributes array, or when the requested index is negative / >= attribute count. The formatted message includes the offending index and actual length.","triggerScenarios":"Calling getAttributeValue(index)/getAttributeName(index) outside START_TAG, or with a hardcoded index larger than this element's attribute count (binary XML attributes vary per element; indexes are not stable across tags).","commonSituations":"Copying attribute-index code between elements assuming fixed ordering; iterating attributes of one tag while the parser advanced to the next; AXML where some elements legitimately have fewer attributes.","solutions":["Always iterate with `for (int i = 0; i < parser.getAttributeCount(); i++)` instead of hardcoded indexes.","Prefer name-based lookup getAttributeValue(namespace, name) which is order-independent.","Ensure the parser is on START_TAG (check getEventType()) before attribute access.","Log getAttributeCount() for the failing element to see the real length."],"exampleFix":"// before\nString value = parser.getAttributeValue(2); // may not exist\n\n// after\nString value = parser.getAttributeValue(\"http://schemas.android.com/apk/res/android\", \"name\");\n// or iterate:\nfor (int i = 0; i < parser.getAttributeCount(); i++) {\n    System.out.println(parser.getAttributeName(i) + \"=\" + parser.getAttributeValue(i));\n}","handlingStrategy":"validation","validationCode":"if (parser.getEventType() != XmlPullParser.START_TAG) throw new IllegalStateException(\"attributes need START_TAG\");\nint n = parser.getAttributeCount();\nfor (int i = 0; i < n; i++) { /* use i, never a hardcoded index */ }","typeGuard":null,"tryCatchPattern":"String v = index < parser.getAttributeCount() ? parser.getAttributeValue(index) : null;\nif (v == null) v = parser.getAttributeValue(namespace, name); // name-based fallback","preventionTips":["Prefer namespace+name attribute lookup over positional indexes.","Always iterate with getAttributeCount() as the bound.","Access attributes only while on START_TAG."],"tags":["apktool","xml-pull-parser","attributes","axml"],"backgroundTag":null,"analyzedSha":"79b63384d7d7e22917e6ea8b453272da7012515b","analyzedAt":"2026-08-14T10:43:28.812Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}