Skip to content

Commit

Permalink
Merge pull request #1 from DavidHess-ITS/master
Browse files Browse the repository at this point in the history
handle case where IL has a string set to an sbyte value.
  • Loading branch information
KirillOsenkov authored Apr 16, 2024
2 parents ebbba77 + 641ce69 commit f1e34ee
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Mono.Cecil/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,10 @@ static ElementType GetConstantType (TypeReference constant_type, object constant

return ElementType.Class;
case ElementType.String:
return ElementType.String;
if (constant is string)
return ElementType.String;
else
return GetConstantType (constant.GetType ());
case ElementType.Object:
return GetConstantType (constant.GetType ());
case ElementType.Array:
Expand Down Expand Up @@ -2201,7 +2204,7 @@ SignatureWriter GetConstantSignature (ElementType type, object value)
signature.WriteInt32 (0);
break;
case ElementType.String:
signature.WriteConstantString ((string) value);
signature.WriteConstantString (value);
break;
default:
signature.WriteConstantPrimitive (value);
Expand Down Expand Up @@ -2904,10 +2907,13 @@ bool TryWriteElementType (TypeReference type)
return true;
}

public void WriteConstantString (string value)
public void WriteConstantString (object value)
{
if (value != null)
WriteBytes (Encoding.Unicode.GetBytes (value));
if (value is string v)
WriteBytes (Encoding.Unicode.GetBytes (v));
else
WritePrimitiveValue (value);
else
WriteByte (0xff);
}
Expand Down
6 changes: 6 additions & 0 deletions Test/Mono.Cecil.Tests/FieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ public void ConstantCoalescing ()
Assert.IsTrue (field.HasConstant);
Assert.IsInstanceOf (typeof (char), field.Constant);
Assert.AreEqual ('s', field.Constant);
field = fields.GetField ("string_sbyte");
Assert.AreEqual ("System.String", field.FieldType.FullName);
Assert.IsTrue (field.HasConstant);
Assert.IsInstanceOf (typeof (sbyte), field.Constant);
Assert.AreEqual ((sbyte)0x20, field.Constant);
});
}

Expand Down
1 change: 1 addition & 0 deletions Test/Resources/il/types.il
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
.field private static literal char char_int16 = int16(0x0001)
.field private static literal int16 int16_char = char(0x0073)
.field private static literal int32 int32_nullref = nullref
.field private static literal string string_sbyte = int8(0x20)
}

0 comments on commit f1e34ee

Please sign in to comment.