Skip to content

.net 5.0 seems to be wrong in detecting zero-terminzted string #45912

Closed

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


[severity:It's more difficult to complete my work] [regression] [worked-in:16.7]
The following code:

     class Program
     {
         static void Main(string[] args)
         {
             const string s = "abc";
             const string sz = "abc\0";
    
void Report(string str) 
                 => Console.WriteLine($"\"{str}\" ({str. Length}), zero-terminated: {str. EndsWith("\0")}");
    
Report(s);
             Report(sz);
         }
     }

gives the following output for target framework .net 5.0:

"abc" (3), zero-terminated: True
"abc" (4), zero-terminated: True

For target framework .netcoreapp3.1 the code outputs the following lines:

"abc" (3), zero-terminated: False
"abc" (4), zero-terminated: True

As for me, .net core 3.1 behavior looks correct and .net 5.0 does not.


Original Comments

Feedback Bot on 12/9/2020, 10:13 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

Activity

  1. Dotnet-GitSync-Bot commented on Dec 10, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  2. added
    untriaged
    on Dec 10, 2020
  3. danmoseley commented on Dec 10, 2020

    @danmoseley
    Contributor

    @safern, the repro goes away if I set DOTNET_SYSTEM_GLOBALIZATION_USENLS=1

    Did we discuss this case already?

  4. GrabYourPitchforks commented on Dec 10, 2020

    @GrabYourPitchforks
    Member

    This behavior is by design. EndsWith(string) is linguistic (culture-aware), and the null character has zero weight under ICU. Caller almost certainly meant to perform an ordinal comparison via EndsWith(char) or EndsWith(..., StringComparison.Ordinal).

  5. safern commented on Dec 10, 2020

    @safern
    Member

    Thanks, @GrabYourPitchforks you beat me to it.

  6. removed
    untriaged
    on Dec 10, 2020
  7. tarekgh commented on Dec 10, 2020

    @tarekgh
    Member

    @GrabYourPitchforks is right. For such operation ordinal comparison has to be used. Also why need to use EndsWith at all? why not just testing str[str.Length - 1] == `\0' ?

  8. GrabYourPitchforks commented on Dec 10, 2020

    @GrabYourPitchforks
    Member

    To be fair, EndsWith is more readable. And it works with empty strings. :)

  9. tarekgh commented on Dec 10, 2020

    @tarekgh
    Member

    To be fair, EndsWith is more readable. And it works with empty strings. :)

    That is a good point. but do you consider the empty string is null terminated or not? for such user scenarios, it should be true I guess.

  10. ghost locked as resolved and limited conversation to collaborators on Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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