Skip to content

Ghidra is a software reverse engineering (SRE) framework

fixed stack trace after pasting

ghidragone3baf4e48625
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/IntegerTextField.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/IntegerTextField.java
index e043362f50..80cd18942a 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/IntegerTextField.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/IntegerTextField.java
@@ -15,15 +15,32 @@
*/
package docking.widgets.textfield;
-import java.awt.*;
-import java.awt.event.*;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Graphics;
+import java.awt.Insets;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
-import javax.swing.*;
-import javax.swing.event.*;
-import javax.swing.text.*;
+import javax.swing.JComponent;
+import javax.swing.JTextField;
+import javax.swing.ToolTipManager;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import javax.swing.text.AbstractDocument;
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.DocumentFilter;
import docking.DockingUtils;
import docking.util.GraphicsUtils;
@@ -150,7 +167,7 @@ public class IntegerTextField {
*/
public BigInteger getValue() {
String text = textField.getText();
- return computeValueFromString(text);
+ return computeValueFromString(text, isHexMode);
}
/**
@@ -211,6 +228,24 @@ public class IntegerTextField {
setValue(BigInteger.valueOf(newValue));
}
+ /**
+ * Sets the field to the given text. The text must be a properly formated string that is
+ * a value that is valid for this field. If the field is set to not allow "0x" prefixes, then
+ * the input string cannot start with 0x and furthermore, if the field is in decimal mode, then
+ * input string cannot take in hex digits a-f. On the other hand, if "0x" prefixes are allowed,
+ * then the input string can be either a decimal number or a hex number depending on if the
+ * input string starts with "0x". In this case, the field's hex mode will be set to match
+ * the input text. If the text is not valid, the field will not change.
+ *
+ * @param text the value as text to set on this field
+ * @return true if the set was successful
+ */
+ public boolean setText(String text) {
+ String oldText = textField.getText();
+ textField.setText(text);
+ return !oldText.equals(textField.getText());
+ }
+
/**
* Sets the value of the field to the given value. A null value will clear the field.
*
@@ -424,12 +459,12 @@ public class IntegerTextField {
return value.toString(10);
}
- private BigInteger computeValueFromString(String text) {
+ private BigInteger computeValueFromString(String text, boolean parseAsHex) {
if (text.isEmpty() || isValidPrefix(text)) {
return null;
}
- if (!isHexMode) {
+ if (!parseAsHex) {
return new BigInteger(text, 10);
}
@@ -472,14 +507,17 @@ public class IntegerTextField {
return value.abs().compareTo(maxValue) <= 0;
}
- private void updateNumberMode(String text) {
+ private boolean shouldParseAsHex(String text) {
if (allowsHexPrefix) {
- isHexMode = text.contains("0x");
+ // if allowing "0x" prefix, let the incoming text determine if we should parse as hex
+ return text.startsWith("0x") || text.startsWith("-0x");
}
+ // otherwise parse the input string is whatever mode this field has been set to.
+ return isHexMode;
}
/**
- * Sets the textField to the given value taking into account the current configuation.
+ * Sets the textField to the given value taking into account the current configuration.
*
* @param value the value to convert to a string for the textField.
*/
@@ -552,27 +590,38 @@ public class IntegerTextField {
private boolean isValid(StringBuilder builder) {
String valueString = builder.toString();
- // maybe switch radix mode depending on if the string starts with 0x
- updateNumberMode(valueString);
+ // Depending on configuration and input string, determine if we should parse as hex.
+ // If we don't allow "0x" prefix, then use the current hex/integer mode, Otherwise,
+ // parse as hex depending on whether or not the input string starts with the
+ // "0x" prefix.
+ boolean parseAsHex = shouldParseAsHex(valueString);
- // allow the string if it is the beginning of a valid string, allow it even though
+ // allow the string if it is the beginning of a valid string even though
// it doesn't evaluate to a number yet.
if (isValidPrefix(valueString)) {
+ // When the input is valid, update the hex mode to match how the text was parsed.
+ // See parseAsHex variable comment above.
+ isHexMode = parseAsHex;
return true;
}
// otherwise, it must parse to a number to be valid.
try {
- BigInteger value = computeValueFromString(valueString);
- if (!allowsNegative && value != null && value.signum() < 0) {
+ BigInteger value = computeValueFromString(valueString, parseAsHex);
+ if (isNonAllowedNegativeNumber(value)) {
return false;
}
- return passesMaxCheck(value);
+ if (passesMaxCheck(value)) {
+ // When the input is valid, update the hex mode to match how the text was parsed.
+ // See parseAsHex variable comment above.
+ isHexMode = parseAsHex;
+ return true;
+ }
}
catch (NumberFormatException e) {
return false;
}
-
+ return false;
}
// Retrieves the current document text from inside the document filter.
@@ -585,6 +634,18 @@ public class IntegerTextField {
}
+ private boolean isNonAllowedNegativeNumber(BigInteger value) {
+ if (value == null) {
+ return false;
+ }
+ if (allowsNegative) {
+ return false;
+ }
+
+ // so we don't allow negatives
+ return value.signum() < 0;
+ }
+
/**
* Overrides the JTextField mainly to allow hint painting for the current radix mode.
*/
diff --git a/Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/textfield/IntegerTextFieldTest.java b/Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/textfield/IntegerTextFieldTest.java
index d708536be5..138e229656 100644
--- a/Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/textfield/IntegerTextFieldTest.java
+++ b/Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/textfield/IntegerTextFieldTest.java
@@ -15,16 +15,23 @@
*/
package docking.widgets.textfield;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
-import javax.swing.*;
+import javax.swing.JFrame;
+import javax.swing.JTextField;
+import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
-import org.junit.*;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
import docking.test.AbstractDockingTest;
@@ -271,6 +278,44 @@ public class IntegerTextFieldTest extends AbstractDockingTest {
assertEquals("0xff", field.getText());
}
+ @Test
+ public void testPastingBadText() {
+ field.setHexMode();
+ field.setValue(0);
+ assertFalse(field.setText("asdf 0x azzz"));
+ }
+
+ @Test
+ public void testSetText() {
+ field.setHexMode();
+ field.setValue(0);
+ assertTrue(field.setText("0x15"));
+ assertEquals(0x15, field.getIntValue());
+ }
+
+ @Test
+ public void testSetTextWithInvalidValue() {
+ field.setHexMode();
+ field.setValue(0);
+ assertFalse(field.setText("bad value"));
+ assertEquals(0, field.getIntValue());
+ assertTrue(field.isHexMode());
+ }
+
+ @Test
+ public void testSucessfulSetTextChangesHexMode() {
+ field.setHexMode();
+ field.setValue(0);
+ assertTrue(field.setText("33"));
+ assertEquals(33, field.getIntValue());
+ assertFalse(field.isHexMode());
+
+ assertTrue(field.setText("0x33"));
+ assertEquals(0x33, field.getIntValue());
+ assertTrue(field.isHexMode());
+
+ }
+
private void setHexMode() {
runSwing(() -> field.setHexMode());
waitForSwing();