Commit 6f3671d
[src] Update code to honor the first rule of unsafe coding practices. (#23835)
Don't do this:
```cs
unsafe void UnreliableCode(ref int x)
{
int* nativePointer = (int*)Unsafe.AsPointer(ref x);
nativePointer[0] = 42;
}
```
The reason being is that the GC can move the 'ref int' argument (which
doesn't
seem right at first glance, but objects can be moved by the GC, and the
'ref
int' argument can point to an int field inside an object), which would
make
the pointer point to an invalid memory location.
The proper pattern is:
```cs
unsafe void ReliableCode(ref int x)
{
fixed (int* ptr = &x) {
ptr[0] = 42;
}
}
```
So update everything to use this pattern instead.
References:
*
https://github.com/dotnet/designs/blob/67ad92c511d8f6a2b8b500bd6f13f865b163e282/accepted/2025/memory-safety/unsafe-code-guidelines.md#1-untracked-managed-pointers-unsafeaspointer-and-friends
---------
Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
Co-authored-by: Alex Soto <alex@soto.dev>1 parent 632f706 commit 6f3671d
File tree
60 files changed
+802
-492
lines changed- src
- Accelerate
- AudioToolbox
- AudioUnit
- CoreFoundation
- CoreGraphics
- CoreMedia
- CoreText
- CoreVideo
- Foundation
- GameController
- MapKit
- MediaToolbox
- ObjCRuntime
- OpenGLES
- OpenGL
- PrintCore
- SearchKit
- Security
- SystemConfiguration
- VideoToolbox
- bgen
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
60 files changed
+802
-492
lines changedLarge diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
977 | 977 | | |
978 | 978 | | |
979 | 979 | | |
980 | | - | |
981 | | - | |
982 | | - | |
983 | | - | |
984 | | - | |
985 | | - | |
986 | | - | |
987 | | - | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
988 | 990 | | |
989 | 991 | | |
990 | 992 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1270 | 1270 | | |
1271 | 1271 | | |
1272 | 1272 | | |
1273 | | - | |
1274 | | - | |
1275 | | - | |
1276 | | - | |
1277 | | - | |
1278 | | - | |
1279 | | - | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
1280 | 1282 | | |
1281 | 1283 | | |
1282 | 1284 | | |
| |||
1556 | 1558 | | |
1557 | 1559 | | |
1558 | 1560 | | |
1559 | | - | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
1560 | 1564 | | |
1561 | 1565 | | |
1562 | 1566 | | |
| |||
1600 | 1604 | | |
1601 | 1605 | | |
1602 | 1606 | | |
1603 | | - | |
1604 | | - | |
1605 | | - | |
1606 | | - | |
1607 | | - | |
1608 | | - | |
1609 | | - | |
1610 | | - | |
1611 | | - | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
1612 | 1618 | | |
1613 | 1619 | | |
1614 | 1620 | | |
| |||
1715 | 1721 | | |
1716 | 1722 | | |
1717 | 1723 | | |
1718 | | - | |
| 1724 | + | |
| 1725 | + | |
1719 | 1726 | | |
1720 | 1727 | | |
1721 | 1728 | | |
| |||
1749 | 1756 | | |
1750 | 1757 | | |
1751 | 1758 | | |
1752 | | - | |
| 1759 | + | |
| 1760 | + | |
1753 | 1761 | | |
1754 | 1762 | | |
1755 | 1763 | | |
| |||
1785 | 1793 | | |
1786 | 1794 | | |
1787 | 1795 | | |
1788 | | - | |
| 1796 | + | |
| 1797 | + | |
1789 | 1798 | | |
1790 | 1799 | | |
1791 | 1800 | | |
| |||
1931 | 1940 | | |
1932 | 1941 | | |
1933 | 1942 | | |
1934 | | - | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
1935 | 1946 | | |
1936 | 1947 | | |
1937 | 1948 | | |
| |||
1959 | 1970 | | |
1960 | 1971 | | |
1961 | 1972 | | |
1962 | | - | |
| 1973 | + | |
| 1974 | + | |
1963 | 1975 | | |
1964 | 1976 | | |
1965 | 1977 | | |
| |||
1988 | 2000 | | |
1989 | 2001 | | |
1990 | 2002 | | |
1991 | | - | |
| 2003 | + | |
| 2004 | + | |
1992 | 2005 | | |
1993 | 2006 | | |
1994 | 2007 | | |
| |||
2007 | 2020 | | |
2008 | 2021 | | |
2009 | 2022 | | |
2010 | | - | |
2011 | | - | |
2012 | | - | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
2013 | 2028 | | |
2014 | 2029 | | |
2015 | 2030 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
243 | | - | |
244 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
245 | 247 | | |
246 | 248 | | |
247 | 249 | | |
248 | 250 | | |
249 | 251 | | |
250 | | - | |
251 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
252 | 256 | | |
253 | 257 | | |
254 | 258 | | |
255 | 259 | | |
256 | 260 | | |
257 | | - | |
258 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
259 | 265 | | |
260 | 266 | | |
261 | 267 | | |
| |||
304 | 310 | | |
305 | 311 | | |
306 | 312 | | |
307 | | - | |
308 | | - | |
309 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
310 | 317 | | |
311 | 318 | | |
312 | 319 | | |
313 | 320 | | |
314 | 321 | | |
315 | 322 | | |
316 | | - | |
317 | | - | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
318 | 327 | | |
319 | 328 | | |
320 | 329 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
474 | 474 | | |
475 | 475 | | |
476 | 476 | | |
477 | | - | |
| 477 | + | |
| 478 | + | |
478 | 479 | | |
479 | 480 | | |
480 | 481 | | |
| |||
502 | 503 | | |
503 | 504 | | |
504 | 505 | | |
505 | | - | |
| 506 | + | |
| 507 | + | |
506 | 508 | | |
507 | 509 | | |
508 | 510 | | |
| |||
530 | 532 | | |
531 | 533 | | |
532 | 534 | | |
533 | | - | |
| 535 | + | |
| 536 | + | |
534 | 537 | | |
535 | 538 | | |
536 | 539 | | |
| |||
565 | 568 | | |
566 | 569 | | |
567 | 570 | | |
568 | | - | |
| 571 | + | |
| 572 | + | |
569 | 573 | | |
570 | 574 | | |
571 | 575 | | |
| |||
0 commit comments