From b10d01a2cb9d4eff300cb3ac031f8096d0d29b0a Mon Sep 17 00:00:00 2001 From: emteere <47253321+emteere@users.noreply.github.com> Date: Sun, 19 Mar 2023 19:17:58 +0000 Subject: [PATCH] GP-3216 Fixed CParser creation of Enums from define's with upper and lower case u,l,ul when value surrounded by parentheses --- .../program/util/AddressEvaluatorTest.java | 9 +++++++ .../app/util/cparser/PreProcessorTest.java | 26 ++++++++++++++++++- .../app/util/cparser/PreProcessorTest.h | 8 ++++++ .../ghidra/program/util/AddressEvaluator.java | 7 ++--- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.java index 0b3eb063aa..a91bfd4cb6 100644 --- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.java +++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.java @@ -76,6 +76,15 @@ public class AddressEvaluatorTest extends AbstractGhidraHeadedIntegrationTest { assertEquals(addr("0x1"), AddressEvaluator.evaluate(p, "(((0x1 | 0x2) & 0x2) >= 0x1)")); assertEquals(addr("0x0"), AddressEvaluator.evaluate(p, "(((0x1 | 0x2) & 0x2) <= 0x1)")); + + assertEquals(addr("0x4"), AddressEvaluator.evaluate(p, "(4ul)")); + assertEquals(addr("0x4"), AddressEvaluator.evaluate(p, "(4UL)")); + assertEquals(addr("0x4"), AddressEvaluator.evaluate(p, "( 4l )")); + assertEquals(addr("0x4"), AddressEvaluator.evaluate(p, "(4L)")); + assertEquals(addr("0x4"), AddressEvaluator.evaluate(p, "(4u )")); + assertEquals(addr("0x4"), AddressEvaluator.evaluate(p, "( 4U)")); + + assertEquals(null, AddressEvaluator.evaluate(p, "( 4P)")); Symbol s = p.getSymbolTable().createLabel(addr("0x100"), "entry", SourceType.IMPORTED); Address a = s.getAddress(); 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 283d37fd39..230058bc59 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 @@ -117,7 +117,7 @@ public class PreProcessorTest extends AbstractGenericTest { public void testDefines() throws Exception { long value; String defname; - + value = 32516; defname = "DefVal1"; checkDefine(dtMgr, path, value, defname); @@ -157,7 +157,31 @@ public class PreProcessorTest extends AbstractGenericTest { value = ((0x7fff) * 900L / 1000); defname = "DefVal10"; checkDefine(dtMgr, path, value, defname); + + value = 1; + defname = "DefVal_1L"; + checkDefine(dtMgr, path, value, defname); + value = 2; + defname = "DefVal_2l"; + checkDefine(dtMgr, path, value, defname); + + value = 3; + defname = "DefVal_3U"; + checkDefine(dtMgr, path, value, defname); + + value = 4; + defname = "DefVal_4u"; + checkDefine(dtMgr, path, value, defname); + + value = 5; + defname = "DefVal_5UL"; + checkDefine(dtMgr, path, value, defname); + + value = 6; + defname = "DefVal_6ul"; + checkDefine(dtMgr, path, value, defname); + value = 0; defname = "TOO_MANY_FISH"; checkDefine(dtMgr, path, value, defname); 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 dfd37c4996..4b9c42294c 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 @@ -19,6 +19,7 @@ #undef a #endif + /* definition coming from -D, should evaluate to true */ #if FROM_ARG_VALUE #define DID_ARG_VALUE 1 @@ -218,6 +219,13 @@ int TEST_FAILED; #define DefVal10 ((0x7fff) * 900L / 1000) +#define DefVal_1L (1L) +#define DefVal_2l (2l) +#define DefVal_3U (3U ) +#define DefVal_4u ( 4u) +#define DefVal_5UL ( 5UL ) +#define DefVal_6ul (6ul) + #define BIGNUM 64 * 16 + 16 #define ImOctal 01234567 diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.java index c5df6c6cc9..12d98bcac8 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.java @@ -257,7 +257,7 @@ public class AddressEvaluator { if (globalSymbols.size() == 1) { return globalSymbols.get(0).getAddress(); } - return null; + return getValueObject(tok); } private static Object getValueObject(String strValue) { @@ -268,10 +268,11 @@ public class AddressEvaluator { start = 2; radix = 16; } - if (strValue.endsWith("UL")) { + strValue = strValue.toLowerCase(); + if (strValue.endsWith("ul")) { strValue = strValue.substring(start, strValue.length() - 2); } - else if (strValue.endsWith("L") || strValue.endsWith("l") || strValue.endsWith("U")) { + else if (strValue.endsWith("l") || strValue.endsWith("u")) { strValue = strValue.substring(start, strValue.length() - 1); } else {