Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3002: Handling comments with null characters

Ryan Kurtz575e64b45a6c
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentCmd.java
index 61b297d566..f153b021ac 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentCmd.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentCmd.java
@@ -15,6 +15,7 @@
*/
package ghidra.app.cmd.comments;
+import ghidra.app.util.viewer.field.CommentUtils;
import ghidra.framework.cmd.Command;
import ghidra.framework.model.DomainObject;
import ghidra.program.model.address.Address;
@@ -44,9 +45,6 @@ public class SetCommentCmd implements Command {
cmdName = comment == null ? "Delete Comment" : "Set Comment";
}
- /**
- * @see ghidra.framework.cmd.Command#getName()
- */
@Override
public String getName() {
return cmdName;
@@ -68,19 +66,19 @@ public class SetCommentCmd implements Command {
return !oldValue.equals(newValue);
}
- /**
- * @see ghidra.framework.cmd.Command#applyTo(ghidra.framework.model.DomainObject)
- */
@Override
public boolean applyTo(DomainObject obj) {
- CodeUnit cu = getCodeUnit((Program) obj);
+ Program program = (Program) obj;
+ CodeUnit cu = getCodeUnit(program);
if (cu == null) {
message = "No Instruction or Data found for address " + address.toString() +
" Is this address valid?";
return false;
}
- if (commentChanged(cu.getComment(commentType), comment)) {
- cu.setComment(commentType, comment);
+ String updatedComment = CommentUtils.fixupAnnoations(comment, program);
+ updatedComment = CommentUtils.sanitize(updatedComment);
+ if (commentChanged(cu.getComment(commentType), updatedComment)) {
+ cu.setComment(commentType, updatedComment);
}
return true;
}
@@ -104,9 +102,6 @@ public class SetCommentCmd implements Command {
return cu;
}
- /**
- * @see ghidra.framework.cmd.Command#getStatusMsg()
- */
@Override
public String getStatusMsg() {
return message;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentsCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentsCmd.java
index d666f55985..f4f7c316f5 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentsCmd.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentsCmd.java
@@ -91,23 +91,28 @@ public class SetCommentsCmd implements Command {
if (cu != null) {
if (commentChanged(cu.getComment(CodeUnit.PRE_COMMENT), preComment)) {
String updatedPreComment = CommentUtils.fixupAnnoations(preComment, program);
+ updatedPreComment = CommentUtils.sanitize(updatedPreComment);
cu.setComment(CodeUnit.PRE_COMMENT, updatedPreComment);
}
if (commentChanged(cu.getComment(CodeUnit.POST_COMMENT), postComment)) {
String updatedPostComment = CommentUtils.fixupAnnoations(postComment, program);
+ updatedPostComment = CommentUtils.sanitize(updatedPostComment);
cu.setComment(CodeUnit.POST_COMMENT, updatedPostComment);
}
if (commentChanged(cu.getComment(CodeUnit.EOL_COMMENT), eolComment)) {
String updatedEOLComment = CommentUtils.fixupAnnoations(eolComment, program);
+ updatedEOLComment = CommentUtils.sanitize(updatedEOLComment);
cu.setComment(CodeUnit.EOL_COMMENT, updatedEOLComment);
}
if (commentChanged(cu.getComment(CodeUnit.PLATE_COMMENT), plateComment)) {
String updatedPlateComment = CommentUtils.fixupAnnoations(plateComment, program);
+ updatedPlateComment = CommentUtils.sanitize(updatedPlateComment);
cu.setComment(CodeUnit.PLATE_COMMENT, updatedPlateComment);
}
if (commentChanged(cu.getComment(CodeUnit.REPEATABLE_COMMENT), repeatableComment)) {
String updatedRepeatableComment =
CommentUtils.fixupAnnoations(repeatableComment, program);
+ updatedRepeatableComment = CommentUtils.sanitize(updatedRepeatableComment);
cu.setComment(CodeUnit.REPEATABLE_COMMENT, updatedRepeatableComment);
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/CommentUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/CommentUtils.java
index 56d3b81b3a..4b5876d8fd 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/CommentUtils.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/CommentUtils.java
@@ -136,6 +136,24 @@ public class CommentUtils {
Function<Annotation, Annotation> noFixing = Function.identity();
return doParseTextForAnnotations(text, noFixing, program, prototypeString, row);
}
+
+ /**
+ * Sanitizes the given text, removing or replacing illegal characters.
+ * <p>
+ * Each illegal character is handled as follows:
+ * <ul>
+ * <li>null character (\0) -> remove</li>
+ * </ul>
+ *
+ * @param text The text to sanitize
+ * @return The sanitized text, or null if the given text was null
+ */
+ public static String sanitize(String text) {
+ if (text == null) {
+ return null;
+ }
+ return text.replaceAll("\0", "");
+ }
/**
* Parses the given text looking for annotations.
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/comments/CommentsPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/comments/CommentsPluginTest.java
index 1cfc55023c..36d77501ad 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/comments/CommentsPluginTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/comments/CommentsPluginTest.java
@@ -750,6 +750,18 @@ public class CommentsPluginTest extends AbstractGhidraHeadedIntegrationTest {
!editAction.isEnabledForContext(browser.getProvider().getActionContext(null)));
}
+ @Test
+ public void testIllegalCharacters() throws Exception {
+ openX86ProgramInTool();
+ Address addr = addr(0x01006420);
+ CodeUnit cu = program.getListing().getCodeUnitAt(addr);
+
+ String illegal = "null\0 comment";
+ String legal = "null comment";
+ setAt(addr, CodeUnit.PRE_COMMENT, illegal, "OK");
+ assertEquals(legal, cu.getComment(CodeUnit.PRE_COMMENT));
+ }
+
private void setAt(Address addr, int commentType, String comment, String nameOfButtonToClick)
throws Exception {
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/CommentUtilsTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/CommentUtilsTest.java
index f17d41002f..b9d3ef0fc4 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/CommentUtilsTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/viewer/field/CommentUtilsTest.java
@@ -15,8 +15,7 @@
*/
package ghidra.app.util.viewer.field;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
import java.util.List;
@@ -108,4 +107,16 @@ public class CommentUtilsTest extends AbstractGhidraHeadlessIntegrationTest {
WordLocation word = annotations.get(0);
assertEquals("{@symbol symbol\\{Name\\}}", word.getWord());
}
+
+ @Test
+ public void testSanitize() {
+
+ String comment = null;
+ String sanitized = CommentUtils.sanitize(comment);
+ assertNull(sanitized);
+
+ comment = "Null\0 character test";
+ sanitized = CommentUtils.sanitize(comment);
+ assertEquals(sanitized, "Null character test");
+ }
}