Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1964 fixed sizeof(ptr->member) and sizeof(p.member), was parsing only simple identifier and failing.

emteere2c60874b04a3
diff --git a/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/C/C.jj b/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/C/C.jj
index d7de5bf506..a3b20e649e 100644
--- a/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/C/C.jj
+++ b/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/C/C.jj
@@ -2646,7 +2646,6 @@ Object CastExpression() : {
Object UnaryExpression() : {
Object obj = null;
Token op = null;
- Token id = null;
Declaration dec = null;
}
{
@@ -2670,7 +2669,7 @@ Object UnaryExpression() : {
|
<SIZEOF>
(
- "(" ( dec = TypeName() | id = <IDENTIFIER> | obj = ConstantExpression() ) ")"
+ "(" ( dec = TypeName() | obj = UnaryExpression() ) ")"
{
if (obj != null && obj instanceof String) {
obj = Long.valueOf(((String) obj).length() - 1); // will include "" plus \0
@@ -2678,7 +2677,7 @@ Object UnaryExpression() : {
else if (dec != null) {
obj = Long.valueOf(dec.getDataType().getLength());
}
- else if (id != null) {
+ else if (obj != null) {
// TODO: try to look up the type of the identifier
// TODO: Throw error if identifier is not defined
// may need to actually track declarations!
diff --git a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/CParserTest.h b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/CParserTest.h
index 3c40dc41de..d95a084d29 100644
--- a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/CParserTest.h
+++ b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/CParserTest.h
@@ -29,16 +29,6 @@
- struct _IO_FILE_complete
-{
- size_t __pad5;
- int _mode;
- /* Make sure we don't get into trouble again. */
- char _unused2[15 * sizeof (int) - 4 * sizeof (void **) - sizeof (size_t)];
-};
-
-
-
/**
** use of long as an attribute
**/
@@ -51,34 +41,34 @@ typedef long unsigned long int LULI_size_t;
typedef unsigned long long int ULLI_size_t;
typedef long long unsigned int LLUI_size_t;
typedef unsigned int UI_size_t;
-
+
/**
** pragma usage
**/
int (__stdcall * GetSectionBlock) (
- int section,
- long len,
- long align=1,
+ int section,
+ long len,
+ long align=1,
void **ppBytes=0) ;
-
+
#pragma region Input compatibility macros
-
+
#pragma warning(disable)
-
+
#pragma warning(disable:4035 4793) // re-enable below
-
+
#pragma section(".CRTMP$XCA",long,read)
-
-
+
+
/**
** Packing tests
**/
#pragma pack(push,2)
-
+
#pragma pack(push, PlaceHolder)
-
+
#pragma pack(push, 4)
-
+
__pragma(pack(push, MyName, 8))
struct __declspec(align(16)) __pragma(warning(push)) __pragma(warning(disable:4845)) __declspec(no_init_all) __pragma(warning(pop)) packed8 {
char a;
@@ -183,15 +173,15 @@ union wait
int w_status;
struct
{
- unsigned int t:7;
- unsigned int c:1;
- unsigned int r:8;
+ unsigned int t:7;
+ unsigned int c:1;
+ unsigned int r:8;
unsigned int:16;
} __wait_terminated;
struct
{
- unsigned int v:8;
- unsigned int s:8;
+ unsigned int v:8;
+ unsigned int s:8;
unsigned int:0;
unsigned int:8;
unsigned int:8;
@@ -253,7 +243,7 @@ __checkint(int val, int* err) {
if (val < (-2147483647-1) || val > 2147483647) {
*err |= OVR_ERR;
}
-
+
val = 8 * sizeof(val);
return (int32_t) val;
@@ -263,7 +253,7 @@ typedef enum {} EmptyEnum;
typedef struct fstruct;
-
+
typedef int (*fnptr)(struct fstruct);
@@ -383,6 +373,31 @@ typedef struct
} cpu_set_t;
+typedef struct testStruct
+{
+ char test[8];
+} *pTestStruct;
+
+int testFunction(pTestStruct ptr)
+{
+ struct cpu_set_t cpset;
+
+ int a = sizeof(ptr->test);
+ int b = sizeof(ptr);
+ int c = sizeof(cpset.__bits);
+
+ return a;
+}
+
+struct _IO_FILE_complete
+{
+ size_t __pad5;
+ int _mode;
+ /* Make sure we don't get into trouble again. */
+ char _unused2[15 * sizeof (int) - 4 * sizeof (void **) - sizeof (size_t)];
+};
+
+
void __mem_func (void *, char **, int ***, long (*) (size_t),
short *(*) (size_t),
void *(*) (void *, size_t));
@@ -624,7 +639,7 @@ typedef struct _arraysInStruct
unsigned int num;
unsigned long flexTwo[0];
} ArraysInStruct;
-
+
struct EmptyBuffer {
int x;
char buf[];
@@ -633,7 +648,7 @@ struct EmptyBuffer {
/**
** Array Size allocation with expression
- **/
+ **/
struct ivd {
char type[(1-1+1)];
char id[(6-2+1)];
@@ -666,19 +681,19 @@ enum options_enum {
PLUS_SET = 4 + 12,
MINUS_SET = 12 - 1,
-
+
SHIFTED1 = 1 << 1 >> 1,
-
+
SHIFTED3 = 7 >> 3 << 3,
-
+
SHIFTED4 = 15 >> 3 << 3,
-
+
ISONE = 1 - 1 + 1,
-
+
ISNEGATIVE = -5-1,
-
+
BIGNUM = 64 * 16 + 16,
-
+
TRINARY = (0 ? 10 : 11),
};
@@ -692,7 +707,7 @@ char * retInitedStorage(int i)
return (char *) (void) { 'a', 'b', 'c'};
}
-
+
typedef float __m128 __attribute__ ((__vector_size__ (16), __may_alias__));
@@ -707,11 +722,11 @@ _mm_set1_ps (float __F)
/**
** Structure Initializers / casting
- **/
+ **/
struct { int a[3], b; } w[] =
{
{ { 1, 0, 0 }, 0 },
- { { 2, 0, 0 }, 0 }
+ { { 2, 0, 0 }, 0 }
};
int *ptr = (int *){ 10, 20, 30, 40 };
@@ -761,9 +776,9 @@ typedef unsigned int __uint32_t;
**/
struct arm_exception_state
{
- __uint32_t exception;
- __uint32_t fsr;
- __uint32_t far;
+ __uint32_t exception;
+ __uint32_t fsr;
+ __uint32_t far;
};
@@ -898,9 +913,9 @@ typedef struct mystruct {
char lineInFunc(int i) {
#line 1 "first/line.h"
int j;
-
+
#line 2 "second/line.h"
return 'a';
#line 3 "third/line.h"
}
-
+