{"record":{"id":"7e4add0ddc356786","repo":"NationalSecurityAgency/ghidra","slug":"length-override-of-newlength-conflicts-with-code","errorCode":null,"errorMessage":"Length override of {newLength} conflicts with code unit at {cu.getMinAddress}, lifespan={cu.getLifespan}","messagePattern":"Length override of (.+?) conflicts with code unit at (.+?), lifespan=(.+?)","errorType":"exception","errorClass":"CodeUnitInsertionException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceInstruction.java","lineNumber":596,"sourceCode":"\tpublic void setLengthOverride(int length) throws CodeUnitInsertionException {\n\t\tint oldLengthOverride = this.lengthOverride;\n\t\ttry (LockHold hold = space.trace.lockWrite()) {\n\t\t\tcheckDeleted();\n\t\t\tInstructionPrototype proto = getPrototype();\n\t\t\tlength = InstructionDB.checkLengthOverride(length, proto);\n\t\t\tif (length == lengthOverride) {\n\t\t\t\treturn; // no change\n\t\t\t}\n\n\t\t\tint newLength = length != 0 ? length : proto.getLength();\n\t\t\tif (newLength > getLength()) {\n\t\t\t\tAddress minAddr = getMinAddress();\n\t\t\t\tAddress newEndAddr = minAddr.add(newLength - 1);\n\t\t\t\tTraceAddressSnapRange tasr = new ImmutableTraceAddressSnapRange(\n\t\t\t\t\tnew AddressRangeImpl(minAddr.next(), newEndAddr), getLifespan());\n\t\t\t\tfor (AbstractDBTraceCodeUnit<?> cu : space.definedUnits.getIntersecting(tasr)) {\n\t\t\t\t\tif (cu != this) {\n\t\t\t\t\t\tthrow new CodeUnitInsertionException(\n\t\t\t\t\t\t\t\"Length override of \" + newLength + \" conflicts with code unit at \" +\n\t\t\t\t\t\t\t\tcu.getMinAddress() + \", lifespan=\" + cu.getLifespan());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tupdateLengthOverride(length);\n\t\t}\n\t\tspace.trace.setChanged(new TraceChangeRecord<>(\n\t\t\tTraceEvents.INSTRUCTION_LENGTH_OVERRIDE_CHANGED, space.space, this, oldLengthOverride,\n\t\t\tlength));\n\t}\n\n\tprivate void updateLengthOverride(int length) {\n\t\tflags &= LENGTH_OVERRIDE_CLEAR_MASK;\n\t\tflags |= (length << LENGTH_OVERRIDE_SHIFT);\n\t\tlengthOverride = length;\n\t\tupdate(FLAGS_COLUMN);","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceInstruction.java#L578-L614","documentation":"Thrown by DBTraceInstruction when setting a length override that would cause the instruction's expanded range to collide with an adjacent defined code unit. When the new length (newLength) exceeds the current unit length, the method checks the extended range [minAddr.next(), newEndAddr] for any other defined unit at the same lifespan. If one is found, the override is rejected because it would overlap that unit. This prevents silent corruption of the code listing from length overrides.","triggerScenarios":"Calling instruction.setLengthOverride(newLength) (or equivalent) where newLength > current length and another instruction or data unit occupies the bytes the instruction would grow into, within the same lifespan. The method iterates space.definedUnits.getIntersecting(tasr) and throws on the first foreign unit.","commonSituations":"Overriding instruction length to disassemble a longer encoding when the adjacent bytes already have a defined unit; conflict between user overrides and auto-disassembly results; stale code units from a previous disassembly pass blocking the override.","solutions":["Delete the conflicting adjacent code unit before setting the override: clear the range [addr+1, addr+newLength-1].","Set a smaller length override that does not collide with the neighbor.","Clear and re-disassemble the region first, then apply the override.","Shrink the conflicting unit's lifespan so it no longer overlaps."],"exampleFix":"// before\ninstruction.setLengthOverride(8); // adjacent unit at addr+4 blocks\n\n// after — clear the conflicting range first\nCodeUnit neighbor = space.definedUnits.getAt(snap, instruction.getMinAddress().add(4));\nif (neighbor != null) {\n    neighbor.delete();\n}\ninstruction.setLengthOverride(8);","handlingStrategy":"validation","validationCode":"// Before setting a length override, check for adjacent conflicts\nint newLength = desiredLength;\nif (newLength > instruction.getLength()) {\n    Address minAddr = instruction.getMinAddress();\n    Address newEndAddr = minAddr.add(newLength - 1);\n    TraceAddressSnapRange tasr = new ImmutableTraceAddressSnapRange(\n        new AddressRangeImpl(minAddr.next(), newEndAddr), instruction.getLifespan());\n    for (CodeUnit cu : space.definedUnits.getIntersecting(tasr)) {\n        if (cu != instruction) {\n            // Conflict — clear it first\n            cu.delete();\n        }\n    }\n}\ninstruction.setLengthOverride(newLength);","typeGuard":null,"tryCatchPattern":"try {\n    instruction.setLengthOverride(newLength);\n} catch (CodeUnitInsertionException e) {\n    if (e.getMessage().startsWith(\"Length override\") && e.getMessage().contains(\"conflicts\")) {\n        // Clear the conflicting adjacent unit and retry\n        Address minAddr = instruction.getMinAddress();\n        for (CodeUnit cu : space.definedUnits.getIntersecting(\n                new ImmutableTraceAddressSnapRange(\n                    new AddressRangeImpl(minAddr.next(), minAddr.add(newLength - 1)),\n                    instruction.getLifespan()))) {\n            if (cu != instruction) cu.delete();\n        }\n        instruction.setLengthOverride(newLength);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Check space.definedUnits.getIntersecting() for the extended range before setting length overrides.","Clear adjacent units that would be overlapped by the larger instruction encoding.","Clear and re-disassemble the region before applying overrides to avoid stale unit conflicts."],"tags":["instruction","length-override","overlap","code-unit","trace-modeling"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}