dotnet / dotnet/runtime

Bound check is not removed for logical or pattern + assertions

Open
#122,146 1 comment 0 reactions 1 assignee Claimed by @EgorBo View on GitHub
area-CodeGen-coreclr reduce-unsafe
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

```cs
private static ReadOnlySpan Char2val => // 128
[
/* 0-15 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 16-31 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 32-47 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 62, 0xFF, 0xFF, 0xFF, 63,
/* 48-63 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xFF, 0xFF, 0xFF, 64, 0xFF, 0xFF,
/* 64-79 */ 0xFF, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
/* 80-95 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 96-111 */ 0xFF, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
/* 112-127 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
];

static void Foo(char[] pch)
{
if (pch.Length >= 4)
{
char pch0 = pch[0];
char pch1 = pch[1];
char pch2 = pch[2];
char pch3 = pch[3];
if ((pch0 | pch1 | pch2 | pch3) >= 128)
throw new FormatException();
Console.WriteLine(Char2val[pch0]);
Console.WriteLine(Char2val[pch1]);
Console.WriteLine(Char2val[pch2]);
Console.WriteLine(Char2val[pch3]);
}
}
```
Here all four `Char2val` accesses have bounds checks while they should be elided due to `if ((pch0 | pch1 | pch2 | pch3) >= 128)` check. This pattern is used in Base64Encoder.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.