Skip to content

Commit b04ba98

Browse files
Copilotjkotas
andauthored
Clarify Marshal AccessViolationException compatibility comments (#128379)
Updates misleading AccessViolationException comments in Marshal interop helpers to accurately describe compatibility behavior. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
1 parent 99f87de commit b04ba98

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

  • src
    • coreclr
    • libraries/System.Private.CoreLib/src/System/Runtime/InteropServices

src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static long ReadInt64([MarshalAs(UnmanagedType.AsAny), In] object ptr, in
9999
/// </remarks>
100100
private static unsafe T ReadValueSlow<T>(object ptr, int ofs, Func<IntPtr, int, T> readValueHelper)
101101
{
102-
// Consumers of this method are documented to throw AccessViolationException on any AV
102+
// Compatibility: null input to these obsolete APIs throws AccessViolationException.
103103
if (ptr is null)
104104
{
105105
throw new AccessViolationException();
@@ -164,7 +164,7 @@ public static void WriteInt64(object ptr, int ofs, long val)
164164
/// </summary>
165165
private static unsafe void WriteValueSlow<T>(object ptr, int ofs, T val, Action<IntPtr, int, T> writeValueHelper)
166166
{
167-
// Consumers of this method are documented to throw AccessViolationException on any AV
167+
// Compatibility: null input to these obsolete APIs throws AccessViolationException.
168168
if (ptr is null)
169169
{
170170
throw new AccessViolationException();

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NativeAot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public static unsafe long ReadInt64(object ptr, int ofs)
234234
[RequiresDynamicCode("Marshalling code for the object might not be available")]
235235
private static unsafe T ReadValueSlow<T>(object ptr, int ofs, delegate*<IntPtr, int, T> readValueHelper)
236236
{
237-
// Consumers of this method are documented to throw AccessViolationException on any AV
237+
// Compatibility: null input to these obsolete APIs throws AccessViolationException.
238238
if (ptr is null)
239239
{
240240
throw new AccessViolationException();
@@ -312,7 +312,7 @@ public static unsafe void WriteInt64(object ptr, int ofs, long val)
312312
[RequiresDynamicCode("Marshalling code for the object might not be available")]
313313
private static unsafe void WriteValueSlow<T>(object ptr, int ofs, T val, delegate*<IntPtr, int, T, void> writeValueHelper)
314314
{
315-
// Consumers of this method are documented to throw AccessViolationException on any AV
315+
// Compatibility: null input to these obsolete APIs throws AccessViolationException.
316316
if (ptr is null)
317317
{
318318
throw new AccessViolationException();

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public static unsafe byte ReadByte(IntPtr ptr, int ofs)
293293
}
294294
catch (NullReferenceException)
295295
{
296-
// this method is documented to throw AccessViolationException on any AV
296+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
297297
throw new AccessViolationException();
298298
}
299299
}
@@ -317,7 +317,7 @@ public static unsafe short ReadInt16(IntPtr ptr, int ofs)
317317
}
318318
catch (NullReferenceException)
319319
{
320-
// this method is documented to throw AccessViolationException on any AV
320+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
321321
throw new AccessViolationException();
322322
}
323323
}
@@ -341,7 +341,7 @@ public static unsafe int ReadInt32(IntPtr ptr, int ofs)
341341
}
342342
catch (NullReferenceException)
343343
{
344-
// this method is documented to throw AccessViolationException on any AV
344+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
345345
throw new AccessViolationException();
346346
}
347347
}
@@ -388,7 +388,7 @@ public static unsafe long ReadInt64(IntPtr ptr, int ofs)
388388
}
389389
catch (NullReferenceException)
390390
{
391-
// this method is documented to throw AccessViolationException on any AV
391+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
392392
throw new AccessViolationException();
393393
}
394394
}
@@ -404,7 +404,7 @@ public static unsafe void WriteByte(IntPtr ptr, int ofs, byte val)
404404
}
405405
catch (NullReferenceException)
406406
{
407-
// this method is documented to throw AccessViolationException on any AV
407+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
408408
throw new AccessViolationException();
409409
}
410410
}
@@ -428,7 +428,7 @@ public static unsafe void WriteInt16(IntPtr ptr, int ofs, short val)
428428
}
429429
catch (NullReferenceException)
430430
{
431-
// this method is documented to throw AccessViolationException on any AV
431+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
432432
throw new AccessViolationException();
433433
}
434434
}
@@ -461,7 +461,7 @@ public static unsafe void WriteInt32(IntPtr ptr, int ofs, int val)
461461
}
462462
catch (NullReferenceException)
463463
{
464-
// this method is documented to throw AccessViolationException on any AV
464+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
465465
throw new AccessViolationException();
466466
}
467467
}
@@ -508,7 +508,7 @@ public static unsafe void WriteInt64(IntPtr ptr, int ofs, long val)
508508
}
509509
catch (NullReferenceException)
510510
{
511-
// this method is documented to throw AccessViolationException on any AV
511+
// Convert NRE for null/near-null pointers to match the documented AccessViolationException.
512512
throw new AccessViolationException();
513513
}
514514
}

0 commit comments

Comments
 (0)