MemoryExtensions.LastIndexOf ignores a custom comparer for bitwise-equatable element types
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 74/100
Hướng nghiên cứu
Tìm overload của MemoryExtensions.LastIndexOf<T> nhận hai giá trị ReadOnlySpan<T> và một IEqualityComparer<T> trong mã nguồn runtime. So sánh điều kiện đường dẫn nhanh theo bit của overload này với các overload comparer liên quan được đề cập trong issue, sau đó kiểm tra các bài kiểm thử hiện có cho overload này. Mục tiêu là comparer tùy chỉnh được cung cấp quyết định kết quả đối với các kiểu có thể so sánh theo bit, đồng thời hành vi của comparer mặc định vẫn được kiểm thử.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
MemoryExtensions.LastIndexOf<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IEqualityComparer<T>?) appears to ignore a non-default comparer when T is known to be bitwise equatable.
The implementation takes the bitwise fast path whenever RuntimeHelpers.IsBitwiseEquatable<T>() returns true, without checking whether comparer is null or EqualityComparer<T>.Default.
As a result, a custom comparer is not invoked for types such as byte and char, and the returned index can differ from the result implied by the API contract.
The related comparer overloads generally restrict bitwise fast paths to the default comparer, for example:
comparer is null || comparer == EqualityComparer<T>.Default
Reproduction Steps
using System;
using System.Collections.Generic;
ReadOnlySpan<byte> source = [1, 2, 3];
ReadOnlySpan<byte> value = [9];
var comparer = new AllBytesEqualComparer();
var index = source.LastIndexOf(value, comparer);
Console.WriteLine(index);
sealed class AllBytesEqualComparer : IEqualityComparer<byte>
{
public bool Equals(byte x, byte y) => true;
public int GetHashCode(byte value) => 0;
}
Expected behavior
The custom comparer should be used.
Because the comparer considers every pair of bytes equal, the one-element sequence should match at the last valid position, and the result should be: 2
Actual behavior
The result is: -1
Regression?
No known regression
Known Workarounds
Avoid this overload for bitwise-equatable element types when using a custom comparer, and implement the sequence search manually.
Configuration
.NET SDK:
Version: 10.0.402
Commit: a8dd0577c5
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.402\
Other information
The fast-path condition currently appears to be equivalent to:
if (RuntimeHelpers.IsBitwiseEquatable<T>())
{
// Bitwise search.
}
This bypasses the supplied comparer.
A possible fix may be to restrict the fast path to the default comparer:
if (RuntimeHelpers.IsBitwiseEquatable<T>() && (comparer is null || comparer == EqualityComparer<T>.Default))
{
// Bitwise search.
}
- Ngôn ngữ chính
- C#
- Star
- 18.4k
- Fork
- 5.7k
- Merge trung bình
- 2 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 646
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/runtime
-
[mono] Native memory leak on the first invoke of every open-instance delegate to a virtual methodĐang mởarea-VM-meta-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
dotnet/runtime#136284 · 6 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Several MemoryExtensions.LastIndexOfAnyExcept overloads incorrectly document "first occurrence"Có thể đã có người làm @ehsan-raoufi đã nhận 1 ngày trước. Đang mởarea-System.Memory untriaged
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 83/100
dotnet/runtime#136282 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-System.IO.Compression untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
dotnet/runtime#136215 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-CodeGen-coreclr untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
dotnet/runtime#135849 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[wasm] Emscripten clang wrapper fails when the SDK path contains a spaceCó thể đã có người làm @agocke đã nhận 15 ngày trước. Đang mởarch-wasm area-Infrastructure untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
dotnet/runtime#135416 · 2 bình luận · 2 reaction · 2 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của dotnet/runtime
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
unoplatform/uno.check#591 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
weaviate/csharp-client#378 ·
-
agentic-workflows automated testing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Azure-Samples/Legacy-Modernization-Agents#194 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-encryption area-security p1-recommended technical-debt
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug flaky small
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
hexide-io/HexIDE#1906 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày