Skip to content

NRE in encoders #89931

Description

@NinoFloris

Description

Certain byte sequences causes certain encoders to throw NREs instead of overflow exceptions.

Reproduction Steps

using System;
using System.Text;

var str = "😀";
var encoding = Encoding.Unicode;
var bytes = encoding.GetBytes(str);
var decoder = encoding.GetDecoder();

var chars = new char[0];

decoder.Convert(bytes.AsSpan(), chars.AsSpan(), true, out _, out _, out var completed);

Expected behavior

Expecting the same exception as something like this gives:

var str = "件";
...

Actual behavior

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Text.UnicodeEncoding.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, DecoderNLS baseDecoder)
   at System.Text.DecoderNLS.Convert(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, Boolean flush, Int32& bytesUsed, Int32& charsUsed, Boolean& completed)
   at System.Text.Decoder.Convert(ReadOnlySpan`1 bytes, Span`1 chars, Boolean flush, Int32& bytesUsed, Int32& charsUsed, Boolean& completed)

Regression?

No response

Known Workarounds

No response

Configuration

macOS Ventura, 8.0 preview 7

Other information

UTF32 shares the same issue, throwing an NRE, UTF8 is working correctly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Encodingbugin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions