Ghidra is a software reverse engineering (SRE) framework
GP-2692 Minor fix for self referential structures that use struct keyword in stuct body
emteeref4e9dbb13a85
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.jjindex 9fe9ad8993..f06e66fbe0 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@@ -382,23 +382,28 @@ public class CParser {* @param name name of composite* @param comp composite most likely empty* @return composite with a name, possibly already declared- *- * @throws InvalidNameException bad name- * @throws DuplicateNameException duplicate name*/- private Composite defineForwardDeclaredComposite(Token symName, Composite comp)- throws InvalidNameException, DuplicateNameException {+ private Composite defineForwardDeclaredComposite(Token symName, Composite comp) {DataType dt = findAnyComposite(symName.image);if (dt instanceof Composite) {comp = (Composite) dt;}if (dt == null) {- comp.setName(symName.image);- comp = (Composite) addDef(composites, symName.image, comp);+ String nameStr = symName.image;+ try {+ comp.setName(nameStr);+ } catch (InvalidNameException e) {+ // This should not happen+ e.printStackTrace();+ } catch (DuplicateNameException e) {+ // This should not happen+ e.printStackTrace();+ }+ comp = (Composite) addDef(composites, nameStr, comp);}return comp;}-+/*** Define a named composite*@@ -406,28 +411,24 @@ public class CParser {* @param parentName possible parent name* @param comp the composite components* @return a new composite with the correct name and components- *- * @throws InvalidNameException invalid name- * @throws DuplicateNameException duplicate name*/- private Composite defineNamedComposite(Token name, Token parentName, Composite comp)- throws InvalidNameException, DuplicateNameException {-+ private Composite defineNamedComposite(Token name, Token parentName, Composite comp) {+String nameStr = name.image;-+DataType dt = findAnyComposite(nameStr);-+// if existing composite is foundboolean hasNoComponents = false;boolean hasSameSourceArchive = true;if (dt != null) {Composite dtComp = (Composite) dt;-+hasNoComponents = (dtComp.getNumDefinedComponents() == 0 ? true : false);-+hasSameSourceArchive = dt.getSourceArchive().equals(dtMgr.getLocalSourceArchive());}-+// make sure comp is a Composite, if existing dt is empty, in same category and archiveif (comp instanceof Composite && hasNoComponents && hasSameSourceArchive) {// existing composite was an empty placeholder@@ -436,7 +437,7 @@ public class CParser {dtcomp.replaceWith(comp);comp = dtcomp;} else {-+// check if a parent name was specified, and if so insert it// only single parent supported currentlyif (parentName != null) {@@ -448,13 +449,22 @@ public class CParser {comp.insert(0, superDt);}}- comp.setName(nameStr);+ try {+ comp.setName(nameStr);+ } catch (InvalidNameException e) {+ // This should not happen+ e.printStackTrace();+ } catch (DuplicateNameException e) {+ // This should not happen+ e.printStackTrace();+ }comp = (Composite) addDef(composites, nameStr, comp);}-+return comp;}+private DataType findAnyComposite(String name) {DataType dt = getDef(composites, name);if (dt != null) {@@ -1938,23 +1948,17 @@ DataType StructOrUnionSpecifier() : {comp= StructOrUnion()(LOOKAHEAD(3)- [ t= <IDENTIFIER> [ ":" parent=<IDENTIFIER>] ] "{" [StructDeclarationList(comp)] "}" [ AttributeSpecList() ]+ [ t= <IDENTIFIER> [ ":" parent=<IDENTIFIER>]+ { comp=defineNamedComposite(t, null, comp); } // no parent yet, since there are no guts+ ] "{" [StructDeclarationList(comp)] "}" [ AttributeSpecList() ]|sname= <IDENTIFIER>){- try {- if (t != null) {- comp = defineNamedComposite(t, parent, comp);- } else if (sname != null) {- comp = defineForwardDeclaredComposite(sname, comp);- }- } catch (InvalidNameException e) {- // TODO Auto-generated catch block- e.printStackTrace();- } catch (DuplicateNameException e) {- // TODO Auto-generated catch block- e.printStackTrace();+ if (t != null) {+ comp = defineNamedComposite(t, parent, comp);+ } else if (sname != null) {+ comp = defineForwardDeclaredComposite(sname, comp);}typedefParsingStack.pop();return comp;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.hindex 2af9d8710e..21c74a1021 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@@ -39,6 +39,19 @@ typedef struct SomeStruct {int finalMember;} SomeStruct;+/**+ * Test forward declaration+ **/+ typedef struct ThisStruct {+ ThisStruct *prev;+ struct ThisStruct *next;+} ThisStruct;++ typedef struct _ThatStruct {+ _ThatStruct *prev;+ struct ThatStruct *next;+} ThatStruct;+/*** Anonymous function parameter definitions@@ -1044,7 +1057,9 @@ unsigned char _interlockedbittestandset(long *Base, long Offset)return old;}-unsigned char _interlockedbittestandset2(long *Base, long Offset) { unsigned char old; __asm__ __volatile__ ("lock bts{l %[Offset],%[Base] | %[Base],%[Offset]} ; setc %[old]" : [old] "=qm" (old), [Base] "+m" (*Base) : [Offset] "I" "r" (Offset) : "memory", "cc"); return old; }+extern __inline__ __attribute__((__always_inline__,__gnu_inline__))+unsigned char _interlockedbittestandset64(long long volatile *Base, long long Offset) { unsigned char old; __asm__ __volatile__ ("lock bts{q %[Offset],%[Base] | %[Base],%[Offset]}" "\n\tsetc %[old]" : [old] "=qm" (old), [Base] "+m" (*Base) : [Offset] "J" "r" (Offset) : "memory" , "cc"); return old; }+/**/