Skip to content

Ghidra is a software reverse engineering (SRE) framework

Use Attach on bstr_t otherwise the ref count can be wrong and it can still be leaked. Reserve string space in escapeXmlEntities to improve performance.

Paul Moran81b0f31f0343
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
index c9b587e128..c81b26ae10 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
@@ -41,7 +41,8 @@ void findNameInNamespace(PDBApiContext& ctx, const std::wstring& name, IDiaSymbo
fatal("Namespace get_name failed");
}
- bstr_t bstrNamespace(temp);
+ bstr_t bstrNamespace;
+ bstrNamespace.Attach(temp);
const std::wstring strNamespace(bstrNamespace.GetBSTR(), bstrNamespace.length());
const std::wstring fullName = strNamespace + L"::" + name;
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp b/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
index 427193be6c..06201b422a 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
@@ -141,13 +141,13 @@ void iterateDataTypes(PDBApiContext& ctx) {
void iterateTypedefs(PDBApiContext& ctx) {
DWORD celt = 0;
CComPtr<IDiaEnumSymbols> pEnum;
- CComPtr<IDiaSymbol> pSymbol;
ctx.Global().findChildren(SymTagTypedef, NULL, nsNone/*nsfCaseInsensitive|nsfUndecoratedName*/, &pEnum);
if (pEnum == NULL) {
return;
}
printf("%S<typedefs>\n", indent(4).c_str());
while ( 1 ) {
+ CComPtr<IDiaSymbol> pSymbol;
if (FAILED(pEnum->Next( 1, &pSymbol, &celt ))) {
break;
}
@@ -161,8 +161,6 @@ void iterateTypedefs(PDBApiContext& ctx) {
}
printf("%S<typedef name=\"%S\" basetype=\"%S\" />\n", indent(8).c_str(), getName(*pSymbol).c_str(), getTypeAsString(*pSymbol).c_str());
-
- pSymbol = 0;
}
printf("%S</typedefs>\n", indent(4).c_str());
}
@@ -305,8 +303,9 @@ void dumpFunctionLines( IDiaSymbol& pSymbol, IDiaSession& pSession )
pLine->get_sourceFile( &pSrc );
BSTR temp = NULL;
- pSrc->get_fileName( &temp);
- bstr_t sourceFileName(temp);
+ pSrc->get_fileName(&temp);
+ bstr_t sourceFileName;
+ sourceFileName.Attach(temp);
DWORD addr = 0;
pLine->get_relativeVirtualAddress( &addr );
@@ -372,7 +371,6 @@ void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
if (name.empty()) {
continue;
}
-
printf("%S", indent(12).c_str());
printf("<symbol name=\"%S\" ", name.c_str());
printf("address=\"0x%x\" ", getRVA(*pSymbol));
@@ -394,7 +392,8 @@ void iterateSourceFiles(IDiaEnumSourceFiles * pSourceFiles) {
while ( SUCCEEDED( hr = pSourceFiles->Next( 1, &pSourceFile, &celt ) ) && celt == 1 ) {
BSTR temp = NULL;
pSourceFile->get_fileName( &temp );
- bstr_t name(temp);
+ bstr_t name;
+ name.Attach(temp);
DWORD id = 0;
pSourceFile->get_uniqueId( &id );
if ( name.GetAddress() != NULL ) {
@@ -489,11 +488,13 @@ void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
BSTR fileNameTemp = NULL;
pInjectedSrc->get_filename(&fileNameTemp);
- bstr_t filename(fileNameTemp);
+ bstr_t filename;
+ filename.Attach(fileNameTemp);
BSTR objectNameTemp = NULL;
pInjectedSrc->get_objectFilename(&objectNameTemp);
- bstr_t objectname(objectNameTemp);
+ bstr_t objectname;
+ objectname.Attach(objectNameTemp);
DWORD crc;
pInjectedSrc->get_crc(&crc);
@@ -543,9 +544,9 @@ int iterateTables(PDBApiContext& ctx, bool printAll) {
return hr;
}
- CComPtr<IDiaTable> pTable;
while ( 1 ) {
+ CComPtr<IDiaTable> pTable;
if (FAILED(pTables->Next( 1, &pTable, &celt ))) {
break;
}
@@ -555,7 +556,8 @@ int iterateTables(PDBApiContext& ctx, bool printAll) {
BSTR nameTemp;
pTable->get_name( &nameTemp );
- bstr_t name(nameTemp);
+ bstr_t name;
+ name.Attach(nameTemp);
printf("%S<table name=\"%ws\">\n", indent(8).c_str(), name.GetBSTR() );
@@ -588,7 +590,6 @@ int iterateTables(PDBApiContext& ctx, bool printAll) {
}
printf("%S</table>\n", indent(8).c_str());
- pTable = NULL;
}
printf("%S</tables>\n", indent(4).c_str());
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
index affcfc0d26..fc5bd0b604 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
@@ -120,7 +120,8 @@ void printBound( IDiaSymbol& pBound ) {
//printVariant( v );
}
else if ( pBound.get_name( &nameTemp ) == S_OK ) {
- bstr_t name(nameTemp);
+ bstr_t name;
+ name.Attach(nameTemp);
printf( "%ws", name.GetBSTR() );
}
}
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp b/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
index 3810b34614..c25e7fef7c 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
@@ -176,45 +176,47 @@ const static std::wstring BASIC_TYPE_STRINGS [] = {
std::wstring getName(IDiaSymbol& pSymbol) {
BSTR temp = NULL;
- if (SUCCEEDED(pSymbol.get_name(&temp))) {
- bstr_t name(temp);
- const std::wstring wstrName = std::wstring(name.GetBSTR(), name.length());
- if (wstrName.empty()) {
- return escapeXmlEntities(L"NONAME");
- }
+ if (SUCCEEDED(pSymbol.get_name(&temp))) {
+ bstr_t name;
+ name.Attach(temp);
+ const std::wstring wstrName = std::wstring(name.GetBSTR(), name.length());
+ if (wstrName.empty()) {
+ return escapeXmlEntities(L"NONAME");
+ }
- if (wstrName.find(L"unnamed-tag") != std::wstring::npos) {
- DWORD symIndexId = 0;
- if (FAILED(pSymbol.get_symIndexId(&symIndexId))) {
- symIndexId = 0;
- }
- const size_t length = 20; // length of: "<unnamed_NNNN>\0" + 1 extra
- std::vector<wchar_t> str(20);
- swprintf_s(str.data(), length, L"<unnamed_%04x>", symIndexId);
- return escapeXmlEntities(str.data());
- }
-
- DWORD locType = 0;
- ULONGLONG len = 0;
- if (SUCCEEDED(pSymbol.get_locationType(&locType)) &&
- locType == LocIsBitField &&
- SUCCEEDED(pSymbol.get_length(&len))) {
- const size_t length = wstrName.length() + 4 + 32; // length of: name + ":0x\0" + wag_hex_numeric_str_len
- std::vector<wchar_t> str(length);
- swprintf_s(str.data(), length, L"%ws:0x%I64x", wstrName.c_str(), len);
- return escapeXmlEntities(str.data());
- }
+ if (wstrName.find(L"unnamed-tag") != std::wstring::npos) {
+ DWORD symIndexId = 0;
+ if (FAILED(pSymbol.get_symIndexId(&symIndexId))) {
+ symIndexId = 0;
+ }
+ const size_t length = 20; // length of: "<unnamed_NNNN>\0" + 1 extra
+ std::vector<wchar_t> str(20);
+ swprintf_s(str.data(), length, L"<unnamed_%04x>", symIndexId);
+ return escapeXmlEntities(str.data());
+ }
- return escapeXmlEntities(wstrName);
- }
- return std::wstring();
+ DWORD locType = 0;
+ ULONGLONG len = 0;
+ if (SUCCEEDED(pSymbol.get_locationType(&locType)) &&
+ locType == LocIsBitField &&
+ SUCCEEDED(pSymbol.get_length(&len))) {
+ const size_t length = wstrName.length() + 4 + 32; // length of: name + ":0x\0" + wag_hex_numeric_str_len
+ std::vector<wchar_t> str(length);
+ swprintf_s(str.data(), length, L"%ws:0x%I64x", wstrName.c_str(), len);
+ return escapeXmlEntities(str.data());
+ }
+
+ return escapeXmlEntities(wstrName);
+ }
+ return std::wstring();
}
std::wstring getUndecoratedName(IDiaSymbol& pSymbol) {
BSTR temp = NULL;
if (pSymbol.get_undecoratedName(&temp) == S_OK) {
// May also return S_FALSE which is not failure, however in this case there is no name
- bstr_t name(temp);
+ bstr_t name;
+ name.Attach(temp);
return escapeXmlEntities(std::wstring(name.GetBSTR(), name.length()));
}
return L"";
@@ -237,7 +239,11 @@ DWORD getTag(IDiaSymbol& pSymbol) {
}
std::wstring getTagAsString(IDiaSymbol& pSymbol) {
- return SYMBOL_TAG_STRINGS[getTag(pSymbol)];
+ const DWORD tag = getTag(pSymbol);
+ if (tag > _countof(SYMBOL_TAG_STRINGS)) {
+ return L"";
+ }
+ return SYMBOL_TAG_STRINGS[tag];
}
DWORD getKind(IDiaSymbol& pSymbol) {
DWORD kind = 0;
@@ -252,9 +258,17 @@ DWORD getUdtKind(IDiaSymbol& pSymbol) {
std::wstring getKindAsString(IDiaSymbol& pSymbol) {
const DWORD tag = getTag(pSymbol);
if (tag == SymTagUDT) {
- return UDT_KIND_STRINGS[getUdtKind(pSymbol)];
+ const DWORD kind = getUdtKind(pSymbol);
+ if (kind < _countof(UDT_KIND_STRINGS)) {
+ return UDT_KIND_STRINGS[kind];
+ }
+ return L"";
+ }
+ const DWORD dataKind = getKind(pSymbol);
+ if (dataKind < _countof(DATA_KIND_STRINGS)) {
+ return DATA_KIND_STRINGS[dataKind];
}
- return DATA_KIND_STRINGS[getKind(pSymbol)];
+ return L"";
}
LONG getOffset(IDiaSymbol& pSymbol) {
@@ -352,7 +366,10 @@ std::wstring getBaseTypeAsString(IDiaSymbol& pSymbol) {
}
break;
}
- return escapeXmlEntities(BASIC_TYPE_STRINGS[bt]);
+ if (bt < _countof(BASIC_TYPE_STRINGS)) {
+ return escapeXmlEntities(BASIC_TYPE_STRINGS[bt]);
+ }
+ return L"";
}
bool isScopeSym( DWORD tag )
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
index a922601695..f42c2098a8 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
@@ -23,6 +23,8 @@ std::wstring indent(size_t nSpaces) {
std::wstring escapeXmlEntities(const std::wstring& str) {
std::wstring escaped;
+ escaped.reserve(str.length() * 2);
+
for (int i = 0 ; i < str.length(); ++i) {
switch (str[i]) {
case '&' :