Skip to content

Commit b672577

Browse files
committed
Print an error when trying to read too large of an array.
DbgEng only supports reads with a 32bit size. (more than 2^32 elements)
1 parent 1e718ea commit b672577

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

include/RemoteType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace PyExt::Remote {
3535
auto offset() const -> Offset;
3636
auto symbolName() const -> std::string;
3737
// necessary for x86 because offsets in remote address space are only 32 Bit
38-
static auto readOffsetArray(/*const*/ ExtRemoteTyped& remoteArray, unsigned long numElements) -> std::vector<Offset>;
38+
static auto readOffsetArray(/*const*/ ExtRemoteTyped& remoteArray, std::uint64_t numElements) -> std::vector<Offset>;
3939

4040
protected: // Helpers for more derived classes.
4141
/// Access to the instance's memory in the debuggee.

src/ExtHelpers.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <engextcpp.hpp>
44
#include <type_traits>
55
#include <vector>
6+
#include <string>
7+
#include <limits>
68
using namespace std::literals::string_literals;
79

810
namespace utils {
@@ -31,16 +33,22 @@ namespace utils {
3133

3234
// Reads an array of elements pointed to by an ExtRemoteData.
3335
template <typename ElemType>
34-
auto readArray(/*const*/ ExtRemoteTyped& remoteArray, unsigned long numElements) -> std::vector<ElemType>
36+
auto readArray(/*const*/ ExtRemoteTyped& remoteArray, std::uint64_t numElements) -> std::vector<ElemType>
3537
{
3638
auto remoteData = remoteArray.Dereference();
3739
const auto remoteSize = remoteData.GetTypeSize();
3840
if (remoteSize != sizeof(ElemType)) {
3941
g_Ext->ThrowRemote(E_INVALIDARG, "sizeof(ElemType) does not match size type size for ExtRemoteTyped array read.");
4042
}
4143

44+
const auto arrayExtent = numElements * remoteSize;
45+
// ExtRemoteData only supports arrays with 32bit extents, so we have to narrow it.
46+
if (numElements > std::numeric_limits<std::uint32_t>::max()) {
47+
g_Ext->ThrowRemote(E_BOUNDS, "Could not read array. Size too large.");
48+
}
49+
4250
std::vector<ElemType> buffer(numElements);
43-
remoteData.ReadBuffer(buffer.data(), numElements*remoteSize);
51+
remoteData.ReadBuffer(buffer.data(), static_cast<std::uint32_t>(arrayExtent));
4452
return buffer;
4553
}
4654

src/PyExt.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
6464
<ForcedIncludeFiles>pch.h</ForcedIncludeFiles>
6565
<LanguageStandard>stdcpp17</LanguageStandard>
66-
<PreprocessorDefinitions>PYEXT_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
66+
<PreprocessorDefinitions>PYEXT_DLL;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
6767
</ClCompile>
6868
<Link>
6969
<SubSystem>Windows</SubSystem>

src/RemoteType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace PyExt::Remote {
5151
}
5252

5353

54-
auto RemoteType::readOffsetArray(/*const*/ ExtRemoteTyped& remoteArray, unsigned long numElements) -> vector<Offset>
54+
auto RemoteType::readOffsetArray(/*const*/ ExtRemoteTyped& remoteArray, std::uint64_t numElements) -> vector<Offset>
5555
{
5656
auto ptrSize = utils::getPointerSize();
5757
switch (ptrSize) {
@@ -63,7 +63,7 @@ namespace PyExt::Remote {
6363
}
6464
case 8:
6565
// x64 - 64 Bit Python
66-
return utils::readArray<Offset>(remoteArray, numElements);
66+
return utils::readArray<std::uint64_t>(remoteArray, numElements);
6767
}
6868

6969
g_Ext->ThrowInterrupt();

0 commit comments

Comments
 (0)