Ghidra is a software reverse engineering (SRE) framework
GP-4951 Corrected structure editor component actions related to 0-length components and arrays.
ghidra1584026d81762
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.javaindex ff8edd751e..1539dd5da3 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java@@ -1669,15 +1669,16 @@ public abstract class CompEditorModel extends CompositeEditorModel {}DataTypeComponent dtc = getComponent(rowIndex);DataType dt = dtc.getDataType();- int dtcLen = dt.getLength();- if (dtcLen < 0) {- dtcLen = dtc.getLength();- }- int maxDups = (Integer.MAX_VALUE - getLength()) / dtcLen;- if (dt != DataType.DEFAULT && isShowingUndefinedBytes() && !isAtEnd(rowIndex)) {- // If editModel is showing undefined bytes (non-packed)- // then constrain by number of undefined bytes that follow.- maxDups = getNumUndefinedBytesAfter(dtc) / dtcLen;+ int dtcLen = dtc.getLength();++ int maxDups = (Integer.MAX_VALUE - getLength());+ if (dtcLen > 0) {+ maxDups /= dtcLen;+ if (dt != DataType.DEFAULT && isShowingUndefinedBytes() && !isAtEnd(rowIndex)) {+ // If editModel is showing undefined bytes (non-packed)+ // then constrain by number of undefined bytes that follow.+ maxDups = getNumUndefinedBytesAfter(dtc) / dtcLen;+ }}return maxDups;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.javaindex 5643908f1f..d55a0914e0 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.java@@ -692,7 +692,7 @@ abstract public class CompositeEditorModel extends CompositeViewerModel {if (!isArrayAllowed()) {throw new UsrException("Array not permitted in current context");}- int min = 1;+ int min = 0;int max = getMaxElements();if (isSingleRowSelection()) {if (max != 0) {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.javaindex ab9f52d06a..661f159fcb 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@@ -344,16 +344,14 @@ class StructureEditorModel extends CompEditorModel {throw new IllegalArgumentException("Invalid component index specified");}DataType dt = originalComp.getDataType();- int len = dt.getLength();- if (len < 0) {- len = originalComp.getLength();- }+ int dtcLen = originalComp.getLength();+checkIsAllowableDataType(dt);- int dtcLen = len;viewDTM.withTransaction("Duplicate Components", () -> {int startIndex = index + 1;- if (dt != DataType.DEFAULT && isShowingUndefinedBytes() && !isAtEnd(index)) {+ if (dtcLen > 0 && dt != DataType.DEFAULT && isShowingUndefinedBytes() &&+ !isAtEnd(index)) {int endIndex = startIndex + (dtcLen * multiple) - 1;if (startIndex < getNumComponents()) {deleteComponentRange(startIndex, endIndex, monitor);@@ -585,7 +583,7 @@ class StructureEditorModel extends CompEditorModel {return true;}DataType dt = comp.getDataType();- if (dt.equals(DataType.DEFAULT)) {+ if (dt.equals(DataType.DEFAULT) || dt.isZeroLength()) {return true; // Insert an undefined and push everything down.}if (comp.isBitFieldComponent()) {@@ -860,8 +858,7 @@ class StructureEditorModel extends CompEditorModel {return viewDTM.withTransaction("Insert Component", () -> {DataTypeComponent dtc;if (isPackingEnabled() || !(dataType instanceof BitFieldDataType)) {- dtc = viewComposite.insert(rowIndex, dataType, length, name,- comment);+ dtc = viewComposite.insert(rowIndex, dataType, length, name, comment);}else {BitFieldDataType bitfield = (BitFieldDataType) dataType;diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.javaindex 3687e1d410..a354dc3808 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java@@ -21,9 +21,7 @@ import javax.swing.*;import org.junit.Test;-import docking.widgets.OptionDialog;import ghidra.program.database.DatabaseObject;-import ghidra.program.database.data.StructureDBTest;import ghidra.program.model.data.*;public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTest {