Skip to content

Commit c6d5074

Browse files
committed
Address feedback
1 parent c533394 commit c6d5074

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

docs/standard/unsafe-code/best-practices.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,8 @@ ref object obj = ref Unsafe.AsRef<object>((void*)0);
644644
```
645645

646646
The risk of introducing memory safety issues is admittedly low any attempt to dereference
647-
a null byref will lead to a well-defined `NullReferenceException`. However, since this is documented as a disallowed operation,
648-
and since the runtime and .NET tools might assume soundness while performing various optimizations, the actual observed behavior
649-
might be something other than what the developer expected.
650-
651-
For example, the C# compiler [assumes](https://github.com/dotnet/roslyn/issues/72165) that dereferencing a byref always succeeds
647+
a null byref will lead to a well-defined `NullReferenceException`.
648+
However, the C# compiler [assumes](https://github.com/dotnet/roslyn/issues/72165) that dereferencing a byref always succeeds
652649
and produces no observable side effect. Therefore it is a legal optimization to elide any dereference whose resulting value is
653650
immediately thrown away. See [dotnet/runtime#98681](https://github.com/dotnet/runtime/pull/98681) (and
654651
[this related comment](https://github.com/dotnet/runtime/pull/98623#discussion_r1493490532)) for an example of a now-fixed bug within .NET
@@ -902,7 +899,7 @@ Avoid using such techniques unless absolutely necessary.
902899

903900
## 19. Uninitialized locals `[SkipLocalsInit]` and `Unsafe.SkipInit`
904901

905-
`[SkipLocalsInit]` was introduced in .NET 5.0 to allow the JIT to skip zeroing local variables in methods, either on a per-method basis or assembly-wide. This feature was often used to help the JIT eliminate redundant zero initializations, such as those for `stackalloc`. However, it can lead to undefined behavior if locals are not explicitly initialized before use. With recent improvements in the JIT's ability to eliminate zero-initializations and perform vectorization, the need for `[SkipLocalsInit]` and `Unsafe.SkipInit` has significantly decreased.
902+
`[SkipLocalsInit]` was introduced in .NET 5.0 to allow the JIT to skip zeroing local variables in methods, either on a per-method basis or module-wide. This feature was often used to help the JIT eliminate redundant zero initializations, such as those for `stackalloc`. However, it can lead to undefined behavior if locals are not explicitly initialized before use. With recent improvements in the JIT's ability to eliminate zero-initializations and perform vectorization, the need for `[SkipLocalsInit]` and `Unsafe.SkipInit` has significantly decreased.
906903

907904
### Recommendations
908905

0 commit comments

Comments
 (0)