From 880631356dcc47578c389d856ae6d220d6c86773 Mon Sep 17 00:00:00 2001 From: Nihal Jere Date: Tue, 2 Apr 2024 13:31:51 -0500 Subject: [PATCH 1/2] ArrayDataType: add constructor without elementLength parameter The elementLength parameter is unused when the base type isn't dynamic, but the existing constructors require it to be specified. --- .../java/ghidra/program/model/data/ArrayDataType.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java index b280085b17..31fb8af3f7 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java @@ -32,6 +32,15 @@ public class ArrayDataType extends DataTypeImpl implements Array { private int elementLength; private boolean deleted = false; + /** + * Constructs a new Array dataType. + * @param dataType the dataType of the elements in the array (null is not permitted). + * @param numElements the number of elements in the array (0 is permitted). + */ + public ArrayDataType(DataType dataType, int numElements) { + this(dataType, numElements, -1, null); + } + /** * Constructs a new Array dataType. * @param dataType the dataType of the elements in the array (null is not permitted). From 319619c4fccd26514bb7255569d65f4634aa894f Mon Sep 17 00:00:00 2001 From: ghidra1 Date: Mon, 15 Apr 2024 12:05:26 -0400 Subject: [PATCH 2/2] GP-4525 Updated ArrayDataType javadoc and forced use of datatype's DTM if DTM is not specified. --- .../program/model/data/ArrayDataType.java | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java index 31fb8af3f7..9bed952ae6 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/ArrayDataType.java @@ -24,6 +24,9 @@ import ghidra.util.exception.DuplicateNameException; /** * Basic implementation of the Array interface. + * + * NOTE: The use of {@link FactoryDataType} and {@link Dynamic}, where + * {@link Dynamic#canSpecifyLength()} is false, are not supported for array use. */ public class ArrayDataType extends DataTypeImpl implements Array { @@ -33,21 +36,29 @@ public class ArrayDataType extends DataTypeImpl implements Array { private boolean deleted = false; /** - * Constructs a new Array dataType. - * @param dataType the dataType of the elements in the array (null is not permitted). + * Constructs a new Array dataType for fixed-length datatypes. The specified datatype's + * {@link DataTypeManager} will be used for its data organization. + * @param dataType the dataType of the elements in the array ({@link FactoryDataType} and + * {@link Dynamic} data types are not permitted). * @param numElements the number of elements in the array (0 is permitted). + * @throws IllegalArgumentException if invalid datatype is specified or required valid + * {@code elementLength} required. */ public ArrayDataType(DataType dataType, int numElements) { this(dataType, numElements, -1, null); } /** - * Constructs a new Array dataType. - * @param dataType the dataType of the elements in the array (null is not permitted). + * Constructs a new Array dataType. The specified datatype's {@link DataTypeManager} will + * be used for its data organization. + * @param dataType the dataType of the elements in the array. {@link FactoryDataType} and + * {@link Dynamic}, where {@link Dynamic#canSpecifyLength()} is false, are not not permitted. * @param numElements the number of elements in the array (0 is permitted). * @param elementLength the length of an individual element in the array. This value * is only used for {@link Dynamic} dataType where {@link Dynamic#canSpecifyLength()} - * returns true. + * returns true. A -1 value can be specified for fixed-length datatypes. + * @throws IllegalArgumentException if invalid datatype is specified or required valid + * {@code elementLength} required. */ public ArrayDataType(DataType dataType, int numElements, int elementLength) { this(dataType, numElements, elementLength, null); @@ -55,19 +66,22 @@ public class ArrayDataType extends DataTypeImpl implements Array { /** * Constructs a new Array dataType. - * @param dataType the dataType of the elements in the array. + * @param dataType the dataType of the elements in the array. {@link FactoryDataType} and + * {@link Dynamic}, where {@link Dynamic#canSpecifyLength()} is false, are not not permitted. * @param numElements the number of elements in the array (0 is permitted). * @param elementLength the length of an individual element in the array. This value * is only used for {@link Dynamic} dataType where {@link Dynamic#canSpecifyLength()} - * returns true. - * @param dtm datatype manager or null + * returns true. A -1 value can be specified for fixed-length datatypes. + * @param dataMgr datatype manager or null. If null, the datatype manager associated with the + * specified datatype will be used. + * @throws IllegalArgumentException if invalid datatype is specified or required valid + * {@code elementLength} required. */ public ArrayDataType(DataType dataType, int numElements, int elementLength, - DataTypeManager dtm) { - super(dataType.getCategoryPath(), "array", dtm); + DataTypeManager dataMgr) { + super(dataType.getCategoryPath(), "array", getPreferredDataTypeManager(dataType, dataMgr)); if (dataType instanceof FactoryDataType) { - throw new IllegalArgumentException( - "Factory data type not permitted"); + throw new IllegalArgumentException("Factory data type not permitted"); } if (numElements < 0) { throw new IllegalArgumentException( @@ -78,7 +92,7 @@ public class ArrayDataType extends DataTypeImpl implements Array { baseDt = ((TypeDef) dataType).getBaseDataType(); } validate(baseDt); - dataType = dataType.clone(dtm); + dataType = dataType.clone(getDataTypeManager()); this.elementLength = -1; if (baseDt instanceof Dynamic) { if (elementLength < 0) { @@ -97,6 +111,13 @@ public class ArrayDataType extends DataTypeImpl implements Array { dataType.addParent(this); } + private static DataTypeManager getPreferredDataTypeManager(DataType dt, DataTypeManager dtm) { + if (dtm != null) { + return dtm; + } + return dt.getDataTypeManager(); + } + /** * Validate a array base datatype to ensure that it is allowed * @param baseDt intended base datatype for array (always pass in typedef's base type if applicable) @@ -114,12 +135,13 @@ public class ArrayDataType extends DataTypeImpl implements Array { if (baseDt instanceof Dynamic) { if (!((Dynamic) baseDt).canSpecifyLength()) { throw new IllegalArgumentException( - "Array data-type may not be a non-sizable Dynamic data-type: " + baseDt.getName()); + "Array data-type may not be a non-sizable Dynamic data-type: " + + baseDt.getName()); } } else if (baseDt.getLength() < 1) { // not Dynamic - throw new IllegalArgumentException( - "Data type may not report a length less than 1: " + baseDt.getClass().getSimpleName()); + throw new IllegalArgumentException("Data type may not report a length less than 1: " + + baseDt.getClass().getSimpleName()); } }