Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5527 Added structure unpack support for zero-length component edge case

ghidra15cd086296900
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 5f88476f7a..7ae58ee855 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
@@ -1345,6 +1345,9 @@ class StructureEditorModel extends CompEditorModel {
numComps = array.getNumElements();
// Remove the array.
delete(componentOrdinal);
+ if (array.isZeroLength()) {
+ return;
+ }
if (numComps > 0) {
// Add the array's elements
try {
@@ -1366,18 +1369,41 @@ class StructureEditorModel extends CompEditorModel {
// Remove the structure.
int currentOffset = currentComp.getOffset();
- // NOTE: There is still a case which is unhandled: if there is a zero-length
- // component at the same offset as the component to be unpacked, the unpacking
- // will be inserted before the zero-length component. More work is needed to
- // handle this case.
-
- deleteComponent(rowIndex);
+ // TODO: may want to add this functionality into the API
- // Add the structure's elements
Stack<DataTypeComponent> zeroDtcStack = new Stack<>();
int zeroStackOffset = -1;
int zeroStackOrdinal = -1;
int packedOrdinal = 0;
+
+ deleteComponent(rowIndex);
+
+ if (struct.isZeroLength()) {
+ return;
+ }
+
+ if (rowIndex != 0 && !viewStruct.isPackingEnabled()) {
+ // Must consume any preceeding zero-length components at the same offset
+ // into the zeroDtcStack to prevent their movement on subsequent component
+ // inserts at the same offset
+ DataTypeComponent dtc =
+ viewStruct.getDefinedComponentAtOrAfterOffset(currentOffset);
+ if (dtc != null && dtc.getOffset() == currentOffset) {
+ // zero-length is assumed if offset matches
+ zeroStackOffset = 0;
+ int componentCount = viewStruct.getNumComponents();
+ while (dtc != null && dtc.getOffset() == currentOffset) {
+ int ordinal = dtc.getOrdinal();
+ zeroDtcStack.push(dtc);
+ viewStruct.delete(ordinal);
+ --componentCount;
+ dtc = ordinal < componentCount ? viewStruct.getComponent(ordinal)
+ : null;
+ }
+ }
+ }
+
+ // Add the structure's elements
for (DataTypeComponent dtc : struct.getDefinedComponents()) {
DataType compDt = dtc.getDataType();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.java
index 252f24b921..4c6e9ffe7d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.java
@@ -60,18 +60,6 @@ public class UnpackageAction extends CompositeEditorTableAction {
int currentRowIndex =
model.getSelection().getFieldRange(0).getStart().getIndex().intValue();
- // Check for unsupported unpack case.
- StructureEditorModel structModel = (StructureEditorModel) model;
- if (!structModel.isPackingEnabled()) {
- DataTypeComponent dtc = structModel.getComponent(currentRowIndex);
- if (dtc != null && dtc.getOrdinal() != 0 &&
- structModel.getComponent(currentRowIndex - 1).getOffset() == dtc.getOffset()) {
- Msg.showInfo(this, model.getProvider().getComponent(), "Unsupported Unpack",
- "Unpack is not supported when component offset is shared with a zero-length component.");
- return;
- }
- }
-
int subComps = model.getNumSubComponents(currentRowIndex);
if (subComps > 1000) {
String question = "Are you sure you want to unpackage " + subComps + " components?";