Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1796 NULL ptr accesses on rare paths

caheckman45eeb7cfb235
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc
index e9c7344752..c8bfcb4c25 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc
@@ -763,8 +763,9 @@ ProtoModel *Architecture::parseProto(const Element *el)
ProtoModel *other = protoModels[res->getName()];
if (other != (ProtoModel *)0) {
+ string errMsg = "Duplicate ProtoModel name: " + res->getName();
delete res;
- throw LowlevelError("Duplicate ProtoModel name: "+res->getName());
+ throw LowlevelError(errMsg);
}
protoModels[res->getName()] = res;
return res;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.cc
index d6fc5792a1..d726945484 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.cc
@@ -2064,6 +2064,9 @@ GrammarToken::GrammarToken(void)
{
type = 0;
value.integer = 0;
+ lineno = -1;
+ colno = -1;
+ filenum = -1;
}
GrammarLexer::GrammarLexer(int4 maxbuffer)
@@ -2617,6 +2620,9 @@ CParse::CParse(Architecture *g,int4 maxbuf)
{
glb = g;
firsttoken = -1;
+ lineno = -1;
+ colno = -1;
+ filenum = -1;
lastdecls = (vector<TypeDeclarator *> *)0;
keywords["typedef"] = f_typedef;
keywords["extern"] = f_extern;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.y b/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.y
index 33141afdab..01f435b3a8 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.y
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/grammar.y
@@ -279,6 +279,9 @@ GrammarToken::GrammarToken(void)
{
type = 0;
value.integer = 0;
+ lineno = -1;
+ colno = -1;
+ filenum = -1;
}
GrammarLexer::GrammarLexer(int4 maxbuffer)
@@ -832,6 +835,9 @@ CParse::CParse(Architecture *g,int4 maxbuf)
{
glb = g;
firsttoken = -1;
+ lineno = -1;
+ colno = -1;
+ filenum = -1;
lastdecls = (vector<TypeDeclarator *> *)0;
keywords["typedef"] = f_typedef;
keywords["extern"] = f_extern;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/pcodecompile.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/pcodecompile.cc
index 7927bececa..49128f7e6e 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/pcodecompile.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/pcodecompile.cc
@@ -350,10 +350,7 @@ void PcodeCompile::newLocalDefinition(string *varname,uint4 size)
{ // Create a new temporary symbol (without generating any pcode)
VarnodeSymbol *sym;
- VarnodeTpl *tmpvn = buildTemporary();
- if (size != 0)
- tmpvn->setSize(ConstTpl(ConstTpl::real,size)); // Size was explicitly specified
- sym = new VarnodeSymbol(*varname,tmpvn->getSpace().getSpace(),tmpvn->getOffset().getReal(),tmpvn->getSize().getReal());
+ sym = new VarnodeSymbol(*varname,uniqspace,allocateTemp(),size);
addSymbol(sym);
delete varname;
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc
index cb7231c202..64ecda14c9 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc
@@ -1523,8 +1523,8 @@ void PrintC::pushEnumConstant(uintb val,const TypeEnum *ct,
/// If so push the string, if not return \b false to indicate a token was not pushed
/// \param val is the value of the given constant pointer
/// \param ct is the pointer data-type attached to the value
-/// \param vn is the Varnode holding the value
-/// \param op is the PcodeOp using the value
+/// \param vn is the Varnode holding the value (may be null)
+/// \param op is the PcodeOp using the value (may be null)
/// \return \b true if a quoted string was pushed to the RPN stack
bool PrintC::pushPtrCharConstant(uintb val,const TypePointer *ct,const Varnode *vn,const PcodeOp *op)
@@ -1532,7 +1532,10 @@ bool PrintC::pushPtrCharConstant(uintb val,const TypePointer *ct,const Varnode *
if (val==0) return false;
AddrSpace *spc = glb->getDefaultDataSpace();
uintb fullEncoding;
- Address stringaddr = glb->resolveConstant(spc,val,ct->getSize(),op->getAddr(),fullEncoding);
+ Address point;
+ if (op != (const PcodeOp *)0)
+ point = op->getAddr();
+ Address stringaddr = glb->resolveConstant(spc,val,ct->getSize(),point,fullEncoding);
if (stringaddr.isInvalid()) return false;
if (!glb->symboltab->getGlobalScope()->isReadOnly(stringaddr,1,Address()))
return false; // Check that string location is readonly
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc
index de3621aa40..d5ecdf9469 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc
@@ -365,16 +365,18 @@ void AddrSpaceManager::insertSpace(AddrSpace *spc)
}
if (nameTypeMismatch || duplicateName || duplicateId) {
+ string errMsg = "Space " + spc->getName();
+ if (nameTypeMismatch)
+ errMsg = errMsg + " was initialized with wrong type";
+ if (duplicateName)
+ errMsg = errMsg + " was initialized more than once";
+ if (duplicateId)
+ errMsg = errMsg + " was assigned as id duplicating: "+baselist[spc->index]->getName();
if (spc->refcount == 0)
delete spc;
spc = (AddrSpace *)0;
+ throw LowlevelError(errMsg);
}
- if (nameTypeMismatch)
- throw LowlevelError("Space "+spc->getName()+" was initialized with wrong type");
- if (duplicateName)
- throw LowlevelError("Space "+spc->getName()+" was initialized more than once");
- if (duplicateId)
- throw LowlevelError("Space "+spc->getName()+" was assigned as id duplicating: "+baselist[spc->index]->getName());
baselist[spc->index] = spc;
spc->refcount += 1;
assignShortcut(spc);