{"record":{"id":"fe5532a6aee870db","repo":"NationalSecurityAgency/ghidra","slug":"code-units-cannot-overlap-fe5532","errorCode":null,"errorMessage":"Code units cannot overlap","messagePattern":"Code units cannot overlap","errorType":"exception","errorClass":"CodeUnitInsertionException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceDefinedDataView.java","lineNumber":86,"sourceCode":"\t\treturn false;\n\t}\n\n\t@Override\n\tpublic DBTraceDataAdapter create(Lifespan lifespan, Address address, TracePlatform platform,\n\t\t\tDataType origType, int origLength) throws CodeUnitInsertionException {\n\t\tif (platform.getTrace() != getTrace() ||\n\t\t\t!(platform instanceof InternalTracePlatform iPlatform)) {\n\t\t\tthrow new IllegalArgumentException(\"Platform is not part of this trace\");\n\t\t}\n\t\ttry (LockHold hold = LockHold.lock(space.lock.writeLock())) {\n\t\t\tDBTraceMemorySpace memSpace = space.trace.getMemoryManager().get(space.space, true);\n\t\t\t// NOTE: User-given length could be ignored....\n\t\t\t// Check start address first. After I know length, I can check for other existing units\n\t\t\tlong startSnap = lifespan.lmin();\n\t\t\tif (!space.undefinedData.coversRange(Lifespan.at(startSnap),\n\t\t\t\tnew AddressRangeImpl(address, address))) {\n\t\t\t\t// TODO: Figure out the conflicting unit?\n\t\t\t\tthrow new CodeUnitInsertionException(\"Code units cannot overlap\");\n\t\t\t}\n\n\t\t\tDataType dataType;\n\t\t\tint length;\n\t\t\tif (origType instanceof FactoryDataType) {\n\t\t\t\tMemBuffer buffer = memSpace.getBufferAt(startSnap, address);\n\t\t\t\tFactoryDataType fdt = (FactoryDataType) origType;\n\t\t\t\tdataType = fdt.getDataType(buffer);\n\t\t\t\tlength = -1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tdataType = origType;\n\t\t\t\tlength = origLength;\n\t\t\t}\n\n\t\t\tif (dataType == null) {\n\t\t\t\tthrow new CodeUnitInsertionException(\"Failed to resolve data type\");\n\t\t\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceDefinedDataView.java#L68-L104","documentation":"Thrown early in DBTraceDefinedDataView.create() when the start address is already occupied by a defined unit at the requested startSnap — the undefinedData view does not cover the single-byte range at (startSnap, address). This is the initial overlap check performed before data type resolution; it catches the case where another instruction or data unit already exists at the exact start address before any type processing happens.","triggerScenarios":"Calling view.create() at an address+snap where an existing code unit (instruction or data) already starts or covers that address. The check space.undefinedData.coversRange(Lifespan.at(startSnap), [address, address]) fails because the address is already defined.","commonSituations":"Creating data at an address that already has an instruction; creating overlapping data units; not clearing old units before re-annotating; script that iterates and creates data without checking for conflicts.","solutions":["Delete the existing code unit at that address+snap before creating: codeUnit.delete() or disassembleClear().","Use a different address or snapshot range.","Check view.getContaining(startSnap, address) before creating and handle the conflict."],"exampleFix":"// before\nview.create(Lifespan.span(0, 10), address, platform, dataType); // address already defined\n\n// after — clear existing unit first\nCodeUnit existing = view.getContaining(0, address);\nif (existing != null) {\n    existing.delete();\n}\nview.create(Lifespan.span(0, 10), address, platform, dataType);","handlingStrategy":"validation","validationCode":"// Check if the start address is already defined before creating\nlong startSnap = lifespan.lmin();\nif (!space.undefinedData.coversRange(Lifespan.at(startSnap),\n        new AddressRangeImpl(address, address))) {\n    // Address is occupied — clear or skip\n    CodeUnit existing = view.getContaining(startSnap, address);\n    if (existing != null) existing.delete();\n}\nview.create(lifespan, address, platform, dataType, length);","typeGuard":null,"tryCatchPattern":"try {\n    view.create(lifespan, address, platform, dataType, length);\n} catch (CodeUnitInsertionException e) {\n    if (e.getMessage().equals(\"Code units cannot overlap\")) {\n        CodeUnit existing = view.getContaining(lifespan.lmin(), address);\n        if (existing != null) {\n            existing.delete();\n            view.create(lifespan, address, platform, dataType, length);\n        }\n    } else {\n        throw e;\n    }\n}","preventionTips":["Check view.getContaining(startSnap, address) before creating data units.","Clear existing units at the target address before bulk annotations.","Design scripts to handle pre-existing units gracefully (overwrite or skip)."],"tags":["code-unit","overlap","create","address-conflict","trace-modeling"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}