-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: fix candid decoding at immutable array types to support opt d…
…efaulting (#4240) Another Candid decoding bug, seems independent of #4238. I think the code for decoding immutable arrays was never extended to handle backtracking correctly. When recovery is an option (haha) and the type descriptor does not match, the current code would continue to try to decode the array length and elements. When recovery is not an option, the previous trap would render this code unreachable. But when recovery is enabled, we need to bail and not try to read the array size and elements but just return the sentinel value instead. - [ ] Consider applying the same refactoring to mutable array, if possible. Since these aren't Candid, but extended Candid (for stable variables), and don't need to support recovery, the change is (probably) not necessary but nice for uniformity only. - [ ] restore or delete original repro - I must have edited it while experimenting
- Loading branch information
Showing
5 changed files
with
121 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
let b : Blob = to_candid ( | ||
?[{a=?#a}, | ||
{a=?#b{}}, | ||
{a=?#b{}}, | ||
{a=?#a}, | ||
{a=?#c}, | ||
{a=?#a}] : ?[{a:?{#a;#b:{};#c}}]); | ||
|
||
let o1 = (from_candid b) : ?[{a:?{#a}}]; //note missing ?, forcing decoding at incorrect array type | ||
assert o1 == null; | ||
|
||
let o2 = (from_candid b) : ??[{a:?{#a}}]; //intended example, with embedded defaulting | ||
assert o2 == | ||
??[{a=?#a}, | ||
{a=null}, | ||
{a=null}, | ||
{a=?#a}, | ||
{a=null}, | ||
{a=?#a}]; | ||
|
||
//SKIP run | ||
//SKIP run-ir | ||
//SKIP run-low |
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,78 @@ | ||
import P "mo:prim"; | ||
|
||
do { | ||
let b : Blob = to_candid( | ||
(#a 1) : | ||
({#a:Int}) | ||
); | ||
let o = (from_candid b) :?({#a:Int}); | ||
P.debugPrint(debug_show(o)); | ||
assert o == ? (#a 1); | ||
}; // ok | ||
|
||
do { | ||
let b : Blob = to_candid( | ||
(#a 1) : | ||
({#a:Int}) | ||
); | ||
let o = (from_candid b) :?({#b:Int}); | ||
P.debugPrint(debug_show(o)); | ||
assert o == null; | ||
}; // ok | ||
|
||
do { | ||
let b : Blob = to_candid( | ||
(#b 1) : | ||
({#b:Int}) | ||
); | ||
let o = (from_candid b) :?({#b:Nat}); | ||
P.debugPrint(debug_show(o)); | ||
assert o == null; | ||
}; | ||
|
||
|
||
do { | ||
let b : Blob = to_candid( | ||
[#b 1] : | ||
[{#b:Int}] | ||
); | ||
let o = (from_candid b) :?[{#b:Nat}]; | ||
P.debugPrint(debug_show(o)); | ||
assert o == null; | ||
}; // ok | ||
|
||
|
||
do { | ||
let b : Blob = to_candid( | ||
?[1] : | ||
?[Int] | ||
); | ||
let o = (from_candid b) :??[Nat]; | ||
P.debugPrint(debug_show(o)); | ||
assert o == ?null; | ||
}; // ok | ||
|
||
do { | ||
let b : Blob = to_candid( | ||
?[1] : | ||
?[Int] | ||
); | ||
let o = (from_candid b) :?[Nat]; | ||
P.debugPrint(debug_show(o)); | ||
assert o == null; | ||
}; // traps due to broken back-tracking in array decoding | ||
|
||
|
||
do { | ||
let b : Blob = to_candid( | ||
?[#b 1] : | ||
?[{#b:Int}] | ||
); | ||
let o = (from_candid b) :?[{#b:Nat}]; | ||
P.debugPrint(debug_show(o)); | ||
assert o == null; | ||
}; // traps due to broken back-tracking in array decoding | ||
|
||
//SKIP run | ||
//SKIP run-ir | ||
//SKIP run-low |
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,7 @@ | ||
?(#a(+1)) | ||
null | ||
null | ||
null | ||
?null | ||
null | ||
null |