Skip to content

Commit

Permalink
WebcilReader: copy updated struct for BlobReader.ReadUtf8NullTerminated
Browse files Browse the repository at this point in the history
When using reflection to call a method on BlobReader, make sure to
copy the struct after the call since the internal state is modified

Fixes PdbChecksum extraction from Webcil files

(This was a problem even before webcil-in-wasm)
  • Loading branch information
lambdageek committed May 16, 2023
1 parent 2585a04 commit 9d563a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ internal static class Reflection
return mi;
});

internal static string? ReadUtf8NullTerminated(BlobReader reader) => (string?)s_readUtf8NullTerminated.Value.Invoke(reader, null);
internal static string? ReadUtf8NullTerminated(ref BlobReader reader)
{
object boxedReader = reader;
string? result = (string?)s_readUtf8NullTerminated.Value.Invoke(boxedReader, null);
reader = (BlobReader) boxedReader; // the call modifies the struct state, make sure to copy it back.
return result;
}

private static readonly Lazy<ConstructorInfo> s_codeViewDebugDirectoryDataCtor = new Lazy<ConstructorInfo>(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ private static CodeViewDebugDirectoryData DecodeCodeViewDebugDirectoryData(BlobR

Guid guid = reader.ReadGuid();
int age = reader.ReadInt32();
string path = ReadUtf8NullTerminated(reader)!;
string path = ReadUtf8NullTerminated(ref reader)!;

return MakeCodeViewDebugDirectoryData(guid, age, path);
}

private static string? ReadUtf8NullTerminated(BlobReader reader) => Reflection.ReadUtf8NullTerminated(reader);
private static string? ReadUtf8NullTerminated(ref BlobReader reader) => Reflection.ReadUtf8NullTerminated(ref reader);

private static CodeViewDebugDirectoryData MakeCodeViewDebugDirectoryData(Guid guid, int age, string path) => Reflection.MakeCodeViewDebugDirectoryData(guid, age, path);

Expand Down Expand Up @@ -316,7 +316,7 @@ public PdbChecksumDebugDirectoryData ReadPdbChecksumDebugDirectoryData(DebugDire

private static PdbChecksumDebugDirectoryData DecodePdbChecksumDebugDirectoryData(BlobReader reader)
{
var algorithmName = ReadUtf8NullTerminated(reader);
var algorithmName = ReadUtf8NullTerminated(ref reader);
byte[]? checksum = reader.ReadBytes(reader.RemainingBytes);
if (string.IsNullOrEmpty(algorithmName) || checksum == null || checksum.Length == 0)
{
Expand Down

0 comments on commit 9d563a9

Please sign in to comment.