Skip to content

Commit d052b22

Browse files
Ensure ReadRequest type can be loaded on server. Fixes #26882 (#26931)
1 parent e43041d commit d052b22

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/Components/Web.JS/dist/Release/blazor.server.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.webassembly.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/src/InputFile.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ async function readFileData(elem: InputElement, fileId: number, startOffset: num
112112
function readFileDataSharedMemory(readRequest: any): number {
113113
const inputFileElementReferenceId = monoPlatform.readStringField(readRequest, 0);
114114
const inputFileElement = document.querySelector(`[_bl_${inputFileElementReferenceId}]`);
115-
const fileId = monoPlatform.readInt32Field(readRequest, 4);
116-
const sourceOffset = monoPlatform.readUint64Field(readRequest, 8);
117-
const destination = monoPlatform.readInt32Field(readRequest, 16) as unknown as System_Array<number>;
118-
const destinationOffset = monoPlatform.readInt32Field(readRequest, 20);
119-
const maxBytes = monoPlatform.readInt32Field(readRequest, 24);
115+
const fileId = monoPlatform.readInt32Field(readRequest, 8);
116+
const sourceOffset = monoPlatform.readUint64Field(readRequest, 12);
117+
const destination = monoPlatform.readInt32Field(readRequest, 24) as unknown as System_Array<number>;
118+
const destinationOffset = monoPlatform.readInt32Field(readRequest, 32);
119+
const maxBytes = monoPlatform.readInt32Field(readRequest, 36);
120120

121121
const sourceArrayBuffer = getFileById(inputFileElement as InputElement, fileId).arrayBuffer as ArrayBuffer;
122122
const bytesToRead = Math.min(maxBytes, sourceArrayBuffer.byteLength - sourceOffset);

src/Components/Web/src/Forms/InputFile/ReadRequest.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ namespace Microsoft.AspNetCore.Components.Forms
88
[StructLayout(LayoutKind.Explicit)]
99
internal struct ReadRequest
1010
{
11+
// Even though this type is only intended for use on WebAssembly, make it able to
12+
// load on 64-bit runtimes by allowing 8 bytes for each reference-typed field.
13+
1114
[FieldOffset(0)]
1215
public string InputFileElementReferenceId;
1316

14-
[FieldOffset(4)]
17+
[FieldOffset(8)]
1518
public int FileId;
1619

17-
[FieldOffset(8)]
20+
[FieldOffset(12)]
1821
public long SourceOffset;
1922

20-
[FieldOffset(16)]
23+
[FieldOffset(24)]
2124
public byte[] Destination;
2225

23-
[FieldOffset(20)]
26+
[FieldOffset(32)]
2427
public int DestinationOffset;
2528

26-
[FieldOffset(24)]
29+
[FieldOffset(36)]
2730
public int MaxBytes;
2831
}
2932
}

src/Components/Web/test/Forms/InputFileChangeEventArgsTest.cs

+11
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,16 @@ public void GetMultipleFiles_ThrowsIfTooManyFiles()
6565
var ex = Assert.Throws<InvalidOperationException>(() => instance.GetMultipleFiles(1));
6666
Assert.Equal($"The maximum number of files accepted is 1, but 2 were supplied.", ex.Message);
6767
}
68+
69+
[Fact]
70+
public void ReadRequestTypeCanBeLoaded()
71+
{
72+
// Represents https://github.com/dotnet/aspnetcore/issues/26882
73+
// Even though the ReadRequest type is only ever used on WebAssembly, developers might
74+
// do something that causes the type to be loaded on other environments, for example
75+
// using reflection. It's just a DTO with no behaviors so there's nothing to test
76+
// except that loading the type doesn't trigger an exception.
77+
GC.KeepAlive(new ReadRequest());
78+
}
6879
}
6980
}

0 commit comments

Comments
 (0)