Skip to content

Correctness bug: String.EndsWith may ignore trailing null characters #85064

@Smaug123

Description

@Smaug123

Description

String.EndsWith can give the wrong answer for a string which ends with a null character.

Reproduction Steps

> dotnet fsi
> "\n\000".EndsWith "\n";;
val it: bool = true

> "\n\000".EndsWith '\n';;
val it: bool = false

(The problem also manifests in the invariant culture.)

Expected behavior

The string consisting of a newline should not be considered a suffix of the string consisting of a newline and then a null character.

Actual behavior

The string consisting of a newline is considered to be a suffix of the string consisting of a newline and then a null character.

Regression?

No response

Known Workarounds

Use s.EndsWith '\n' instead.

Configuration

> dotnet --version     
7.0.203

Running on darwin-aarch64, obtained through Nixpkgs at commit 639d4f17218568afd6494dbd807bebb2beb9d6b3, macOS Ventura 13.3.1 (22E261).

Observed in .NET 6 on Windows x86-64 as well.

Other information

This can also be triggered with \001 or \010 instead of \000, but not \r.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions