Browse Source

Don't crash when trying to decode invalid attribute/secdecl blobs.

pull/18/head
Daniel Grunwald 14 years ago
parent
commit
e4c4a81eb7
  1. 56
      src/Libraries/NewNRefactory/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

56
src/Libraries/NewNRefactory/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

@ -806,7 +806,8 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -806,7 +806,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
sealed class CecilResolvedAttribute : IAttribute
{
readonly ITypeResolveContext context;
readonly CecilUnresolvedAttribute unresolved;
readonly byte[] blob;
readonly IList<ITypeReference> ctorParameterTypes;
readonly IType attributeType;
IMethod constructor;
@ -818,10 +819,18 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -818,10 +819,18 @@ namespace ICSharpCode.NRefactory.TypeSystem
public CecilResolvedAttribute(ITypeResolveContext context, CecilUnresolvedAttribute unresolved)
{
this.context = context;
this.unresolved = unresolved;
this.blob = unresolved.blob;
this.ctorParameterTypes = unresolved.ctorParameterTypes;
this.attributeType = unresolved.attributeType.Resolve(context);
}
public CecilResolvedAttribute(ITypeResolveContext context, IType attributeType)
{
this.context = context;
this.attributeType = attributeType;
this.ctorParameterTypes = EmptyList<ITypeReference>.Instance;
}
public DomRegion Region {
get { return DomRegion.Empty; }
}
@ -842,7 +851,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -842,7 +851,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
IMethod ResolveConstructor()
{
var parameterTypes = unresolved.ctorParameterTypes.Resolve(context);
var parameterTypes = ctorParameterTypes.Resolve(context);
foreach (var ctor in attributeType.GetConstructors(m => m.Parameters.Count == parameterTypes.Count)) {
bool ok = true;
for (int i = 0; i < parameterTypes.Count; i++) {
@ -901,12 +910,14 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -901,12 +910,14 @@ namespace ICSharpCode.NRefactory.TypeSystem
void DecodeBlob(List<ResolveResult> positionalArguments, List<KeyValuePair<IMember, ResolveResult>> namedArguments)
{
BlobReader reader = new BlobReader(unresolved.blob, context.CurrentAssembly);
if (blob == null)
return;
BlobReader reader = new BlobReader(blob, context.CurrentAssembly);
if (reader.ReadUInt16() != 0x0001) {
Debug.WriteLine("Unknown blob prolog");
return;
}
foreach (var ctorParameter in unresolved.ctorParameterTypes.Resolve(context)) {
foreach (var ctorParameter in ctorParameterTypes.Resolve(context)) {
positionalArguments.Add(reader.ReadFixedArg(ctorParameter));
}
ushort numNamed = reader.ReadUInt16();
@ -1151,7 +1162,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -1151,7 +1162,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
case KnownTypeCode.String:
return ReadSerString();
default:
return ErrorResolveResult.UnknownError;
throw new NotSupportedException();
}
}
@ -1318,14 +1329,30 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -1318,14 +1329,30 @@ namespace ICSharpCode.NRefactory.TypeSystem
ITypeResolveContext context = new SimpleTypeResolveContext(currentAssembly);
BlobReader reader = new BlobReader(blob, currentAssembly);
IList<IAttribute> result = new List<IAttribute>();
if (reader.ReadByte() != '.') {
// should not use UnresolvedSecurityDeclaration for XML secdecls
throw new InvalidOperationException();
}
ResolveResult securityActionRR = securityAction.Resolve(context);
uint attributeCount = reader.ReadCompressedUInt32();
for (uint i = 0; i < attributeCount; i++) {
IAttribute[] attributes = new IAttribute[attributeCount];
try {
ReadSecurityBlob(reader, attributes, context, securityActionRR);
} catch (NotSupportedException ex) {
// ignore invalid blobs
Debug.WriteLine(ex.ToString());
}
for (int i = 0; i < attributes.Length; i++) {
if (attributes[i] == null)
attributes[i] = new CecilResolvedAttribute(context, SpecialType.UnknownType);
}
return attributes;
// return (IList<IAttribute>)cache.GetOrAddShared(this, attributes);
}
void ReadSecurityBlob(BlobReader reader, IAttribute[] attributes, ITypeResolveContext context, ResolveResult securityActionRR)
{
for (int i = 0; i < attributes.Length; i++) {
string attributeTypeName = reader.ReadSerString();
ITypeReference attributeTypeRef = ReflectionHelper.ParseReflectionName(attributeTypeName);
IType attributeType = attributeTypeRef.Resolve(context);
@ -1341,15 +1368,12 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -1341,15 +1368,12 @@ namespace ICSharpCode.NRefactory.TypeSystem
namedArgs.Add(namedArg);
}
result.Add(new ResolvedSecurityAttribute {
AttributeType = attributeType,
NamedArguments = namedArgs,
PositionalArguments = new ResolveResult[] { securityActionRR }
});
attributes[i] = new ResolvedSecurityAttribute {
AttributeType = attributeType,
NamedArguments = namedArgs,
PositionalArguments = new ResolveResult[] { securityActionRR }
};
}
return result;
// return (IList<IAttribute>)cache.GetOrAddShared(this, result);
}
void ISupportsInterning.PrepareForInterning(IInterningProvider provider)

Loading…
Cancel
Save