Skip to content

Commit

Permalink
Convert StringUtils to header-only and remove ProfilerProxy linking I…
Browse files Browse the repository at this point in the history
…nstrumentationEngine.Lib (#419)

* Proxy version update

* Remove StringUtils.cpp

* Move StringUtils to commonheaders
  • Loading branch information
WilliamXieMSFT authored Jul 21, 2021
1 parent 8b02287 commit 9ef85a0
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 76 deletions.
2 changes: 1 addition & 1 deletion build/Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
-->
<ProxyFileVersionMajor>$(SemanticVersionMajor)</ProxyFileVersionMajor>
<ProxyFileVersionMinor>0</ProxyFileVersionMinor>
<ProxyFileVersionPatch>2</ProxyFileVersionPatch>
<ProxyFileVersionPatch>3</ProxyFileVersionPatch>
<ProxyFileVersion>$(ProxyFileVersionMajor).$(ProxyFileVersionMinor).$(ProxyFileVersionPatch)</ProxyFileVersion>

<!--
Expand Down
1 change: 1 addition & 0 deletions src/Common.Headers/Common.Headers.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)InitOnce.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)SafeFindFileHandle.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Singleton.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)StringUtils.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)tstring.h" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,73 @@ class StringUtils
{
public:

// The maximum string length. Set to 10000;
static const size_t MAX_STRING_LEN;
// The maximum string length. Set arbitrarily;
static const size_t MAX_STRING_LEN = 10000;

// Gets the length of the given string using the the standard wcsnlen_s.
// Returns E_BOUNDS if the length of wszString is greater than MAX_STRING_LEN.
// For use with untrusted data which may not have a null terminator.
static HRESULT WStringLen(_In_ const WCHAR* wszString, _Out_ size_t& len);
static HRESULT WStringLen(_In_ const WCHAR* wszString, _Out_ size_t& len)
{
len = wcsnlen_s(wszString, MAX_STRING_LEN);

if (len >= MAX_STRING_LEN)
{
return E_BOUNDS;
}

return S_OK;
}

// Gets the length of the given string using the standard strnlen.
// Returns E_BOUNDS if the length of wszString is greater than MAX_STRING_LEN.
// For use with untrusted data which may not have a null terminator.
static HRESULT StringLen(_In_ const char* szString, _Out_ size_t& len);
static HRESULT StringLen(_In_ const char* szString, _Out_ size_t& len)
{
len = strnlen(szString, MAX_STRING_LEN);

if (len >= MAX_STRING_LEN)
{
return E_BOUNDS;
}

return S_OK;
}

// Gets the length of the given string using the standard wcsnlen_s.
// Yields the same result as calling wcsnlen_s(wszString, MAX_STRING_LEN);
// For use with untrusted data which may not have a null terminator.
static size_t WStringLen(_In_ const WCHAR* wszString);
static size_t WStringLen(_In_ const WCHAR* wszString)
{
return wcsnlen_s(wszString, MAX_STRING_LEN);
}


// Gets the length of the given string using the standard strnlen.
// Yields the same result as calling strnlen(szString, MAX_STRING_LEN);
// For use with untrusted data which may not have a null terminator.
static size_t StringLen(_In_ const char* szString);
static size_t StringLen(_In_ const char* szString)
{
return strnlen(szString, MAX_STRING_LEN);
}

// Modifies pwszPath by appending pwszMore.
// Checks cBounds before appending to prevent buffer overflows.
//
//
// This function wraps PathAppend since supporting Win7 means PathCchAppend is not available.
static HRESULT SafePathAppend(_Inout_updates_z_(cBounds) LPWSTR pwszPath, _In_ LPCWSTR pwszMore, _In_ size_t cBounds);
static HRESULT SafePathAppend(_Inout_updates_z_(cBounds) LPWSTR pwszPath, _In_ LPCWSTR pwszMore, _In_ size_t cBounds)
{
// If PathAppend fails, the destination buffer will be cleared. Check bounds before appending.
if (StringUtils::WStringLen(pwszPath) + StringUtils::WStringLen(pwszMore) >= cBounds)
{
return E_BOUNDS;
}

if (!PathAppend(pwszPath, pwszMore))
{
return E_FAIL;
}

return S_OK;
}
};
1 change: 0 additions & 1 deletion src/InstrumentationEngine.Lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ set(src_files
./InstrumentationMethodSetting.cpp
./LoggerService.cpp
./Logging.cpp
./StringUtils.cpp
./XmlDocWrapper.cpp
./XmlNode.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/InstrumentationEngine.Lib/Encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "stdafx.h"
#include "Encoding.h"
#include "StringUtils.h"
#include "../Common.Headers/StringUtils.h"

using namespace ATL;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
<ClInclude Include="SharedArray.h" />
<ClInclude Include="SignatureValidator.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="StringUtils.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="Util.h" />
<ClInclude Include="XmlDocWrapper.h" />
Expand Down Expand Up @@ -144,7 +143,6 @@
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="StringUtils.cpp" />
<ClCompile Include="XmlDocWrapper.cpp" />
<ClCompile Include="XmlNode.cpp" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/InstrumentationEngine.Lib/InstrumentationMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef PLATFORM_UNIX
#include "SignatureValidator.h"
#endif
#include "StringUtils.h"
#include "../Common.Headers/StringUtils.h"


MicrosoftInstrumentationEngine::CInstrumentationMethod::CInstrumentationMethod(
Expand Down
58 changes: 0 additions & 58 deletions src/InstrumentationEngine.Lib/StringUtils.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/InstrumentationEngine.Lib/XmlDocWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "stdafx.h"
#include "XmlDocWrapper.h"
#include "Encoding.h"
#include "StringUtils.h"
#include "../Common.Headers/StringUtils.h"

using namespace ATL;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Common.Lib.lib;InstrumentationEngine.ProfilerProxy.Lib.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;msxml6.lib;version.lib;mscoree.lib;Faultrep.lib;Shlwapi.lib;InstrumentationEngine.Lib.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Common.Lib.lib;InstrumentationEngine.ProfilerProxy.Lib.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;msxml6.lib;version.lib;mscoree.lib;Faultrep.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalOptions Condition="'$(Platform)'=='Win32'">/SafeSEH %(AdditionalOptions)</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/InstrumentationEngine.ProfilerProxy/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "InstrumentationEngineVersion.h"
#include <Shlwapi.h>
#include "../InstrumentationEngine.ProfilerProxy.Lib/EventLogger.h"
#include "../InstrumentationEngine.Lib/StringUtils.h"
#include "../Common.Headers/StringUtils.h"

typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

Expand Down
2 changes: 1 addition & 1 deletion src/InstrumentationEngine/ProfilerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "RawProfilerHookSettingsReader.h"
#include "../Common.Lib/PathUtils.h"
#endif
#include "StringUtils.h"
#include "../Common.Headers/StringUtils.h"
#include "Encoding.h"
#include <algorithm>

Expand Down

0 comments on commit 9ef85a0

Please sign in to comment.