Skip to content
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
6 changes: 6 additions & 0 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ JL_DLLEXPORT jl_array_t *jl_pchar_to_array(const char *str, size_t len)
JL_DLLEXPORT jl_value_t *jl_array_to_string(jl_array_t *a)
{
size_t len = jl_array_len(a);
if (len == 0) {
// this may seem like purely an optimization (which it also is), but it
// also ensures that calling `String(a)` doesn't corrupt a previous
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, but why/how exactly does a previous string get corrupted w/o this? The segfault seems to be gc-related; something about the original string now being "shared" between two strings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We truncate the array here (so that future uses will get a fresh data object), but that ends up truncating the string later, if it is still the data pointer and we call this immediately. See the added tests.

// string also created the same way, where `a = StringVector(_)`.
return jl_an_empty_string;
}
if (a->flags.how == 3 && a->offset == 0 && a->elsize == 1 &&
(jl_array_ndims(a) != 1 ||
((a->maxsize + sizeof(void*) + 1 <= GC_MAX_SZCLASS) == (len + sizeof(void*) + 1 <= GC_MAX_SZCLASS)))) {
Expand Down
10 changes: 10 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5299,6 +5299,16 @@ if Sys.WORD_SIZE == 64
@test_nowarn tester20360()
end

# issue #39717
let a = Base.StringVector(2^17)
b = String(a)
c = String(a)
GC.gc()
@test sizeof(a) == 0
@test sizeof(b) == 2^17
@test sizeof(c) == 0
end

@test_throws ArgumentError eltype(Bottom)

# issue #16424, re-evaluating type definitions
Expand Down