Skip to content

Commit 246c93b

Browse files
authored
Bugfix: String indexing in big_str macro (#57621)
Make sure that non-ASCII in the big string macro correctly throws an ArgumentError instead of a string indexing error.
1 parent 0d363a8 commit 246c93b

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

base/int.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,14 @@ macro big_str(s)
699699
message = "invalid number format $s for BigInt or BigFloat"
700700
throw_error = :(throw(ArgumentError($message)))
701701
if '_' in s
702-
# remove _ in s[2:end-1]
703-
bf = IOBuffer(maxsize=lastindex(s))
702+
# remove _ in s[2:end-1].
703+
# Do not allow '_' right before or after dot.
704+
bf = IOBuffer(sizehint=ncodeunits(s))
704705
c = s[1]
705706
print(bf, c)
706707
is_prev_underscore = (c == '_')
707708
is_prev_dot = (c == '.')
708-
for c in SubString(s, 2, lastindex(s)-1)
709+
for c in SubString(s, nextind(s, 1), prevind(s, lastindex(s)))
709710
c != '_' && print(bf, c)
710711
c == '_' && is_prev_dot && return throw_error
711712
c == '.' && is_prev_underscore && return throw_error

test/int.jl

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ end
118118
@test big"1.0" == BigFloat(1.0)
119119
@test_throws ArgumentError big"1.0.3"
120120
@test_throws ArgumentError big"pi"
121+
122+
@test_throws ArgumentError big"_æ1"
123+
@test_throws ArgumentError big"æ_1"
124+
@test_throws ArgumentError big"_ææ"
121125
end
122126

123127
@test round(UInt8, 123) == 123

0 commit comments

Comments
 (0)