Skip to content

Commit e9ff6e8

Browse files
authored
Merge pull request github#6578 from tamasvajk/fix/cil-local-decoding
C#: Handle non-critical exception in CIL local variable extraction
2 parents 29bcd7c + d6ae19c commit e9ff6e8

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

csharp/extractor/Semmle.Extraction.CIL/Entities/DefinitionMethod.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,39 @@ public override IEnumerable<IExtractionProduct> Contents
114114

115115
if (!body.LocalSignature.IsNil)
116116
{
117-
var locals = Context.MdReader.GetStandaloneSignature(body.LocalSignature);
118-
var localVariableTypes = locals.DecodeLocalSignature(Context.TypeSignatureDecoder, this);
119-
120-
this.locals = new LocalVariable[localVariableTypes.Length];
117+
var localVariableTypes = System.Collections.Immutable.ImmutableArray<Type>.Empty;
118+
var hasError = false;
119+
try
120+
{
121+
var locals = Context.MdReader.GetStandaloneSignature(body.LocalSignature);
122+
localVariableTypes = locals.DecodeLocalSignature(Context.TypeSignatureDecoder, this);
123+
}
124+
catch (System.BadImageFormatException exc)
125+
{
126+
Context.Extractor.Logger.Log(Util.Logging.Severity.Info,
127+
$"Could not decode locals in method {declaringType.GetQualifiedName()}.{name}. {exc}");
128+
hasError = true;
129+
}
121130

122-
for (var l = 0; l < this.locals.Length; ++l)
131+
if (!hasError)
123132
{
124-
var t = localVariableTypes[l];
125-
if (t is ByRefType brt)
126-
{
127-
t = brt.ElementType;
128-
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
129-
yield return this.locals[l];
130-
yield return Tuples.cil_type_annotation(this.locals[l], TypeAnnotation.Ref);
131-
}
132-
else
133+
this.locals = new LocalVariable[localVariableTypes.Length];
134+
135+
for (var l = 0; l < this.locals.Length; ++l)
133136
{
134-
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
135-
yield return this.locals[l];
137+
var t = localVariableTypes[l];
138+
if (t is ByRefType brt)
139+
{
140+
t = brt.ElementType;
141+
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
142+
yield return this.locals[l];
143+
yield return Tuples.cil_type_annotation(this.locals[l], TypeAnnotation.Ref);
144+
}
145+
else
146+
{
147+
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
148+
yield return this.locals[l];
149+
}
136150
}
137151
}
138152
}

0 commit comments

Comments
 (0)