Skip to content

Ghidra is a software reverse engineering (SRE) framework

#597 fix many memory leaks/string handling issues. Still a few leaks left.

Paul Moran48fbde6216f0
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
index daf8ba9d0f..3cdb3d59ec 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
@@ -16,7 +16,7 @@
*/
#include "find.h"
-BSTR findMangledName(IDiaSymbol * pFunction) {
+std::wstring findMangledName(IDiaSymbol * pFunction) {
DWORD rva = getRVA(pFunction);
IDiaSymbol * pSymbol = NULL;
HRESULT hr = pSession->findSymbolByRVA(rva, SymTagPublicSymbol, &pSymbol);
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp b/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
index 9ade77de1c..0fd3493c18 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
@@ -31,9 +31,9 @@ void iterateEnumMembers(IDiaSymbol * pSymbol) {
if (celt != 1) {
break;
}
- BSTR name = getName(pMember);
- BSTR value = getValue(pMember);
- printf("%s<member name=\"%ws\" value=\"%ws\" />\n", indent(12), name, value);
+ std::wstring name = getName(pMember);
+ std::wstring value = getValue(pMember);
+ printf("%S<member name=\"%S\" value=\"%S\" />\n", indent(12).c_str(), name.c_str(), value.c_str());
pMember = 0;
}
}
@@ -46,7 +46,7 @@ void iterateEnums() {
if (pEnum == NULL) {
return;
}
- printf("%s<enums>\n", indent(4));
+ printf("%S<enums>\n", indent(4).c_str());
while ( 1 ) {
if (pEnum->Next( 1, &pSymbol, &celt ) < 0) {
break;
@@ -60,18 +60,18 @@ void iterateEnums() {
continue;
}
- BSTR name = getName(pSymbol);
- BSTR type = getTypeAsString(pSymbol);
+ std::wstring name = getName(pSymbol);
+ std::wstring type = getTypeAsString(pSymbol);
ULONGLONG len = getLength(pSymbol);
- printf("%s<enum name=\"%ws\" type=\"%ws\" length=\"0x%I64x\" >\n", indent(8), name, type, len);
+ printf("%S<enum name=\"%ws\" type=\"%ws\" length=\"0x%I64x\" >\n", indent(8).c_str(), name.c_str(), type.c_str(), len);
iterateEnumMembers(pSymbol);
- printf("%s</enum>\n", indent(8));
+ printf("%S</enum>\n", indent(8).c_str());
pSymbol = 0;
}
- printf("%s</enums>\n", indent(4));
+ printf("%S</enums>\n", indent(4).c_str());
}
void iterateMembers(IDiaSymbol * pSymbol) {
@@ -89,12 +89,14 @@ void iterateMembers(IDiaSymbol * pSymbol) {
if (celt != 1) {
break;
}
- printf("%s<member name=\"%ws\" datatype=\"%ws\" offset=\"0x%x\" kind=\"%ws\" length=\"0x%I64x\" />\n",
- indent(12),
- getName(pMember),
- getTypeAsString(pMember),
+
+ std::wstring name = getName(pMember);
+ printf("%S<member name=\"%ws\" datatype=\"%ws\" offset=\"0x%x\" kind=\"%ws\" length=\"0x%I64x\" />\n",
+ indent(12).c_str(),
+ name.c_str(),
+ getTypeAsString(pMember).c_str(),
getOffset(pMember),
- getKindAsString(pMember),
+ getKindAsString(pMember).c_str(),
getLength(pMember));
}
}
@@ -107,7 +109,7 @@ void iterateDataTypes() {
if (pEnum == NULL) {
return;
}
- printf("%s<datatypes>\n", indent(4));
+ printf("%S<datatypes>\n", indent(4).c_str());
while ( 1 ) {
if (pEnum->Next( 1, &pSymbol, &celt ) < 0) {
break;
@@ -125,22 +127,22 @@ void iterateDataTypes() {
continue;
}
- BSTR name = getName(pSymbol);
- BSTR kind = getKindAsString(pSymbol);
+ std::wstring name = getName(pSymbol);
+ std::wstring kind = getKindAsString(pSymbol);
ULONGLONG len = getLength(pSymbol);
if ( len == 0 ) {
continue;
}
- printf("%s<datatype name=\"%ws\" kind=\"%ws\" length=\"0x%I64x\" >\n", indent(8), name, kind, len);
+ printf("%S<datatype name=\"%ws\" kind=\"%ws\" length=\"0x%I64x\" >\n", indent(8).c_str(), name.c_str(), kind.c_str(), len);
iterateMembers(pSymbol);
- printf("%s</datatype>\n", indent(8));
+ printf("%S</datatype>\n", indent(8).c_str());
pSymbol = 0;
}
- printf("%s</datatypes>\n", indent(4));
+ printf("%S</datatypes>\n", indent(4).c_str());
}
void iterateTypedefs() {
@@ -151,7 +153,7 @@ void iterateTypedefs() {
if (pEnum == NULL) {
return;
}
- printf("%s<typedefs>\n", indent(4));
+ printf("%S<typedefs>\n", indent(4).c_str());
while ( 1 ) {
if (pEnum->Next( 1, &pSymbol, &celt ) < 0) {
break;
@@ -164,14 +166,14 @@ void iterateTypedefs() {
if (tag != SymTagTypedef) {//do not delete
continue;
}
- BSTR name = getName(pSymbol);
- BSTR type = getTypeAsString(pSymbol);
+ std::wstring name = getName(pSymbol);
+ std::wstring type = getTypeAsString(pSymbol);
- printf("%s<typedef name=\"%ws\" basetype=\"%ws\" />\n", indent(8), name, type);
+ printf("%S<typedef name=\"%ws\" basetype=\"%ws\" />\n", indent(8).c_str(), name.c_str(), type.c_str());
pSymbol = 0;
}
- printf("%s</typedefs>\n", indent(4));
+ printf("%S</typedefs>\n", indent(4).c_str());
}
void iterateClasses() {
@@ -182,7 +184,7 @@ void iterateClasses() {
if (pEnum == NULL) {
return;
}
- printf("%s<classes>\n", indent(4));
+ printf("%S<classes>\n", indent(4).c_str());
while ( 1 ) {
if (pEnum->Next( 1, &pSymbol, &celt ) < 0) {
break;
@@ -200,21 +202,21 @@ void iterateClasses() {
continue;
}
- BSTR name = getName(pSymbol);
+ std::wstring name = getName(pSymbol);
ULONGLONG len = getLength(pSymbol);
if ( len == 0 ) {
continue;
}
- printf("%s<class name=\"%ws\" length=\"0x%I64x\" >\n", indent(8), name, len);
+ printf("%S<class name=\"%ws\" length=\"0x%I64x\" >\n", indent(8).c_str(), name.c_str(), len);
iterateMembers(pSymbol);
- printf("%s</class>\n", indent(8));
+ printf("%S</class>\n", indent(8).c_str());
pSymbol = 0;
}
- printf("%s</classes>\n", indent(4));
+ printf("%S</classes>\n", indent(4).c_str());
}
void dumpFunctionStackVariables( DWORD rva )
@@ -235,12 +237,12 @@ void dumpFunctionStackVariables( DWORD rva )
while ( pEnum != NULL && SUCCEEDED( pEnum->Next( 1, &pSymbol, &celt ) ) && celt == 1 ) {
pSymbol->get_symTag( &tag );
if ( tag == SymTagData ) {
- printf("%s<stack_variable name=\"%ws\" kind=\"%ws\" offset=\"0x%x\" datatype=\"%ws\" length=\"0x%I64x\" />\n",
- indent(12),
- getName(pSymbol),
- getKindAsString(pSymbol),
+ printf("%S<stack_variable name=\"%ws\" kind=\"%ws\" offset=\"0x%x\" datatype=\"%ws\" length=\"0x%I64x\" />\n",
+ indent(12).c_str(),
+ getName(pSymbol).c_str(),
+ getKindAsString(pSymbol).c_str(),
getOffset(pSymbol),
- getTypeAsString(pSymbol),
+ getTypeAsString(pSymbol).c_str(),
getLength(pSymbol));
}
else if ( tag == SymTagAnnotation ) {
@@ -322,8 +324,8 @@ void dumpFunctionLines( IDiaSymbol* pSymbol, IDiaSession* pSession )
DWORD end;
pLine->get_lineNumberEnd( &end );
- printf("%s<line_number source_file=\"%ws\" start=\"0x%x\" end=\"0x%x\" addr=\"0x%x\" /> \n",
- indent(12), sourceFileName, start, end, addr);
+ printf("%S<line_number source_file=\"%ws\" start=\"0x%x\" end=\"0x%x\" addr=\"0x%x\" /> \n",
+ indent(12).c_str(), sourceFileName, start, end, addr);
pLine = NULL;
}
@@ -337,7 +339,7 @@ void iterateFunctions() {
if (pEnum == NULL) {
return;
}
- printf("%s<functions>\n", indent(4));
+ printf("%S<functions>\n", indent(4).c_str());
while ( 1 ) {
if (pEnum->Next( 1, &pSymbol, &celt ) < 0) {
break;
@@ -351,20 +353,20 @@ void iterateFunctions() {
continue;
}
- BSTR name = findMangledName(pSymbol);
+ std::wstring name = findMangledName(pSymbol);
DWORD address = getRVA(pSymbol);
ULONGLONG len = getLength(pSymbol);
- printf("%s<function name=\"%ws\" address=\"0x%x\" length=\"0x%I64x\">\n", indent(8), name, address, len);
+ printf("%S<function name=\"%ws\" address=\"0x%x\" length=\"0x%I64x\">\n", indent(8).c_str(), name.c_str(), address, len);
dumpFunctionStackVariables(address);
dumpFunctionLines(pSymbol, pSession);
- printf("%s</function>\n", indent(8));
+ printf("%S</function>\n", indent(8).c_str());
pSymbol = 0;
}
- printf("%s</functions>\n", indent(4));
+ printf("%S</functions>\n", indent(4).c_str());
}
void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
@@ -379,21 +381,21 @@ void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
break;
}
- BSTR name = getName(pSymbol);
- if (name == NULL) {
+ std::wstring name = getName(pSymbol);
+ if (name.empty()) {
continue;
}
- printf("%s", indent(12));
- printf("<symbol name=\"%ws\" ", name);
+ printf("%S", indent(12).c_str());
+ printf("<symbol name=\"%ws\" ", name.c_str());
printf("address=\"0x%x\" ", getRVA(pSymbol));
printf("length=\"0x%I64x\" ", getLength(pSymbol));
printf("tag=\"%ws\" ", getTagAsString(pSymbol));
- printf("kind=\"%ws\" ", getKindAsString(pSymbol));
+ printf("kind=\"%ws\" ", getKindAsString(pSymbol).c_str());
printf("index=\"0x%x\" ", getIndex(pSymbol));
- printf("undecorated=\"%ws\" ", getUndecoratedName(pSymbol));
- printf("value=\"%ws\" ", getValue(pSymbol));
- printf("datatype=\"%ws\" ", getTypeAsString(pSymbol));
+ printf("undecorated=\"%ws\" ", getUndecoratedName(pSymbol).c_str());
+ printf("value=\"%ws\" ", getValue(pSymbol).c_str());
+ printf("datatype=\"%ws\" ", getTypeAsString(pSymbol).c_str());
printf(" />\n");
pSymbol = NULL;
@@ -410,7 +412,7 @@ void iterateSourceFiles(IDiaEnumSourceFiles * pSourceFiles) {
DWORD id;
pSourceFile->get_uniqueId( &id );
if ( name != NULL ) {
- printf("%s<source_file name=\"%ws\" id=\"0x%x\" /> \n", indent(12), name, id);
+ printf("%S<source_file name=\"%ws\" id=\"0x%x\" /> \n", indent(12).c_str(), name, id);
}
pSourceFile = NULL;
}
@@ -432,7 +434,7 @@ void iterateSegments(IDiaEnumSegments * pSegments) {
DWORD seg;
pSegment->get_addressSection( &seg );
pSegment->get_relativeVirtualAddress( &rva );
- printf("%s<segment number=\"%i\" address=\"0x%x\" /> \n", indent(12), seg, rva);
+ printf("%S<segment number=\"%i\" address=\"0x%x\" /> \n", indent(12).c_str(), seg, rva);
pSegment = NULL;
}
}
@@ -471,14 +473,14 @@ void iterateSections(IDiaEnumSectionContribs * pSecContribs) {
}
}
if (pSym == NULL) {
- printf("%s<section_contrib address=\"0x%x\" /> \n", indent(12), rva);
+ printf("%S<section_contrib address=\"0x%x\" /> \n", indent(12).c_str(), rva);
}
else {
- BSTR name = getName(pSym);
- BSTR tag = getTagAsString(pSym);
+ std::wstring name = getName(pSym);
+ std::wstring tag = getTagAsString(pSym);
- printf("%s<section_contrib address=\"0x%x\" name=\"%ws\" tag=\"%ws\" /> \n",
- indent(12), rva, name, tag);
+ printf("%S<section_contrib address=\"0x%x\" name=\"%ws\" tag=\"%ws\" /> \n",
+ indent(12).c_str(), rva, name.c_str(), tag.c_str());
}
}
}
@@ -511,8 +513,8 @@ void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
ULONGLONG length;
pInjectedSrc->get_length(&length);
- printf("%s<injected_source filename=\"%ws\" objectname=\"%ws\" crc=\"0x%x\" length=\"0x%I64x\" />\n",
- indent(8),
+ printf("%S<injected_source filename=\"%ws\" objectname=\"%ws\" crc=\"0x%x\" length=\"0x%I64x\" />\n",
+ indent(8).c_str(),
filename,
objectname,
crc,
@@ -542,7 +544,7 @@ void iterateFrameData(IDiaEnumFrameData * pEnumFrameData) {
}
int iterateTables(const bool printAll) {
- printf("%s<tables>\n", indent(4));
+ printf("%S<tables>\n", indent(4).c_str());
HRESULT hr;
DWORD celt;
@@ -565,7 +567,7 @@ int iterateTables(const bool printAll) {
BSTR name;
pTable->get_name( &name );
- printf("%s<table name=\"%ws\">\n", indent(8), name );
+ printf("%S<table name=\"%ws\">\n", indent(8).c_str(), name );
IDiaEnumSymbols * pSymbols;
IDiaEnumSourceFiles * pSourceFiles;
@@ -595,10 +597,10 @@ int iterateTables(const bool printAll) {
iterateFrameData(pEnumFrameData);
}
- printf("%s</table>\n", indent(8));
+ printf("%S</table>\n", indent(8).c_str());
pTable = NULL;
}
- printf("%s</tables>\n", indent(4));
+ printf("%S</tables>\n", indent(4).c_str());
return 0;
}
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp b/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp
index cebd278916..90384dc40c 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp
@@ -99,7 +99,7 @@ int init(const char * szFilename, const char * szSignature, const char * szAge)
fatal("Unable to get global symbol index\n");
}
- BSTR exename = getName(pGlobal);
+ std::wstring exename = getName(pGlobal);
GUID currGUID;
BSTR guidString;
@@ -128,7 +128,7 @@ int init(const char * szFilename, const char * szSignature, const char * szAge)
fatal("Unable to get GUID\n");
}
- printf("<pdb file=\"%s\" exe=\"%ws\" guid=\"%ws\" age=\"%ld\">\n", szFilename, exename, guidString, currAge);
+ printf("<pdb file=\"%s\" exe=\"%ws\" guid=\"%ws\" age=\"%ld\">\n", szFilename, exename.c_str(), guidString, currAge);
return hr;
}
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
index cf3af867d0..f27a2e9f1e 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
@@ -20,7 +20,7 @@
// Returns a mixture of static strings and allocated strings.
// Abandon all hope of memory management, ye who enter here
-wchar_t * printVariant( VARIANT & v ) {
+std::wstring printVariant( VARIANT & v ) {
// handle results that don't need a format buffer
switch( v.vt ) {
@@ -34,11 +34,8 @@ wchar_t * printVariant( VARIANT & v ) {
return L"null";
}
- size_t blen = 100;
- wchar_t * variant = (wchar_t *)calloc(blen, sizeof(wchar_t));
- if (variant == NULL) {
- return L"error";
- }
+ const int blen = 100;
+ wchar_t variant[blen] = {};
switch( v.vt ) {
case VT_ARRAY://Indicates a SAFEARRAY pointer.
swprintf_s(variant, blen, L"%I64d", (ULONGLONG) v.parray);//TODO
@@ -124,12 +121,11 @@ void printBound( IDiaSymbol* pBound ) {
}
}
-// Returns mixture of allocated and static strings
-BSTR printType( IDiaSymbol * pType, BSTR suffix ) {
+std::wstring printType( IDiaSymbol * pType, BSTR suffix ) {
if (pType == NULL) {
return L"";
}
- BSTR name = getName(pType);
+ std::wstring name = getName(pType);
DWORD tag = getTag(pType);
if ( tag == SymTagPointerType ) {
@@ -139,7 +135,9 @@ BSTR printType( IDiaSymbol * pType, BSTR suffix ) {
wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
if (str != NULL) {
swprintf_s(str, length, L"%ws *", suffix);
- return (BSTR)printType(pBaseType, (BSTR)str);
+ std::wstring ret = printType(pBaseType, (BSTR)str);
+ free(str);
+ return ret;
}
}
else {
@@ -148,12 +146,14 @@ BSTR printType( IDiaSymbol * pType, BSTR suffix ) {
}
if ( tag == SymTagBaseType ) {
- BSTR bt = getBaseTypeAsString( pType );
- size_t length = wcslen(bt) + wcslen(suffix) + 1; // length of: bt + suffix + "\0"
+ std::wstring bt = getBaseTypeAsString( pType );
+ size_t length = bt.length() + wcslen(suffix) + 1; // length of: bt + suffix + "\0"
wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
if (str != NULL) {
- swprintf_s(str, length, L"%ws%ws", bt, suffix);
- return str;
+ swprintf_s(str, length, L"%ws%ws", bt.c_str(), suffix);
+ std::wstring ret(str);
+ free(str);
+ return ret;
}
}
@@ -171,7 +171,9 @@ BSTR printType( IDiaSymbol * pType, BSTR suffix ) {
wchar_t * str = (wchar_t *)calloc(strLen, sizeof(wchar_t));
if (str != NULL) {
swprintf_s(str, strLen, L"%ws[%I64d]", suffix, lenArray / lenElem);
- return printType(pBaseType, (BSTR)str);
+ std::wstring ret = printType(pBaseType, (BSTR)str);
+ free(str);
+ return ret;
}
}
@@ -188,7 +190,9 @@ BSTR printType( IDiaSymbol * pType, BSTR suffix ) {
wchar_t* guidStr = (wchar_t*)calloc((size_t)maxGUIDStrLen, sizeof(wchar_t));
if (guidStr != NULL) {
if (StringFromGUID2(guid, guidStr, maxGUIDStrLen) > 0) {
- return (BSTR)guidStr;
+ std::wstring ret(guidStr);
+ free(guidStr);
+ return ret;
}
}
}
@@ -197,18 +201,22 @@ BSTR printType( IDiaSymbol * pType, BSTR suffix ) {
wchar_t * str = (wchar_t *)calloc(strLen, sizeof(wchar_t));
if (str != NULL) {
swprintf_s(str, strLen, L"0x%x:0x%x", id, rec);
- return (BSTR)str;
+ std::wstring ret(str);
+ free(str);
+ return ret;
}
}
return L"";
}
- if ( name != NULL ) {
- size_t length = wcslen(name) + wcslen(suffix) + 1; // length of name + suffix + "\0"
+ if ( !name.empty() ) {
+ size_t length = name.length() + wcslen(suffix) + 1; // length of name + suffix + "\0"
wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
if (str != NULL) {
- swprintf_s(str, length, L"%ws%ws", name, suffix);
- return (BSTR)str;
+ swprintf_s(str, length, L"%ws%ws", name.c_str(), suffix);
+ std::wstring ret(str);
+ free(str);
+ return ret;
}
}
@@ -216,7 +224,7 @@ BSTR printType( IDiaSymbol * pType, BSTR suffix ) {
}
void printScopeName( IDiaSymbol* pscope ) {
- printf("<scope name=\"%ws\" tag=\"%ws\" />\n", getName( pscope ), getTagAsString( pscope ));
+ printf("<scope name=\"%ws\" tag=\"%ws\" />\n", getName( pscope ).c_str(), getTagAsString( pscope ));
}
void printNameFromScope( wchar_t* name, IDiaSymbol* pscope, IDiaEnumSymbols* pEnum ) {
@@ -224,9 +232,9 @@ void printNameFromScope( wchar_t* name, IDiaSymbol* pscope, IDiaEnumSymbols* pEn
IDiaSymbol * pSym;
DWORD celt;
while ( SUCCEEDED( pEnum->Next( 1, &pSym, &celt ) ) && celt == 1 ) {
- BSTR name = getName( pSym );
- BSTR tag = getTagAsString( pSym );
- wprintf( L"\t%ws %ws found in ", tag, name );
+ std::wstring name = getName( pSym );
+ std::wstring tag = getTagAsString( pSym );
+ wprintf( L"\t%ws %ws found in ", tag.c_str(), name.c_str() );
printScopeName( pscope );
wprintf( L"\n" );
pSym = 0;
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp b/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
index 9a0ef6f3b7..ef66f26007 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
@@ -169,8 +169,9 @@ static wchar_t* BASIC_TYPE_STRINGS [] = {
L"char32_t"
};
-BSTR getName(IDiaSymbol * pSymbol) {
- BSTR name, escapedName = NULL;
+std::wstring getName(IDiaSymbol * pSymbol) {
+ BSTR name;
+ std::wstring escapedName;
DWORD symIndexId, locType;
ULONGLONG len;
if (pSymbol->get_name( &name ) == 0) {
@@ -180,6 +181,7 @@ BSTR getName(IDiaSymbol * pSymbol) {
if (str != NULL) {
swprintf_s(str, length, L"NONAME");
escapedName = escapeXmlEntities(str);
+ free(str);
}
} else
if(wcsstr(name, L"unnamed-tag") != NULL){
@@ -191,6 +193,7 @@ BSTR getName(IDiaSymbol * pSymbol) {
if (str != NULL) {
swprintf_s(str, length, L"<unnamed_%04x>", symIndexId);
escapedName = escapeXmlEntities(str);
+ free(str);
}
} else
if(pSymbol->get_locationType(&locType) == 0 &&
@@ -201,6 +204,7 @@ BSTR getName(IDiaSymbol * pSymbol) {
if (str != NULL) {
swprintf_s(str, length, L"%ws:0x%I64x", name, len);
escapedName = escapeXmlEntities(str);
+ free(str);
}
} else {
escapedName = escapeXmlEntities(name);
@@ -208,13 +212,13 @@ BSTR getName(IDiaSymbol * pSymbol) {
SysFreeString(name);
return escapedName;
}
- return NULL;
+ return std::wstring();
}
-BSTR getUndecoratedName(IDiaSymbol * pSymbol) {
+std::wstring getUndecoratedName(IDiaSymbol * pSymbol) {
BSTR name;
if (pSymbol->get_undecoratedName( &name ) == 0) {
- BSTR escapedName = escapeXmlEntities(name);
- SysFreeString(name);
+ std::wstring escapedName = escapeXmlEntities(name);
+ SysFreeString(name);
return escapedName;
}
return L"";
@@ -234,7 +238,7 @@ DWORD getTag(IDiaSymbol * pSymbol) {
pSymbol->get_symTag( &tag );
return tag;
}
-BSTR getTagAsString(IDiaSymbol * pSymbol) {
+const wchar_t* getTagAsString(IDiaSymbol * pSymbol) {
return SYMBOL_TAG_STRINGS[getTag(pSymbol)];
}
DWORD getKind(IDiaSymbol * pSymbol) {
@@ -247,7 +251,7 @@ DWORD getUdtKind(IDiaSymbol * pSymbol) {
pSymbol->get_udtKind( &kind );
return kind;
}
-BSTR getKindAsString(IDiaSymbol * pSymbol) {
+std::wstring getKindAsString(IDiaSymbol * pSymbol) {
DWORD tag = getTag(pSymbol);
if (tag == SymTagUDT) {
return UDT_KIND_STRINGS[getUdtKind(pSymbol)];
@@ -272,7 +276,7 @@ DWORD getIndex(IDiaSymbol * pSymbol) {
return index;
}
-wchar_t * getValue(IDiaSymbol * pSymbol) {
+std::wstring getValue(IDiaSymbol * pSymbol) {
if (getKind(pSymbol) == DataIsConstant) {
VARIANT value;
HRESULT hr = pSymbol->get_value( &value );
@@ -308,8 +312,8 @@ IDiaSymbol * getType(IDiaSymbol * pSymbol) {
}
return NULL;
}
-BSTR getTypeAsString(IDiaSymbol * pSymbol) {
- BSTR typeStr ;
+std::wstring getTypeAsString(IDiaSymbol * pSymbol) {
+ std::wstring typeStr ;
IDiaSymbol * pType;
if (pSymbol->get_type( &pType ) == 0) {
typeStr = printType( pType, L"" );
@@ -328,7 +332,7 @@ DWORD getBaseType(IDiaSymbol * pSymbol) {
}
return -1;
}
-BSTR getBaseTypeAsString(IDiaSymbol * pSymbol) {
+std::wstring getBaseTypeAsString(IDiaSymbol * pSymbol) {
ULONGLONG len = getLength(pSymbol);
DWORD bt = getBaseType(pSymbol);
switch(bt) {
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
index 9a31a7aadf..ce4220bf22 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
@@ -14,104 +14,66 @@
* limitations under the License.
*/
#include "xml.h"
+#include <string>
-char* indent(size_t nSpaces) {
- switch (nSpaces) {
- case 1: return " ";
- case 2: return " ";
- case 4: return " ";
- case 6: return " ";
- case 8: return " ";
- case 10: return " ";
- case 12: return " ";
- }
-
- // NOTE: memory leak if following code is hit, but luckily there are no callers
- // that use indent() that would trigger this.
- // Probably would be better to throw an error if a non-standard nSpaces value is used.
- if (nSpaces < 0) {
- nSpaces = 0;
- }
- char* indent = (char*)calloc(nSpaces + 1, sizeof(char));
- if (indent != NULL) {
- for (int i = 0; i < nSpaces; ++i) {
- indent[i] = ' ';
- }
- }
- return indent;
+std::wstring indent(size_t nSpaces) {
+ return std::wstring(nSpaces, ' ');
}
-BSTR escapeXmlEntities(BSTR bstr) {
- WCHAR * str = (WCHAR *)bstr;
- size_t len = wcslen(str);
- if (len == 0) return str;
- size_t destLen = 0;
+std::wstring escapeXmlEntities(BSTR bstr) {
// 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.
- 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;
- }
- }
- destLen += 1;
+ const size_t len = SysStringLen(bstr);
+ size_t destLen = 0;
+ for (int i = 0; i < len; ++i) {
+ switch (bstr[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;
+ }
+ }
- WCHAR * newstr = (WCHAR *)calloc(destLen, sizeof(WCHAR));
- if (newstr == NULL) {
- return newstr;
- }
- WCHAR * tmp = newstr;
-
+ std::wstring escaped(destLen, '\0');
for (int i = 0 ; i < len ; ++i) {
- switch (str[i]) {
+ switch (bstr[i]) {
case '&' :
- wcscpy_s(tmp, destLen, L"&amp;");
- tmp += 5;
+ escaped += L"&amp;";
break;
case '<' :
- wcscpy_s(tmp, destLen, L"&lt;");
- tmp += 4;
+ escaped += L"&lt;";
break;
case '>' :
- wcscpy_s(tmp, destLen, L"&gt;");
- tmp += 4;
+ escaped += L"&gt;";
break;
case '\'' :
- wcscpy_s(tmp, destLen, L"&apos;");
- tmp += 6;
+ escaped += L"&apos;";
break;
case '"' :
- wcscpy_s(tmp, destLen, L"&quot;");
- tmp += 6;
+ escaped += L"&quot;";
break;
case 0x7F :
break;
+
default :
- *tmp = str[i];
- ++tmp;
+ escaped += bstr[i];
break;
}
}
-
- // add null term at end of string. Not strictly necessary since we are using calloc
- *tmp = 0;
-
- return newstr;
+ return escaped;
}
diff --git a/Ghidra/Features/PDB/src/pdb/headers/find.h b/Ghidra/Features/PDB/src/pdb/headers/find.h
index d24a38869f..6300d40c93 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/find.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/find.h
@@ -30,7 +30,7 @@
//This method locates the mangled
//function name, which is stored in a SymTagPublicSymbol symbol.
//
-BSTR findMangledName(IDiaSymbol * pFunction);
+std::wstring findMangledName(IDiaSymbol * pFunction);
void findNameInNamespace( wchar_t* name, IDiaSymbol* pnamespace );
void findNameInEnum( wchar_t* name, IDiaSymbol* penumeration );
void findNameInClass( wchar_t* name, IDiaSymbol* pclass );
diff --git a/Ghidra/Features/PDB/src/pdb/headers/print.h b/Ghidra/Features/PDB/src/pdb/headers/print.h
index 34930a77f8..30fc3a18c6 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/print.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/print.h
@@ -22,9 +22,10 @@
#include "dia2.h"
#include "diacreate.h"
#include "cvconst.h"
+#include <string>
-wchar_t * printVariant( VARIANT & v );
-BSTR printType( IDiaSymbol* pType, BSTR prefix );
+std::wstring printVariant( VARIANT & v );
+std::wstring printType( IDiaSymbol* pType, BSTR prefix );
void printBound( IDiaSymbol* pBound );
void printScopeName( IDiaSymbol* pscope );
diff --git a/Ghidra/Features/PDB/src/pdb/headers/symbol.h b/Ghidra/Features/PDB/src/pdb/headers/symbol.h
index 9fa97cb405..1d81ddad92 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/symbol.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/symbol.h
@@ -20,23 +20,24 @@
#include "dia2.h"
#include "diacreate.h"
#include "cvconst.h"
+#include <string>
-BSTR getName(IDiaSymbol * pSymbol);
-BSTR getUndecoratedName(IDiaSymbol * pSymbol);
-DWORD getRVA(IDiaSymbol * pSymbol);
-ULONGLONG getLength(IDiaSymbol * pSymbol);
-DWORD getTag(IDiaSymbol * pSymbol);
-BSTR getTagAsString(IDiaSymbol * pSymbol);
-DWORD getKind(IDiaSymbol * pSymbol);
-BSTR getKindAsString(IDiaSymbol * pSymbol);
-DWORD getUdtKind(IDiaSymbol * pSymbol);
-LONG getOffset(IDiaSymbol * pSymbol);
-DWORD getIndex(IDiaSymbol * pSymbol);
-wchar_t * getValue(IDiaSymbol * pSymbol);
-DWORD getBaseType(IDiaSymbol * pSymbol);
-BSTR getBaseTypeAsString(IDiaSymbol * pSymbol);
-IDiaSymbol * getType(IDiaSymbol * pSymbol);
-BSTR getTypeAsString(IDiaSymbol * pSymbol);
+std::wstring getName(IDiaSymbol * pSymbol);
+std::wstring getUndecoratedName(IDiaSymbol * pSymbol);
+DWORD getRVA(IDiaSymbol * pSymbol);
+ULONGLONG getLength(IDiaSymbol * pSymbol);
+DWORD getTag(IDiaSymbol * pSymbol);
+const wchar_t* getTagAsString(IDiaSymbol * pSymbol);
+DWORD getKind(IDiaSymbol * pSymbol);
+std::wstring getKindAsString(IDiaSymbol * pSymbol);
+DWORD getUdtKind(IDiaSymbol * pSymbol);
+LONG getOffset(IDiaSymbol * pSymbol);
+DWORD getIndex(IDiaSymbol * pSymbol);
+std::wstring getValue(IDiaSymbol * pSymbol);
+DWORD getBaseType(IDiaSymbol * pSymbol);
+std::wstring getBaseTypeAsString(IDiaSymbol * pSymbol);
+IDiaSymbol * getType(IDiaSymbol * pSymbol);
+std::wstring getTypeAsString(IDiaSymbol * pSymbol);
bool isScopeSym( DWORD tag );
diff --git a/Ghidra/Features/PDB/src/pdb/headers/xml.h b/Ghidra/Features/PDB/src/pdb/headers/xml.h
index 8ef7957e4f..4313d4397d 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/xml.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/xml.h
@@ -17,9 +17,10 @@
#define __PDB__XML__H__
#include <stdio.h>
+#include <string>
#include <comutil.h>
-char * indent(size_t nSpaces);
-BSTR escapeXmlEntities(BSTR bstr);
+std::wstring indent(size_t nSpaces);
+std::wstring escapeXmlEntities(BSTR bstr);
#endif