Skip to content

Commit 4992f27

Browse files
authored
Avoid index error when creating zero literal (#167)
* Avoid index error when creating zero literal Trying to use `0'u256` results in `Error: index 1 not in 0 .. 0` because the literal parsing function assumes min length 2 when the literal starts with a 0. Fix that incorrect assumption. * Also fix getRadix * Avoid compiler warning * Avoid problem with Nim < 2.2
1 parent 1a2c661 commit 4992f27

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

stint/literals_stint.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Stint
2-
# Copyright 2018 Status Research & Development GmbH
2+
# Copyright 2018-2025 Status Research & Development GmbH
33
# Licensed under either of
44
#
55
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
@@ -77,8 +77,8 @@ make_mixed_types_ops(`xor`, InputType, BothSigned, switchInputs = true)
7777

7878
# Specialization / fast path for comparison to zero
7979
# Note system.nim has templates to transform > and >= into <= and <
80-
template mtoIsZero*{a == 0}(a: StUint or StInt): bool = a.isZero
81-
template mtoIsZero*{0 == a}(a: StUint or StInt): bool = a.isZero
80+
template mtoIsZeroL*{a == 0}(a: StUint or StInt): bool = a.isZero
81+
template mtoIsZeroR*{0 == a}(a: StUint or StInt): bool = a.isZero
8282

8383
template mtoIsNeg*{a < 0}(a: StInt): bool = a.isNegative
8484
template mtoIsNegOrZero*{a <= 0}(a: StInt): bool = a.isZero or a.isNegative

stint/private/custom_literal.nim

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Stint
2-
# Copyright 2018-2023 Status Research & Development GmbH
2+
# Copyright 2018-2025 Status Research & Development GmbH
33
# Licensed under either of
44
#
55
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
@@ -8,24 +8,21 @@
88
# at your option. This file may not be copied, modified, or distributed except according to those terms.
99

1010
func getRadix(s: static string): uint8 {.compileTime.} =
11-
if s.len <= 2:
12-
return 10
11+
when s.len >= 2: # Nim < 2.2: When using `if`: `Error: index 1 not in 0`
12+
if s[0] == '0':
13+
if s[1] == 'b':
14+
return 2
1315

14-
# maybe have prefix have prefix
15-
if s[0] != '0':
16-
return 10
16+
if s[1] == 'o':
17+
return 8
1718

18-
if s[1] == 'b':
19-
return 2
19+
if s[1] == 'x':
20+
return 16
2021

21-
if s[1] == 'o':
22-
return 8
23-
24-
if s[1] == 'x':
25-
return 16
22+
10
2623

2724
func stripPrefix(s: string): string {.compileTime.} =
28-
if s[0] != '0':
25+
if s.len < 2 or s[0] != '0':
2926
return s
3027
if s[1] in {'b', 'o', 'x'}:
3128
return s[2 .. ^1]

tests/test_features.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Copyright 2023 Status Research & Development GmbH
1+
# Stint
2+
# Copyright 2023-2025 Status Research & Development GmbH
23
# Licensed under either of
34
#
45
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
@@ -20,6 +21,7 @@ suite "new features":
2021
b = 0xabcdef0123456'u256
2122
c = -100'i128
2223
d = -50000'i256
24+
e = 0'u256
2325

2426
let
2527
x = 0b111100011'u128
@@ -32,6 +34,7 @@ suite "new features":
3234
b == 0xabcdef0123456.u256
3335
c == -100.i128
3436
d == -50000.i256
37+
e == 0'u256
3538
x == 0b111100011.u128
3639
y == 0o777766666.u256
3740
z == UInt256.fromHex("0x1122334455667788991011121314151617181920aabbccddeeffb1b2b3b4b500")

0 commit comments

Comments
 (0)