Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3762 - Updated Enum Editor to trim text for name and description when editing; also fixed the Decompiler's Rename Variable action to do the same

dragonmacher9c74254a29d6
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/EnumEditorProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/EnumEditorProvider.java
index 1e837ea61d..6c6e8a28e5 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/EnumEditorProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/EnumEditorProvider.java
@@ -279,12 +279,12 @@ public class EnumEditorProvider extends ComponentProviderAdapter
private void createActions() {
hexDisplayAction = new ToggleActionBuilder("Toggle Hex Mode", plugin.getName())
- .menuPath("Show Enum Values in Hex")
- .description("Toggles Enum value column to show values in hex or decimal")
- .keyBinding("Shift-H")
- .selected(true)
- .onAction(c -> editorPanel.setHexDisplayMode(hexDisplayAction.isSelected()))
- .buildAndInstallLocal(this);
+ .menuPath("Show Enum Values in Hex")
+ .description("Toggles Enum value column to show values in hex or decimal")
+ .keyBinding("Shift-H")
+ .selected(true)
+ .onAction(c -> editorPanel.setHexDisplayMode(hexDisplayAction.isSelected()))
+ .buildAndInstallLocal(this);
addAction = new EnumPluginAction("Add Enum Value", e -> editorPanel.addEntry());
addAction.setEnabled(true);
@@ -297,7 +297,7 @@ public class EnumEditorProvider extends ComponentProviderAdapter
new EnumPluginAction("Delete Enum Value", e -> editorPanel.deleteSelectedEntries());
deleteAction.setEnabled(false);
deleteAction
- .setPopupMenuData(new MenuData(new String[] { "Delete" }, DELETE_ICON, editGroup));
+ .setPopupMenuData(new MenuData(new String[] { "Delete" }, DELETE_ICON, editGroup));
deleteAction.setToolBarData(new ToolBarData(DELETE_ICON, editGroup));
deleteAction.setDescription("Delete the selected enum entries");
@@ -454,7 +454,7 @@ public class EnumEditorProvider extends ComponentProviderAdapter
}
private void applyName(Enum newEnuum) {
- String editorName = editorPanel.getEnumName();
+ String editorName = editorPanel.getEnumName().trim();
if (originalEnumName.equals(editorName)) {
return; // nothing to do
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/AddEditDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/AddEditDialog.java
index bf7123f51f..4958a28cf6 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/AddEditDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/AddEditDialog.java
@@ -527,8 +527,8 @@ public class AddEditDialog extends DialogComponentProvider {
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
- // change the preferred size to use the width determined by the # of columns in
- // combo box editor instead of the largest item in the combo box data model to
+ // change the preferred size to use the width determined by the # of columns in
+ // combo box editor instead of the largest item in the combo box data model to
// prevent the dialog from growing huge when a large label gets added to its recent
// items
Dimension editorSize = getEditor().getEditorComponent().getPreferredSize();
@@ -607,7 +607,11 @@ public class AddEditDialog extends DialogComponentProvider {
}
private String getText() {
- return labelNameChoices.getText();
+ String text = labelNameChoices.getText();
+ if (text != null) {
+ text = text.trim();
+ }
+ return text;
}
public class NamespaceWrapper {
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/editor/EnumEditor2Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/editor/EnumEditor2Test.java
index 22f0bd20be..098f87e077 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/editor/EnumEditor2Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/editor/EnumEditor2Test.java
@@ -65,8 +65,8 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
public void testInsertRow() throws Exception {
Category cat = program.getListing()
- .getDataTypeManager()
- .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
+ .getDataTypeManager()
+ .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
final Enum enumm = new EnumDataType("Colors", 1);
enumm.add("Red", 0);
enumm.add("Green", 0x10);
@@ -101,8 +101,8 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testSortColumns() throws Exception {
Category cat = program.getListing()
- .getDataTypeManager()
- .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
+ .getDataTypeManager()
+ .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
final Enum enumm = new EnumDataType("Colors", 1);
enumm.add("Red", 0);
enumm.add("Green", 0x10);
@@ -145,8 +145,8 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
public void testSortOnComments() {
Category cat = program.getListing()
- .getDataTypeManager()
- .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
+ .getDataTypeManager()
+ .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
Enum enumm = new EnumDataType("Colors", 1);
enumm.add("Red", 0, "1");
enumm.add("Green", 0x10, "3");
@@ -184,8 +184,8 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testSortOrder() throws Exception {
Category cat = program.getListing()
- .getDataTypeManager()
- .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
+ .getDataTypeManager()
+ .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
final Enum enumm = new EnumDataType("Colors", 1);
enumm.add("Red", 0);
enumm.add("Green", 0x10);
@@ -229,8 +229,8 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testInsertRowByName() throws Exception {
Category cat = program.getListing()
- .getDataTypeManager()
- .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
+ .getDataTypeManager()
+ .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
final Enum enumm = new EnumDataType("Colors", 1);
enumm.add("Red", 0);
enumm.add("Green", 0x10);
@@ -483,8 +483,8 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
public void testDuplicateName() throws Exception {
Category cat = program.getListing()
- .getDataTypeManager()
- .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
+ .getDataTypeManager()
+ .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
final Enum enumm = new EnumDataType("Colors", 1);
enumm.add("Red", 0);
enumm.add("Green", 0x10);
@@ -529,6 +529,48 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
}
+ @Test
+ public void testNameTrim() throws Exception {
+
+ Enum enummDt = editSampleEnum();
+
+ EnumEditorPanel panel = findEditorPanel(tool.getToolFrame());
+ String newName = " MyNewName ";
+ runSwing(() -> {
+ JTextField nameField = getTextField(panel, "Name");
+ nameField.setText(newName);
+ });
+
+ DockingActionIf applyAction = getAction(plugin, "Apply Enum Changes");
+ assertTrue(applyAction.isEnabled());
+
+ performAction(applyAction);
+ waitForProgram(program);
+
+ assertEquals(newName.trim(), enummDt.getName());
+ }
+
+ @Test
+ public void testDescriptionTrim() throws Exception {
+
+ Enum enummDt = editSampleEnum();
+
+ EnumEditorPanel panel = findEditorPanel(tool.getToolFrame());
+ String newDescription = " My new description ";
+ runSwing(() -> {
+ JTextField nameField = getTextField(panel, "Description");
+ nameField.setText(newDescription);
+ });
+
+ DockingActionIf applyAction = getAction(plugin, "Apply Enum Changes");
+ assertTrue(applyAction.isEnabled());
+
+ performAction(applyAction);
+ waitForProgram(program);
+
+ assertEquals(newDescription.trim(), enummDt.getDescription());
+ }
+
@Test
public void testDuplicateValue() throws Exception {
@@ -855,8 +897,8 @@ public class EnumEditor2Test extends AbstractGhidraHeadedIntegrationTest {
private Enum editSampleEnum() {
Category cat = program.getListing()
- .getDataTypeManager()
- .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
+ .getDataTypeManager()
+ .getCategory(new CategoryPath(CategoryPath.ROOT, "Category1"));
final Enum enumm = new EnumDataType("Colors", 1);
enumm.add("Red", 0);
enumm.add("Green", 0x10);
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/label/AddEditDialoglTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/label/AddEditDialoglTest.java
index 7311dbd800..b9a2565240 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/label/AddEditDialoglTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/label/AddEditDialoglTest.java
@@ -95,7 +95,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
@After
public void tearDown() throws Exception {
- dialog.close();
+ close(dialog);
env.dispose();
}
@@ -362,6 +362,34 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
assertTrue(function.getSymbol().isPrimary());
}
+ @Test
+ public void testRenameFunction_Trim() throws Exception {
+
+ Symbol s = getUniqueSymbol(program, "entry", null);
+ Function function = program.getFunctionManager().getFunctionAt(s.getAddress());
+ if (function == null) {
+ tool.execute(new CreateFunctionCmd(s.getAddress()), program);
+ program.flushEvents();
+ waitForSwing();
+ function = program.getFunctionManager().getFunctionAt(s.getAddress());
+ s = getUniqueSymbol(program, "entry", null);
+ }
+ // add another label at this address
+ AddLabelCmd cmd = new AddLabelCmd(addr(0x01006420), "fred", SourceType.USER_DEFINED);
+ tool.execute(cmd, program);
+
+ // now attempt to rename the entry label
+ editLabel(s);
+ assertEquals("entry", getText());
+ String newText = " bob ";
+ setText(newText);
+ pressOk();
+ program.flushEvents();
+ waitForSwing();
+ assertEquals("bob", function.getName());
+ assertTrue(function.getSymbol().isPrimary());
+ }
+
@Test
public void testSetPrimaryOnOtherLabel() throws Exception {
Symbol s = getUniqueSymbol(program, "entry", null);
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.java
index f23f568178..fbee02283d 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.java
@@ -75,6 +75,8 @@ public abstract class RenameTask {
dialog.setStatusText("Cannot have empty name");
return false;
}
+
+ name = name.trim();
if (name.equals(oldName)) { // No change to name
newName = name;
return true; // but valid (ends up being equivalent to cancel