GP-1123 Check for name collision when creating placeholder structure

This commit is contained in:
caheckman 2023-02-09 16:36:23 -05:00
parent bb79314d85
commit 51b1b51d89
4 changed files with 54 additions and 24 deletions

View file

@ -209,7 +209,7 @@ public class FunctionDB extends DatabaseObject implements Function {
@Override
public ExternalLocation getExternalLocation() {
if (isExternal()) {
ExternalManagerDB extMgr = (ExternalManagerDB) program.getExternalManager();
ExternalManagerDB extMgr = program.getExternalManager();
return extMgr.getExternalLocation(getSymbol());
}
return null;
@ -993,7 +993,7 @@ public class FunctionDB extends DatabaseObject implements Function {
v.setStorageAndDataType(storage, var.getDataType());
}
else {
SymbolManager symbolMgr = (SymbolManager) program.getSymbolTable();
SymbolManager symbolMgr = program.getSymbolTable();
VariableSymbolDB s = symbolMgr.createVariableSymbol(name, this,
SymbolType.LOCAL_VAR, firstUseOffset, storage, source);
s.setStorageAndDataType(storage, var.getDataType());
@ -1460,7 +1460,7 @@ public class FunctionDB extends DatabaseObject implements Function {
}
// Append new parameters if needed
SymbolManager symbolMgr = (SymbolManager) program.getSymbolTable();
SymbolManager symbolMgr = program.getSymbolTable();
for (int i = newParamIndex; i < newParams.size(); i++) {
Variable newParam = newParams.get(i);
DataType dt = (newParam instanceof Parameter && !useCustomStorage)
@ -1683,7 +1683,7 @@ public class FunctionDB extends DatabaseObject implements Function {
}
}
}
SymbolManager symbolMgr = (SymbolManager) program.getSymbolTable();
SymbolManager symbolMgr = program.getSymbolTable();
VariableSymbolDB s = symbolMgr.createVariableSymbol(name, this,
SymbolType.PARAMETER, ordinal, storage, paramSource);
s.setStorageAndDataType(storage, var.getDataType());
@ -2671,7 +2671,9 @@ public class FunctionDB extends DatabaseObject implements Function {
// relationship between a class namespace and its structure is needed
// so its name and category can track properly.
classStruct = VariableUtilities.findOrCreateClassStruct(this);
dataTypeManager.resolve(classStruct, null);
if (classStruct != null) {
dataTypeManager.resolve(classStruct, null);
}
}
}

View file

@ -750,17 +750,37 @@ public class VariableUtilities {
* Create an empty placeholder class structure whose category is derived from
* the function's class namespace. NOTE: The structure will not be added to the data
* type manager.
* The structure is created in the program's preferred category, or in the root category
* if a preferred category has not been set. If a colliding data-type matching the class name
* and category already exists, null is returned.
* @param classNamespace class namespace
* @param dataTypeManager data type manager's whose data organization should be applied.
* @return new class structure
* @return new class structure (or null if there is a collision)
*/
private static Structure createPlaceholderClassStruct(GhidraClass classNamespace,
DataTypeManager dataTypeManager) {
Namespace classParentNamespace = classNamespace.getParentNamespace();
CategoryPath prefRoot = null;
if (dataTypeManager instanceof ProgramBasedDataTypeManager) {
prefRoot = ((ProgramBasedDataTypeManager) dataTypeManager).getProgram()
.getPreferredRootNamespaceCategoryPath();
}
if (prefRoot == null) {
prefRoot = CategoryPath.ROOT;
}
CategoryPath category =
DataTypeUtilities.getDataTypeCategoryPath(CategoryPath.ROOT, classParentNamespace);
DataTypeUtilities.getDataTypeCategoryPath(prefRoot, classParentNamespace);
DataType existingDT = dataTypeManager.getDataType(category, classNamespace.getName());
if (existingDT != null) {
// If a data-type already exists in the parent, try to create in the child
category = DataTypeUtilities.getDataTypeCategoryPath(prefRoot, classNamespace);
existingDT = dataTypeManager.getDataType(category, classNamespace.getName());
if (existingDT != null) { // If this also already exists
return null; // Don't create a placeholder
}
}
StructureDataType structDT =
new StructureDataType(category, classNamespace.getName(), 0, dataTypeManager);
structDT.setDescription("PlaceHolder Class Structure");
@ -782,6 +802,9 @@ public class VariableUtilities {
* and may refer to a non-existing category path which corresponds to the class-parent's
* namespace.
*
* If an unrelated data-type already exists matching the class name and category,
* null is returned.
*
* @param classNamespace class namespace
* @param dataTypeManager data type manager which should be searched and whose
* data organization should be used.
@ -811,6 +834,9 @@ public class VariableUtilities {
* and may refer to a non-existing category path which corresponds to the class-parent's
* namespace.
*
* If the function is not part of a class, or if an unrelated data-type already exists with
* the class's name and category, null is returned.
*
* @param function function's whose class namespace is the basis for the structure
* @return new or existing structure whose name matches the function's class namespace or
* null if function not contained within a class namespace.