Skip to content

Commit e9f1bc5

Browse files
committed
added IsBadReadPtr check in COM declarations collector
1 parent 8d7cc3d commit e9f1bc5

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Rubberduck.Parsing/Symbols/ReferencedDeclarationsCollector.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Runtime.InteropServices;
45
using System.Runtime.InteropServices.ComTypes;
56
using Microsoft.Vbe.Interop;
@@ -77,6 +78,9 @@ private enum REGKIND
7778
{VarEnum.VT_R8, "Double"},
7879
};
7980

81+
[DllImport("kernel32.dll")]
82+
public static extern bool IsBadReadPtr(IntPtr lp, uint ucb);
83+
8084
private string GetTypeName(TYPEDESC desc, ITypeInfo info)
8185
{
8286
var vt = (VarEnum)desc.vt;
@@ -85,24 +89,29 @@ private string GetTypeName(TYPEDESC desc, ITypeInfo info)
8589
switch (vt)
8690
{
8791
case VarEnum.VT_PTR:
92+
if (IsBadReadPtr(desc.lpValue, (uint) IntPtr.Size))
93+
{
94+
Debug.WriteLine("Bad read pointer; returning fallback 'Object' type name.");
95+
return "Object";
96+
}
8897
tdesc = (TYPEDESC)Marshal.PtrToStructure(desc.lpValue, typeof(TYPEDESC));
8998
return GetTypeName(tdesc, info);
9099
case VarEnum.VT_USERDEFINED:
100+
int href;
91101
unchecked
92102
{
93-
int href;
94-
if (Marshal.SizeOf(typeof (IntPtr)) == sizeof (long))
95-
{
96-
href = (int) desc.lpValue.ToInt64();
97-
}
98-
else
99-
{
100-
href = desc.lpValue.ToInt32();
101-
}
103+
href = (int)(desc.lpValue.ToInt64() & 0xFFFFFFFF);
104+
}
105+
try
106+
{
102107
ITypeInfo refTypeInfo;
103108
info.GetRefTypeInfo(href, out refTypeInfo);
104109
return GetTypeName(refTypeInfo);
105110
}
111+
catch (Exception)
112+
{
113+
return "Object";
114+
}
106115
case VarEnum.VT_CARRAY:
107116
tdesc = (TYPEDESC)Marshal.PtrToStructure(desc.lpValue, typeof(TYPEDESC));
108117
return GetTypeName(tdesc, info) + "()";
@@ -114,7 +123,7 @@ private string GetTypeName(TYPEDESC desc, ITypeInfo info)
114123
}
115124
break;
116125
}
117-
return "UNKNOWN";
126+
return "Object";
118127
}
119128

120129
private string GetTypeName(ITypeInfo info)

0 commit comments

Comments
 (0)