Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3310 Fixed defined include file names and pre-processor constants with size specification of UL and LL

emteere06bc8ddd6b58
diff --git a/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj b/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj
index 4db24f649d..7ca754faf2 100644
--- a/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj
+++ b/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj
@@ -835,9 +835,17 @@ public class PreProcessor {
String fn = new String(ft);
// get rid of any errant quoting or whitespace
fn = fn.trim();
- if (fn.charAt(0) == '\"' && fn.endsWith("\"")) {
- fn = fn.substring(1, fn.length() - 1);
+
+ // Check for quoted include file name, protects embedded spaces
+ if (fn.startsWith("\"") && fn.indexOf('\"', 1) > 0) {
+ fn = fn.substring(1, fn.indexOf('\"', 1));
+ }
+
+ // Check for <> include file name, protects embedded spaces
+ if (fn.startsWith("<") && fn.indexOf('>', 1) > 0) {
+ fn = fn.substring(1, fn.indexOf('>', 1));
}
+
FileInputStream fis = null;
File iFile = null;
int i;
@@ -2475,7 +2483,7 @@ TOKEN:
<#NOTWWSQLT: ~[" ","\t","\n","\r","<","\""]> |
<#NOTWSQLT: ~["\t","\n","\r","<","\""]> |
<#NOTVALCMT: ("/##/")> |
- <INTEGER_LITERAL: <DECIMAL_LITERAL> (["l","L"])? | <HEX_LITERAL> (["l","L","U"])* | <OCTAL_LITERAL> (["l","L"])*> |
+ <INTEGER_LITERAL: <DECIMAL_LITERAL> (["u","l","L","U"])* | <HEX_LITERAL> (["u","l","L","U"])* | <OCTAL_LITERAL> (["u","l","L","U"])*> |
<#DECIMAL_LITERAL: ["0"-"9"] (["0"-"9"])*> |
<#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+> |
<#HEX_DIGIT: (["0"-"9","a"-"f","A"-"F"])> |
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java b/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java
index 8fab182152..1b77911757 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java
@@ -32,7 +32,10 @@ public class PreProcessorTest extends AbstractGenericTest {
private static String resourceName = "PreProcessorTest.h";
private static CategoryPath path =
new CategoryPath(new CategoryPath("/PreProcessorTest.h"), "defines");
-
+
+ private static CategoryPath definedPath =
+ new CategoryPath(new CategoryPath("/defined.h"), "defines");
+
// must get rid of after all tests
private static StandAloneDataTypeManager dtMgr;
private static ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -399,6 +402,13 @@ public class PreProcessorTest extends AbstractGenericTest {
assertEquals("No INCLUDE5 define", "INCLUDE5", defval);
}
+ @Test
+ public void testDefinedInclude() {
+ defname = "DID_INCLUDE_DEFINED_INCLUDED";
+ value = 1;
+ checkDefine(dtMgr, definedPath, value, defname);
+ }
+
@Test
public void testVarags() {
defname = "EPRINTF_VARARGS";
diff --git a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h
index 1269589eb2..0bd12bb0b9 100644
--- a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h
+++ b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h
@@ -76,14 +76,18 @@
#define DID_FILE_ISDEF_DEF 1
#endif
-#include "multinclude.h"
+#include <multinclude.h> /* include once */
-#include "multinclude.h"
+#include "multinclude.h" /* include twice */
#include "multinclude.h"
#include "multinclude.h"
+#define __DEFINED_INCLUDE <defined.h>
+
+#include __DEFINED_INCLUDE /* THIS SHOULD BE IGNORED <> */
+
#define __TEXT(quote) quote
#define TEXT(quote) __TEXT(quote)
diff --git a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/defined.h b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/defined.h
new file mode 100644
index 0000000000..7113e5734b
--- /dev/null
+++ b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/defined.h
@@ -0,0 +1,20 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define DID_INCLUDE_DEFINED_INCLUDED 1LL
+
+#if DID_INCLUDE_DEFINED_INCLUDED == 1UL
+#define UINT64_C(n) n ## ULL
+#endif