Skip to content

Commit 682fa85

Browse files
authored
Added helper functions for toInt/toNat that check for _ characters. (#881)
Signed-off-by: Liana Hadarean <hadarean@amazon.com>
1 parent 5cbc971 commit 682fa85

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

cedar-lean/Cedar/Spec/Ext/Datetime.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-/
1616

1717
import Cedar.Data.Int64
18+
import Cedar.Spec.Ext.Util
1819
import Std.Time
1920
import Std.Time.Format
2021

@@ -227,7 +228,7 @@ def parseUnit? (isNegative : Bool) (str : String) (suffix : String) : Option (In
227228
if digits.isEmpty
228229
then none
229230
else do
230-
let nUnsignedUnits ← String.toNat? (String.ofList digits)
231+
let nUnsignedUnits ← toNat?' (String.ofList digits)
231232
let units ← if isNegative
232233
then durationUnits? (Int.negOfNat nUnsignedUnits) suffix
233234
else durationUnits? (Int.ofNat nUnsignedUnits) suffix

cedar-lean/Cedar/Spec/Ext/Decimal.lean

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-/
1616

1717
import Cedar.Data.Int64
18+
import Cedar.Spec.Ext.Util
1819

1920
/-! This file defines Cedar decimal values and functions. -/
2021

@@ -48,11 +49,7 @@ def parse (str : String) : Option Decimal :=
4849
let rlen := right.length
4950
if 0 < rlen ∧ rlen ≤ DECIMAL_DIGITS
5051
then
51-
-- String.toInt? has undocumented behavior (well, undocumented as of this writing)
52-
-- that it allows `_` characters in certain positions in the representation of an
53-
-- integer. We want to disallow strings with `_` characters per the Cedar spec.
54-
if left.contains '_' ∨ right.contains '_' then .none else
55-
match String.toInt? left, String.toNat? right with
52+
match toInt?' left, toNat?' right with
5653
| .some l, .some r =>
5754
let l' := l * (Int.pow 10 DECIMAL_DIGITS)
5855
let r' := r * (Int.pow 10 (DECIMAL_DIGITS - rlen))

cedar-lean/Cedar/Spec/Ext/IPAddr.lean

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
limitations under the License.
1515
-/
1616

17+
import Cedar.Spec.Ext.Util
18+
1719
/-! This file defines Cedar IpAddr values and functions. -/
1820

1921
namespace Cedar.Spec.Ext
@@ -136,15 +138,15 @@ private def parsePrefixNat (str : String) (digits : Nat) (size : Nat) : Option (
136138
let len := str.length
137139
if 0 < len && len ≤ digits && (str.startsWith "0" → str = "0")
138140
then do
139-
let n ← str.toNat?
141+
let n ← toNat?' str
140142
if n ≤ size then .some (Fin.ofNat (size+1) n) else .none
141143
else .none
142144

143145
private def parseNumV4 (str : String) : Option (BitVec 8) :=
144146
let len := str.length
145147
if 0 < len && len ≤ 3 && (str.startsWith "0" → str = "0")
146148
then do
147-
let n ← str.toNat?
149+
let n ← toNat?' str
148150
if n ≤ 0xff then .some n else .none
149151
else .none
150152

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Cedar.Spec.Ext
2+
3+
----- Helper Functions -----
4+
5+
/--
6+
Helper function that wraps String.toInt? but returns .none if the string contains '_'.
7+
This prevents the undocumented behavior where String.toInt? allows '_' characters.
8+
-/
9+
def toInt?' (str : String) : Option Int :=
10+
if str.contains '_' then .none else String.toInt? str
11+
12+
/--
13+
Helper function that wraps String.toNat? but returns .none if the string contains '_'.
14+
This prevents the undocumented behavior where String.toNat? allows '_' characters.
15+
-/
16+
def toNat?' (str : String) : Option Nat :=
17+
if str.contains '_' then .none else String.toNat? str

0 commit comments

Comments
 (0)