Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsigned wrap around in lpad/rpad/string allocation #32161

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function lpad(
n::Integer,
p::Union{AbstractChar,AbstractString}=' ',
) :: String
m = n - length(s)
m = signed(n) - length(s)
m ≤ 0 && return string(s)
l = length(p)
q, r = divrem(m, l)
Expand All @@ -270,7 +270,7 @@ function rpad(
n::Integer,
p::Union{AbstractChar,AbstractString}=' ',
) :: String
m = n - length(s)
m = signed(n) - length(s)
m ≤ 0 && return string(s)
l = length(p)
q, r = divrem(m, l)
Expand Down
4 changes: 4 additions & 0 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ JL_DLLEXPORT jl_value_t *jl_array_to_string(jl_array_t *a)
JL_DLLEXPORT jl_value_t *jl_pchar_to_string(const char *str, size_t len)
{
size_t sz = sizeof(size_t) + len + 1; // add space for trailing \nul protector and size
if (sz < len) // overflow
jl_throw(jl_memory_exception);
if (len == 0)
return jl_an_empty_string;
jl_value_t *s = jl_gc_alloc_(jl_get_ptls_states(), sz, jl_string_type); // force inlining
Expand All @@ -483,6 +485,8 @@ JL_DLLEXPORT jl_value_t *jl_pchar_to_string(const char *str, size_t len)
JL_DLLEXPORT jl_value_t *jl_alloc_string(size_t len)
{
size_t sz = sizeof(size_t) + len + 1; // add space for trailing \nul protector and size
if (sz < len) // overflow
jl_throw(jl_memory_exception);
if (len == 0)
return jl_an_empty_string;
jl_value_t *s = jl_gc_alloc_(jl_get_ptls_states(), sz, jl_string_type); // force inlining
Expand Down
2 changes: 2 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ end
@test repeat(s, 3) === S
@test repeat(S, 3) === S*S*S
end
# Issue #32160 (string allocation unsigned overflow)
@test_throws OutOfMemoryError repeat('x', typemax(Csize_t))
end
@testset "issue #12495: check that logical indexing attempt raises ArgumentError" begin
@test_throws ArgumentError "abc"[[true, false, true]]
Expand Down
3 changes: 3 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
@test rpad("αβ", 8, "¹₂³") == "αβ¹₂³¹₂³"
@test lpad("αβ", 9, "¹₂³") == "¹₂³¹₂³¹αβ"
@test rpad("αβ", 9, "¹₂³") == "αβ¹₂³¹₂³¹"
# Issue #32160 (unsigned underflow in lpad/rpad)
@test lpad("xx", UInt(1), " ") == "xx"
@test rpad("xx", UInt(1), " ") == "xx"
end

# string manipulation
Expand Down