Skip to content

Ghidra is a software reverse engineering (SRE) framework

Fix BSTRs still leaked due to ref counting issue

Paul Moranfaeedb9d2e5e
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
index 45851e116e..c9b587e128 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
@@ -36,11 +36,13 @@ std::wstring findMangledName(PDBApiContext& ctx, IDiaSymbol& pFunction) {
void findNameInNamespace(PDBApiContext& ctx, const std::wstring& name, IDiaSymbol& pnamespace )
{
- bstr_t bstrNamespace;
- if (FAILED(pnamespace.get_name(bstrNamespace.GetAddress()))) {
+ BSTR temp = NULL;
+ if (FAILED(pnamespace.get_name(&temp))) {
fatal("Namespace get_name failed");
}
+ bstr_t bstrNamespace(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 7505789e3a..427193be6c 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
@@ -282,13 +282,15 @@ void dumpFunctionLines( IDiaSymbol& pSymbol, IDiaSession& pSession )
if ( isect == 0 || length <= 0 ) {
return;
}
+
CComPtr<IDiaEnumLineNumbers> pLines;
if (FAILED(pSession.findLinesByAddr( isect, offset, static_cast<DWORD>( length ), &pLines ))) {
return;
}
- CComPtr<IDiaLineNumber> pLine;
+
DWORD celt = 0;
while ( 1 ) {
+ CComPtr<IDiaLineNumber> pLine;
if (FAILED(pLines->Next( 1, &pLine, &celt ))) {
break;
}
@@ -302,8 +304,9 @@ void dumpFunctionLines( IDiaSymbol& pSymbol, IDiaSession& pSession )
CComPtr<IDiaSourceFile> pSrc;
pLine->get_sourceFile( &pSrc );
- bstr_t sourceFileName ;
- pSrc->get_fileName( sourceFileName.GetAddress() );
+ BSTR temp = NULL;
+ pSrc->get_fileName( &temp);
+ bstr_t sourceFileName(temp);
DWORD addr = 0;
pLine->get_relativeVirtualAddress( &addr );
@@ -315,8 +318,6 @@ void dumpFunctionLines( IDiaSymbol& pSymbol, IDiaSession& pSession )
printf("%S<line_number source_file=\"%S\" start=\"0x%x\" end=\"0x%x\" addr=\"0x%x\" /> \n",
indent(12).c_str(), sourceFileName.GetBSTR(), start, end, addr);
-
- pLine = NULL;
}
}
@@ -358,9 +359,8 @@ void iterateFunctions(PDBApiContext& ctx) {
void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
DWORD celt = 0;
- CComPtr<IDiaSymbol> pSymbol;
-
while ( 1 ) {
+ CComPtr<IDiaSymbol> pSymbol;
if (FAILED(pSymbols->Next( 1, &pSymbol, &celt ))) {
break;
}
@@ -384,8 +384,6 @@ void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
printf("value=\"%S\" ", getValue(*pSymbol).c_str());
printf("datatype=\"%S\" ", getTypeAsString(*pSymbol).c_str());
printf(" />\n");
-
- pSymbol = NULL;
}
}
@@ -394,8 +392,9 @@ void iterateSourceFiles(IDiaEnumSourceFiles * pSourceFiles) {
DWORD celt = 0;
CComPtr<IDiaSourceFile> pSourceFile;
while ( SUCCEEDED( hr = pSourceFiles->Next( 1, &pSourceFile, &celt ) ) && celt == 1 ) {
- bstr_t name;
- pSourceFile->get_fileName( name.GetAddress() );
+ BSTR temp = NULL;
+ pSourceFile->get_fileName( &temp );
+ bstr_t name(temp);
DWORD id = 0;
pSourceFile->get_uniqueId( &id );
if ( name.GetAddress() != NULL ) {
@@ -488,11 +487,13 @@ void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
break;
}
- bstr_t filename;
- pInjectedSrc->get_filename(filename.GetAddress());
+ BSTR fileNameTemp = NULL;
+ pInjectedSrc->get_filename(&fileNameTemp);
+ bstr_t filename(fileNameTemp);
- bstr_t objectname;
- pInjectedSrc->get_objectFilename(objectname.GetAddress());
+ BSTR objectNameTemp = NULL;
+ pInjectedSrc->get_objectFilename(&objectNameTemp);
+ bstr_t objectname(objectNameTemp);
DWORD crc;
pInjectedSrc->get_crc(&crc);
@@ -516,9 +517,9 @@ void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
*/
void iterateFrameData(IDiaEnumFrameData * pEnumFrameData) {
DWORD celt = 0;
- CComPtr<IDiaFrameData> pFrameData;
while ( 1 ) {
+ CComPtr<IDiaFrameData> pFrameData;
const HRESULT hr = pEnumFrameData->Next( 1, &pFrameData, &celt );
if (FAILED(hr)) {
break;
@@ -551,8 +552,10 @@ int iterateTables(PDBApiContext& ctx, bool printAll) {
if (celt != 1) {
break;
}
- bstr_t name;
- pTable->get_name( name.GetAddress() );
+
+ BSTR nameTemp;
+ pTable->get_name( &nameTemp );
+ bstr_t name(nameTemp);
printf("%S<table name=\"%ws\">\n", indent(8).c_str(), name.GetBSTR() );
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
index a3e8bbd008..affcfc0d26 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
@@ -109,7 +109,7 @@ std::wstring printVariant( VARIANT & v ) {
void printBound( IDiaSymbol& pBound ) {
DWORD tag = 0;
- bstr_t name;
+ BSTR nameTemp = NULL;
DWORD kind = 0;
pBound.get_symTag( &tag );
pBound.get_locationType( &kind );
@@ -119,7 +119,8 @@ void printBound( IDiaSymbol& pBound ) {
//pBound->get_value( &v );
//printVariant( v );
}
- else if ( pBound.get_name( name.GetAddress() ) == S_OK ) {
+ else if ( pBound.get_name( &nameTemp ) == S_OK ) {
+ bstr_t name(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 a4f047725f..3810b34614 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
@@ -175,8 +175,9 @@ const static std::wstring BASIC_TYPE_STRINGS [] = {
};
std::wstring getName(IDiaSymbol& pSymbol) {
- bstr_t name;
- if (SUCCEEDED(pSymbol.get_name(name.GetAddress()))) {
+ 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");
@@ -210,12 +211,15 @@ std::wstring getName(IDiaSymbol& pSymbol) {
}
std::wstring getUndecoratedName(IDiaSymbol& pSymbol) {
- bstr_t name;
- if (SUCCEEDED(pSymbol.get_undecoratedName( name.GetAddress() ))) {
- return escapeXmlEntities(std::wstring(name.GetBSTR(), name.length()));
+ 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);
+ return escapeXmlEntities(std::wstring(name.GetBSTR(), name.length()));
}
return L"";
}
+
DWORD getRVA(IDiaSymbol& pSymbol) {
DWORD rva = 0;
pSymbol.get_relativeVirtualAddress( &rva );
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
index 9dc6fed2be..a922601695 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
@@ -22,35 +22,8 @@ std::wstring indent(size_t nSpaces) {
std::wstring escapeXmlEntities(const std::wstring& str) {
- // Scan source str for problematic characters that need escaping.
- // Calculate how many characters we will need in new string.
- // The cases in this switch() statement need to match the cases in the following switch()
- // statement.
- const size_t len = str.length();
- size_t destLen = 0;
- for (int i = 0; i < len; ++i) {
- switch (str[i]) {
- case '&':
- destLen += 5; // length of: "&amp;"
- break;
- case '<':
- case '>':
- destLen += 4; // length of: "&lt;" or "&gt;"
- break;
- case '\'':
- case '"':
- destLen += 6; // length of: "&apos;" or "&quot;"
- break;
- case 0x7F:
- break;
- default:
- destLen++;
- break;
- }
- }
-
- std::wstring escaped(destLen, '\0');
- for (int i = 0 ; i < len ; ++i) {
+ std::wstring escaped;
+ for (int i = 0 ; i < str.length(); ++i) {
switch (str[i]) {
case '&' :
escaped += L"&amp;";