Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-2832 - fix Data Type Preview to allow string data types.

dev747368e8343999d435
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datapreview/DataTypePreviewPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datapreview/DataTypePreviewPlugin.java
index 6359316b90..ecf853e8ac 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datapreview/DataTypePreviewPlugin.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datapreview/DataTypePreviewPlugin.java
@@ -282,7 +282,7 @@ public class DataTypePreviewPlugin extends ProgramPlugin {
dtm = activeProgram.getDataTypeManager();
}
DataTypeSelectionDialog d = new DataTypeSelectionDialog(tool, dtm, Integer.MAX_VALUE,
- AllowedDataTypes.FIXED_LENGTH);
+ AllowedDataTypes.STRINGS_AND_FIXED_LENGTH);
tool.showDialog(d, provider);
DataType dt = d.getUserChosenDataType();
if (dt != null) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java
index dd615bb8fa..423496ca55 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java
@@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
- * REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -323,7 +322,7 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
// then changed the text field text.
DataType selectedDataType = selectionField.getSelectedValue();
if (selectedDataType != null && selectionField.getText().equals(selectedDataType.getName())) {
- DataTypeParser.checkAllowableType(selectedDataType, allowedDataTypes);
+ DataTypeParser.ensureIsAllowableType(selectedDataType, allowedDataTypes);
return true;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java b/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java
index b9eab4373c..da54b1c107 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java
@@ -41,7 +41,11 @@ public class DataTypeParser {
/**
* Only Fixed-length data-types
*/
- FIXED_LENGTH
+ FIXED_LENGTH,
+ /**
+ * Only Fixed-length data types and string data types
+ */
+ STRINGS_AND_FIXED_LENGTH
}
private DataTypeManager sourceDataTypeManager; // may be null
@@ -144,30 +148,44 @@ public class DataTypeParser {
}
/**
- * Validate the specified data-type dt against the specified allowedTypes.
- * @param dt data-type
- * @param allowedTypes
+ * Throws exception if the data type does not match the specified {@link AllowedDataTypes}.
+ *
+ * @param dt {@link DataType} to check
+ * @param allowedTypes {@link AllowedDataTypes enum} specifying what category of data types are ok
* @throws InvalidDataTypeException if dt violates the specified allowedTypes
*/
- public static void checkAllowableType(DataType dt, AllowedDataTypes allowedTypes)
+ public static void ensureIsAllowableType(DataType dt, AllowedDataTypes allowedTypes)
throws InvalidDataTypeException {
- if (allowedTypes == AllowedDataTypes.DYNAMIC) {
- if (dt instanceof FactoryDataType) {
- throw new InvalidDataTypeException("factory data-type not allowed");
- }
- }
- else if (allowedTypes == AllowedDataTypes.SIZABLE_DYNAMIC) {
- if (dt instanceof FactoryDataType) {
- throw new InvalidDataTypeException("factory data-type not allowed");
- }
- if (dt instanceof Dynamic && !((Dynamic) dt).canSpecifyLength()) {
- throw new InvalidDataTypeException("non-sizable data-type not allowed");
- }
- }
- else if (allowedTypes == AllowedDataTypes.FIXED_LENGTH) {
- if (dt.getLength() < 0) {
- throw new InvalidDataTypeException("fixed-length data-type required");
- }
+ switch (allowedTypes) {
+ case DYNAMIC:
+ if (dt instanceof FactoryDataType) {
+ throw new InvalidDataTypeException("factory data-type not allowed");
+ }
+ break;
+ case SIZABLE_DYNAMIC:
+ if (dt instanceof FactoryDataType) {
+ throw new InvalidDataTypeException("factory data-type not allowed");
+ }
+ if (dt instanceof Dynamic && !((Dynamic) dt).canSpecifyLength()) {
+ throw new InvalidDataTypeException("non-sizable data-type not allowed");
+ }
+ break;
+ case FIXED_LENGTH:
+ if (dt.getLength() < 0) {
+ throw new InvalidDataTypeException("fixed-length data-type required");
+ }
+ break;
+ case STRINGS_AND_FIXED_LENGTH:
+ if (dt.getLength() < 0 && !(dt instanceof AbstractStringDataType)) {
+ throw new InvalidDataTypeException("fixed-length or string data-type required");
+ }
+ break;
+ case ALL:
+ // do nothing
+ break;
+ default:
+ throw new InvalidDataTypeException(
+ "unknown data type allowance specified: " + allowedTypes);
}
}
@@ -221,7 +239,7 @@ public class DataTypeParser {
catch (IllegalArgumentException e) {
throw new InvalidDataTypeException(e.getMessage());
}
- checkAllowableType(dt, allowedTypes);
+ ensureIsAllowableType(dt, allowedTypes);
return dt;
}