Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7296 - Added field name checking to the input dialog for Find Uses of...

dragonmacherc1b478a2a065
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindReferencesToFieldByNameOrOffsetAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindReferencesToFieldByNameOrOffsetAction.java
index b4654104e0..8bbdf28c14 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindReferencesToFieldByNameOrOffsetAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindReferencesToFieldByNameOrOffsetAction.java
@@ -15,15 +15,17 @@
*/
package ghidra.app.plugin.core.datamgr.actions;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
+import javax.swing.Icon;
import javax.swing.tree.TreePath;
import org.apache.commons.lang3.StringUtils;
import docking.ActionContext;
+import docking.DockingWindowManager;
import docking.widgets.OptionDialog;
+import docking.widgets.dialogs.InputWithChoicesDialog;
import docking.widgets.tree.GTree;
import docking.widgets.tree.GTreeNode;
import ghidra.app.plugin.core.datamgr.DataTypesActionContext;
@@ -32,6 +34,7 @@ import ghidra.app.services.FieldMatcher;
import ghidra.framework.plugintool.Plugin;
import ghidra.program.model.data.*;
import ghidra.program.model.data.Enum;
+import ghidra.util.MessageType;
import ghidra.util.NumericUtilities;
/**
@@ -81,19 +84,67 @@ public class FindReferencesToFieldByNameOrOffsetAction extends AbstractFindRefer
@Override
protected FieldMatcher createFieldMatcher(DataTypeAndFields typeAndFields) {
- DataType dt = typeAndFields.dataType();
- String message = "Find uses of '" + dt.getName() + "' field by name or offset";
- String userChoice = OptionDialog.showEditableInputChoiceDialog(null, "Choose Field",
- message, typeAndFields.fieldNames(), null, OptionDialog.QUESTION_MESSAGE);
- if (userChoice == null) {
- return null; // cancelled
+ FieldInputDialog dialog = new FieldInputDialog(typeAndFields);
+ return dialog.show();
+ }
+
+ private class FieldInputDialog extends InputWithChoicesDialog {
+
+ private static final Icon ICON =
+ OptionDialog.getIconForMessageType(OptionDialog.QUESTION_MESSAGE);
+ private DataTypeAndFields typeAndFields;
+
+ public FieldInputDialog(DataTypeAndFields typeAndFields) {
+ super("Choose Field", createTitle(typeAndFields), typeAndFields.fieldNames(), null,
+ true, ICON);
+ this.typeAndFields = typeAndFields;
+ }
+
+ private static String createTitle(DataTypeAndFields typeAndFields) {
+ DataType dt = typeAndFields.dataType();
+ return "Find uses of '" + dt.getName() + "' field by name or offset";
+ }
+
+ @Override
+ protected void okCallback() {
+
+ String userChoice = getValue();
+ if (!isValid(userChoice)) {
+ setStatusText("Invalid field name or number", MessageType.ERROR);
+ return;
+ }
+
+ super.okCallback();
}
- try {
- return new FieldMatcher(dt, NumericUtilities.parseInt(userChoice));
+ private boolean isValid(String userValue) {
+ try {
+ NumericUtilities.parseInt(userValue);
+ return true;
+ }
+ catch (NumberFormatException e) {
+ // check the field name
+ }
+
+ List<String> list = Arrays.asList(typeAndFields.fieldNames());
+ return list.contains(userValue);
}
- catch (NumberFormatException e) {
- return new FieldMatcher(dt, userChoice);
+
+ FieldMatcher show() {
+ DockingWindowManager.showDialog(null, this);
+
+ if (isCanceled()) {
+ return null; // cancelled
+ }
+
+ DataType dt = typeAndFields.dataType();
+ String userChoice = getValue();
+ try {
+ return new FieldMatcher(dt, NumericUtilities.parseInt(userChoice));
+ }
+ catch (NumberFormatException e) {
+ return new FieldMatcher(dt, userChoice);
+ }
}
}
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputWithChoicesDialog.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputWithChoicesDialog.java
index 11d3bca08b..b0844a2666 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputWithChoicesDialog.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputWithChoicesDialog.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -51,7 +51,7 @@ public class InputWithChoicesDialog extends DialogComponentProvider {
public InputWithChoicesDialog(String dialogTitle, String label, String[] optionValues,
String initialValue, Icon messageIcon) {
- super(dialogTitle, true, false, true, false);
+ super(dialogTitle, true, true, true, false);
setTransient(true);
addOKButton();
@@ -80,7 +80,7 @@ public class InputWithChoicesDialog extends DialogComponentProvider {
public InputWithChoicesDialog(String dialogTitle, String label, String[] optionValues,
String initialValue, boolean allowEdits, Icon messageIcon) {
- super(dialogTitle, true, false, true, false);
+ super(dialogTitle, true, true, true, false);
this.addOKButton();
this.addCancelButton();