Skip to content

Commit ae95fcc

Browse files
vtjnashKristofferC
authored andcommitted
avoid corrupting String on conversion of StringVector to String (#39726)
fix #39717 (cherry picked from commit 0926ed8)
1 parent fe155d8 commit ae95fcc

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/array.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ JL_DLLEXPORT jl_array_t *jl_pchar_to_array(const char *str, size_t len)
483483
JL_DLLEXPORT jl_value_t *jl_array_to_string(jl_array_t *a)
484484
{
485485
size_t len = jl_array_len(a);
486+
if (len == 0) {
487+
// this may seem like purely an optimization (which it also is), but it
488+
// also ensures that calling `String(a)` doesn't corrupt a previous
489+
// string also created the same way, where `a = StringVector(_)`.
490+
return jl_an_empty_string;
491+
}
486492
if (a->flags.how == 3 && a->offset == 0 && a->elsize == 1 &&
487493
(jl_array_ndims(a) != 1 ||
488494
((a->maxsize + sizeof(void*) + 1 <= GC_MAX_SZCLASS) == (len + sizeof(void*) + 1 <= GC_MAX_SZCLASS)))) {

test/core.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5298,6 +5298,16 @@ if Sys.WORD_SIZE == 64
52985298
@test_nowarn tester20360()
52995299
end
53005300

5301+
# issue #39717
5302+
let a = Base.StringVector(2^17)
5303+
b = String(a)
5304+
c = String(a)
5305+
GC.gc()
5306+
@test sizeof(a) == 0
5307+
@test sizeof(b) == 2^17
5308+
@test sizeof(c) == 0
5309+
end
5310+
53015311
@test_throws ArgumentError eltype(Bottom)
53025312

53035313
# issue #16424, re-evaluating type definitions

0 commit comments

Comments
 (0)