Fix wrong int type casting with radix and larger base value

Fix base value check logic when string have regex

Fixed: #1408
This commit is contained in:
Sang-Heon Jeon
2019-09-26 19:23:49 +09:00
committed by lntuition
parent 51c3f71eed
commit d82fee7afa
2 changed files with 13 additions and 5 deletions

View File

@@ -123,6 +123,11 @@ with assert_raises(ValueError):
with assert_raises(ValueError):
int(b"F\xc3\xb8\xc3\xb6\xbbB\xc3\xa5r")
# string looks like radix
assert int('0b1', base=12) == 133
assert int('0o1', base=25) == 601
assert int('0x1', base=34) == 1123
# underscore
assert int('0xFF_FF_FF', base=16) == 16_777_215
with assert_raises(ValueError):

View File

@@ -792,13 +792,16 @@ fn str_to_int(vm: &VirtualMachine, literal: &str, base: &BigInt) -> PyResult<Big
// try to find base
if let Some(radix_candidate) = radix_candidate {
if let Some(matched_radix) = detect_base(&radix_candidate) {
if base_u32 != 0 && base_u32 != matched_radix {
return Err(invalid_literal(vm, literal, base));
} else {
if base_u32 == 0 || base_u32 == matched_radix {
base_u32 = matched_radix;
}
buf.drain(radix_range);
buf.drain(radix_range);
} else if (matched_radix == 2 && base_u32 < 12)
|| (matched_radix == 8 && base_u32 < 25)
|| (matched_radix == 16 && base_u32 < 34)
{
return Err(invalid_literal(vm, literal, base));
}
}
}