{"id":"b78bf6245e130d40","repo":"spring-projects/spring-framework","slug":"advisor-index-index-is-out-of-bounds-this-confi","errorCode":null,"errorMessage":"Advisor index {index} is out of bounds: This configuration only has {advisors.size()} advisors.","messagePattern":"Advisor index (.+?) is out of bounds: This configuration only has (.+?) advisors\\.","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java","lineNumber":333,"sourceCode":"\t@Override\n\tpublic boolean removeAdvisor(Advisor advisor) {\n\t\tint index = indexOf(advisor);\n\t\tif (index == -1) {\n\t\t\treturn false;\n\t\t}\n\t\telse {\n\t\t\tremoveAdvisor(index);\n\t\t\treturn true;\n\t\t}\n\t}\n\n\t@Override\n\tpublic void removeAdvisor(int index) throws AopConfigException {\n\t\tif (isFrozen()) {\n\t\t\tthrow new AopConfigException(\"Cannot remove Advisor: Configuration is frozen.\");\n\t\t}\n\t\tif (index < 0 || index > this.advisors.size() - 1) {\n\t\t\tthrow new AopConfigException(\"Advisor index \" + index + \" is out of bounds: \" +\n\t\t\t\t\t\"This configuration only has \" + this.advisors.size() + \" advisors.\");\n\t\t}\n\n\t\tAdvisor advisor = this.advisors.remove(index);\n\t\tif (advisor instanceof IntroductionAdvisor introductionAdvisor) {\n\t\t\t// We need to remove introduction interfaces.\n\t\t\tfor (Class<?> ifc : introductionAdvisor.getInterfaces()) {\n\t\t\t\tremoveInterface(ifc);\n\t\t\t}\n\t\t}\n\n\t\tadviceChanged();\n\t}\n\n\t@Override\n\tpublic int indexOf(Advisor advisor) {\n\t\tAssert.notNull(advisor, \"Advisor must not be null\");\n\t\treturn this.advisors.indexOf(advisor);","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java#L315-L351","documentation":"Thrown by AdvisedSupport.removeAdvisor(int) when the supplied index is outside [0, advisors.size()-1]. Spring guards array bounds here to produce a descriptive AopConfigException rather than an IndexOutOfBoundsException deep in the ArrayList. It runs only after the frozen check, so this is a pure input-validation error on a mutable config.","triggerScenarios":"Calling removeAdvisor(n) where n is negative, equals size, or exceeds size; computing an index from indexOf() but acting on a stale advisor list; loop off-by-one when iterating advisors and removing by position.","commonSituations":"Caching an advisor index and later removing by that index after the list changed; off-by-one errors in cleanup loops; calling removeAdvisor(advisors.size()) expecting 'remove last'.","solutions":["Check 0 <= index < advised.getAdvisors().length immediately before calling removeAdvisor(index).","Prefer removeAdvisor(theAdvisorInstance) over index-based removal to avoid stale indices.","When removing in a loop, iterate backwards or rebuild the advisor list rather than shifting indices."],"exampleFix":"// before\nAdvisor[] advisors = ((Advised) proxy).getAdvisors();\n((Advised) proxy).removeAdvisor(advisors.length);  // off-by-one\n\n// after\nAdvisor[] advisors = ((Advised) proxy).getAdvisors();\nif (advisors.length > 0) {\n  ((Advised) proxy).removeAdvisor(advisors.length - 1);\n}","handlingStrategy":"validation","validationCode":"Advised advised = (Advised) proxy;\nint index = ...;\nif (index < 0 || index >= advised.getAdvisors().length) {\n  throw new IndexOutOfBoundsException(\n    \"advisor index \" + index + \" out of bounds for \" + advised.getAdvisors().length);\n}\nadvised.removeAdvisor(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Recompute advisors.length immediately before index-based removal.","Prefer instance-based removeAdvisor(Advisor) over positional removal.","When removing in a loop, iterate from the end to keep indices stable."],"tags":["spring-aop","bounds-check","advisor-mutation","validation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}