Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Recursive ADTs fail to Derive #2101

Open
zarthross opened this issue Feb 20, 2023 · 2 comments · May be fixed by #2187
Open

[Bug] Recursive ADTs fail to Derive #2101

zarthross opened this issue Feb 20, 2023 · 2 comments · May be fixed by #2187

Comments

@zarthross
Copy link
Member

This simple recursive data structure fails to derive an Encoder:

sealed trait Tree; 
case class Branch(l: Tree, r: Tree) extends Tree; 
case object Leaf extends Tree;

With this error:

Welcome to Scala 3.2.2 (17.0.6, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                
scala> import _root_.io.circe._
                                                                                
scala> sealed trait Tree; case class Branch(l: Tree, r: Tree) extends Tree; case object Leaf extends Tree;;Encoder.AsObject.derived[Tree]
-- Error: ----------------------------------------------------------------------
  1 |sealed trait Tree; case class Branch(l: Tree, r: Tree) extends Tree; case object Leaf extends Tree;;Encoder.AsObject.derived[Tree]
    |                                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                   Maximal number of successive inlines (32) exceeded,
    |                   Maybe this is caused by a recursive inline method?
    |                   You can use -Xmax-inlines to change the limit.
    |---------------------------------------------------------------------------
    |Inline stack trace
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 20 |    case _: Mirror.Of[A]     => ConfiguredEncoder.derived[A]
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 15 |    case _: (t *: ts)  => summonEncoder[t] :: summonEncoders[ts]
    |                          ^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 32 |      lazy val elemEncoders: List[Encoder[?]] = summonEncoders[mirror.MirroredElemTypes]
    |                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 20 |    case _: Mirror.Of[A]     => ConfiguredEncoder.derived[A]
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 15 |    case _: (t *: ts)  => summonEncoder[t] :: summonEncoders[ts]
    |                          ^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 32 |      lazy val elemEncoders: List[Encoder[?]] = summonEncoders[mirror.MirroredElemTypes]
    |                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 20 |    case _: Mirror.Of[A]     => ConfiguredEncoder.derived[A]
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 15 |    case _: (t *: ts)  => summonEncoder[t] :: summonEncoders[ts]
    |                          ^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from rs$line$2:1
 32 |      lazy val elemEncoders: List[Encoder[?]] = summonEncoders[mirror.MirroredElemTypes]
    |                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.........
@plokhotnyuk
Copy link
Contributor

plokhotnyuk commented Feb 20, 2023

A better option would be enabling derivation of codecs for recursive ADTs by configuration, and have it turned off by default for the security reason.

Malicious counterparts can submit too deeply nested JSON that will DoS systems opened to untrusted input by stack overflows.

@Lasering
Copy link
Contributor

Lasering commented Feb 27, 2023

If the implementation does not cause stack overflow there is no need to have a configuration for it.

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

Successfully merging a pull request may close this issue.

3 participants