Skip to content

Commit 2d0e7cd

Browse files
committed
core: Don't allow radix 1 in uint::to_str
1 parent 6e00852 commit 2d0e7cd

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/libcore/uint-template.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ fn from_str_radix(buf: str, radix: u64) -> option<u64> {
9595
};
9696
}
9797

98-
#[doc = "Convert to a string in a given base"]
98+
#[doc = "
99+
Convert to a string in a given base
100+
101+
# Failure
102+
103+
Fails if `radix` < 2 or `radix` > 16
104+
"]
99105
fn to_str(num: T, radix: uint) -> str {
100-
assert (0u < radix && radix <= 16u);
106+
assert (1u < radix && radix <= 16u);
101107
let mut n = num;
102108
let radix = radix as T;
103109
fn digit(n: T) -> u8 {
@@ -167,3 +173,17 @@ fn test_parse_buf() {
167173
assert parse_buf(str::bytes("Z"), 10u) == none;
168174
assert parse_buf(str::bytes("_"), 2u) == none;
169175
}
176+
177+
#[test]
178+
#[should_fail]
179+
#[ignore(cfg(target_os = "win32"))]
180+
fn to_str_radix1() {
181+
uint::to_str(100u, 1u);
182+
}
183+
184+
#[test]
185+
#[should_fail]
186+
#[ignore(cfg(target_os = "win32"))]
187+
fn to_str_radix17() {
188+
uint::to_str(100u, 17u);
189+
}

0 commit comments

Comments
 (0)