From 0fc180569572bd91cf86c9d0b12d1d4747bdf85b Mon Sep 17 00:00:00 2001 From: ghidra1 Date: Mon, 24 Feb 2025 17:13:00 -0500 Subject: [PATCH] GP-5412 Corrected structure editor transaction error --- .../compositeeditor/StructureEditorModel.java | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.java index bb1007ec14..92e6623f2a 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.java @@ -1119,17 +1119,8 @@ class StructureEditorModel extends CompEditorModel { } DataTypeManager originalDtm = getOriginalDataTypeManager(); - String baseName = "struct"; CategoryPath originalCategoryPath = getOriginalCategoryPath(); - String uniqueName = viewDTM.getUniqueName(originalCategoryPath, baseName); - DataType conflictingDt = originalDtm.getDataType(originalCategoryPath, uniqueName); - while (conflictingDt != null) { - // pull the data type into the view data type manager with the conflicting name. - viewDTM.resolve(conflictingDt, DataTypeConflictHandler.DEFAULT_HANDLER); - // Try to get another unique name. - uniqueName = viewDTM.getUniqueName(originalCategoryPath, baseName); - conflictingDt = originalDtm.getDataType(originalCategoryPath, uniqueName); - } + String uniqueName = getUniqueDataTypeName(originalCategoryPath, "struct"); String specifiedName = showNameDialog(uniqueName, originalCategoryPath, viewComposite.getName(), originalDtm); @@ -1148,6 +1139,39 @@ class StructureEditorModel extends CompEditorModel { }); } + /** + * Get a unique datatype name. Method based on the logic used by + * {@link DataTypeManager#getUniqueName(CategoryPath, String)} but + * checks both the original and view DTMs for uniqueness. + * @param path category path + * @param baseName datatype name + * @return unique datatype name + */ + private String getUniqueDataTypeName(CategoryPath path, String baseName) { + int pos = baseName.lastIndexOf('_'); + int oneUpNumber = 0; + String name = baseName; + if (pos > 0) { + String numString = baseName.substring(pos + 1); + try { + oneUpNumber = Integer.parseInt(numString); + name = baseName; + baseName = baseName.substring(0, pos); + } + catch (NumberFormatException e) { + // the number will get updated below + } + } + DataTypeManager originalDtm = getOriginalDataTypeManager(); + // Pick a name not used within either original or view DTM + while (originalDtm.getDataType(path, name) != null || + viewDTM.getDataType(path, name) != null) { + ++oneUpNumber; + name = baseName + "_" + oneUpNumber; + } + return name; + } + private void doCreateInternalStructure(DataTypeManager dtm, CategoryPath categoryPath, String name, TaskMonitor monitor) throws InvalidDataTypeException, UsrException {