dotnet / dotnet/dotnet-api-docs

Should docs state that Decoder and Encoder corrupt internal state during exceptional code paths?

Open
#4,142 0 comments 0 reactions 0 assignees View on GitHub
area-System.Text.Encoding Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

For [`System.Text.Decoder`](https://docs.microsoft.com/en-us/dotnet/api/system.text.decoder) and [`System.Text.Encoder`](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoder), the general pattern is that callers should ensure the destination buffers are large enough to hold the converted output.

It is _not_ appropriate for the caller to catch exceptions from these APIs, double the destination buffer, and try the operation again. The `Decoder` and `Encoder` instances mutate their internal state during instance method calls, and exceptions occurring during these method calls can leave the internal instance state undefined. This could result in data corruption when attempting to use these instances for subsequent operations.

Does this behavior need to be documented? It was suggested on another thread that we state this explicitly. I wanted to gauge the feedback here because I was under the impression that `Encoder` and `Decoder` aren't unique here; that it's generally assumed framework-wide that exceptions might leave the target object in an indeterminate state. Similar to how we assume that instance methods are not thread-safe unless explicitly documented as being thread-safe.

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.