typelevel / typelevel/fs2

decodeCWithCharset error handling inconsistency

Open
#2,672 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
2.5k
Forks
636
Avg merge
2d 4h
Merged PRs (30d)
7

Description

utf8.decodeC is a specialization of the generic private decoder used for other charsets:

  def decodeCWithCharset[F[_]: RaiseThrowable](charset: Charset): Pipe[F, Chunk[Byte], String] =
    if (charset.name() == StandardCharsets.UTF_8.name())
      utf8.decodeC
    else
      decodeCWithGenericCharset(charset)

On bad input, the UTF8 decoder succeeds with the Unicode replacement character, and the other charsets raise an error:

scala> Stream(0xc0, 0xaf).map(_.toByte).covary[Fallible].chunks.through(text.utf8.decodeC).compile.string
val res14: scala.util.Either[Throwable,String] = Right(��)

scala> Stream(0xc0, 0xaf).map(_.toByte).covary[Fallible].chunks.through(text.decodeCWithGenericCharset(UTF_8)).compile.string
val res15: scala.util.Either[Throwable,String] = Left(java.nio.charset.MalformedInputException: Input length = 1)

There's no visible UTF-8 encoder that raises errors, nor is there an generic encoder that uses replacement characters. We might want to make the behavior configurable, but it should at least be consistent.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the utf8.decodeC path in decodeCWithCharset with decodeCWithGenericCharset, using the malformed UTF-8 examples in the issue. Determine the intended configurable error behavior, then ensure the UTF-8 and generic charset decoders handle invalid input consistently and add coverage for both paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
stream-processing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.