{"record":{"id":"e097254a479eb8d6","repo":"NationalSecurityAgency/ghidra","slug":"data-not-in-a-structure","errorCode":null,"errorMessage":"Data not in a structure","messagePattern":"Data not in a structure","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/Base/src/main/java/ghidra/app/cmd/data/CreateStructureInStructureCmd.java","lineNumber":93,"sourceCode":"\t\t}\n\n\t\treturn structure;\n\t}\n\n\t/*\n\t * @see AbstractCreateStructureCmd#initializeStructureData(StructureInfo)\n\t */\n\t/*package*/ @Override\n\tDataType initializeStructureData(Program program, Structure localStructure) {\n\n\t\tData data = program.getListing().getDataContaining(getStructureAddress());\n\t\tData comp1 = data.getComponent(fromPath);\n\t\tData comp2 = data.getComponent(toPath);\n\t\tint dataLength = (comp2.getParentOffset() + comp2.getLength()) - comp1.getParentOffset();\n\n\t\tDataType parentDataType = comp1.getParent().getBaseDataType();\n\t\tif (!(parentDataType instanceof Structure)) {\n\t\t\tthrow new IllegalArgumentException(\"Data not in a structure\");\n\t\t}\n\t\tStructure originalStructure = (Structure) parentDataType;\n\n\t\t// clear and initialize the original structure and then get the new\n\t\t// data\n\t\tclearStruct(originalStructure, comp1.getParentOffset(), dataLength);\n\t\toriginalStructure.replace(comp1.getComponentIndex(), localStructure,\n\t\t\tlocalStructure.getLength());\n\t\tcomp1 = data.getComponent(fromPath);\n\n\t\treturn comp1.getDataType();\n\t}\n\n\tprivate void clearStruct(Structure struct, int offset, int length) {\n\t\tDataTypeComponent[] comps = struct.getDefinedComponents();\n\t\tint endOffset = offset + length;\n\t\tfor (int i = comps.length - 1; i >= 0; i--) {\n\t\t\tif (comps[i].getOffset() >= offset && comps[i].getOffset() < endOffset) {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/data/CreateStructureInStructureCmd.java#L75-L111","documentation":"Thrown by CreateStructureInStructureCmd.initializeStructureData when the parent datatype of the selected component is not a Structure. The command navigates comp1.getParent().getBaseDataType() and requires it to be a Structure to perform component replacement; a Union, Array, Typedef-to-non-structure, or other composite triggers the error.","triggerScenarios":"Invoking CreateStructureInStructureCmd on a component whose parent is a Union (or other non-Structure composite). The fromPath/toPath must select components inside a Structure; using them against a Union's members violates the precondition.","commonSituations":"User selects a field inside a Union and runs 'Create Structure' in Ghidra. Programmatic use of CreateStructureInStructureCmd with a fromPath rooted in a non-Structure. Path computed against a Typedef that resolves to a Union.","solutions":["Run the command only when the parent composite is a Structure.","Check `comp1.getParent().getBaseDataType() instanceof Structure` before invoking the command.","For Unions, restructure the data differently - you cannot 'create structure in structure' inside a Union."],"exampleFix":"// before\nnew CreateStructureInStructureCmd(addr, fromPath, toPath).applyTo(program);\n// parent is a Union -> IllegalArgumentException\n\n// after\nData comp1 = program.getListing().getDataContaining(addr).getComponent(fromPath);\nif (!(comp1.getParent().getBaseDataType() instanceof Structure)) {\n    Msg.warn(this, \"Cannot create structure-in-structure: parent is \" +\n        comp1.getParent().getBaseDataType().getClass().getSimpleName());\n    return;\n}\nnew CreateStructureInStructureCmd(addr, fromPath, toPath).applyTo(program);","handlingStrategy":"type-guard","validationCode":"Data comp1 = program.getListing().getDataContaining(addr).getComponent(fromPath);\nDataType parent = comp1.getParent().getBaseDataType();\nif (!(parent instanceof Structure)) {\n    // do not invoke CreateStructureInStructureCmd\n}","typeGuard":"boolean isInsideStructure(Program p, Address addr, int[] fromPath) {\n    Data comp = p.getListing().getDataContaining(addr).getComponent(fromPath);\n    return comp != null && comp.getParent() != null\n        && comp.getParent().getBaseDataType() instanceof Structure;\n}","tryCatchPattern":"try {\n    cmd.applyTo(program);\n} catch (IllegalArgumentException e) {\n    if (\"Data not in a structure\".equals(e.getMessage())) {\n        // user selected a Union/other composite; inform and abort\n    } else throw e;\n}","preventionTips":["Confirm the parent composite is a Structure before running 'Create Structure' in a structure.","Remember Unions are not valid parents for this command.","Resolve Typedefs to their base type before the instanceof check."],"tags":["structure","data-type","command","validation"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}