-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LF: Add check of nesting in SValue.toValue (#10370)
The conversion of SValue to Value already ensures the resulting value has a serializable type. Here we add a check to ensure it does not overpass the maximum allow nesting.
- Loading branch information
1 parent
91529ee
commit 4a33c03
Showing
9 changed files
with
106 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
daml-lf/interpreter/src/test/scala/com/digitalasset/daml/lf/speedy/SValueTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.lf | ||
package speedy | ||
|
||
import com.daml.lf.data.Ref | ||
import com.daml.lf.value.Value | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.annotation.tailrec | ||
import scala.util.{Failure, Success, Try} | ||
|
||
class SValueTest extends AnyWordSpec with Matchers { | ||
|
||
"SValue#toValue" should { | ||
|
||
val Nat = Ref.Identifier.assertFromString("-pkgId:Mod:Nat") | ||
val Z = Ref.Name.assertFromString("Z") | ||
val S = Ref.Name.assertFromString("S") | ||
|
||
@tailrec | ||
def toNat(i: Int, acc: SValue = SValue.SVariant(Nat, Z, 0, SValue.SUnit)): SValue = | ||
if (i <= 0) acc | ||
else toNat(i - 1, SValue.SVariant(Nat, S, 1, acc)) | ||
|
||
"rejects excessive nesting" in { | ||
// Because Z has a nesting of 1, toNat(Value.MAXIMUM_NESTING) has a nesting of | ||
// Value.MAXIMUM_NESTING + 1 | ||
Try(toNat(Value.MAXIMUM_NESTING).toValue) shouldBe | ||
Failure(SError.SErrorDamlException(interpretation.Error.ValueExceedsMaxNesting)) | ||
} | ||
|
||
"accepts just right nesting" in { | ||
// Because Z has a nesting of 1, toNat(Value.MAXIMUM_NESTING - 1) has a nesting of | ||
// Value.MAXIMUM_NESTING | ||
Try(toNat(Value.MAXIMUM_NESTING - 1)) shouldBe a[Success[_]] | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters