From 267a8d1c839c85e2fc5547eeb52ba7b901035e81 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 18 Feb 2021 00:24:36 -0500 Subject: [PATCH] avoid corrupting String on conversion of StringVector to String fix #39717 --- src/array.c | 6 ++++++ test/core.jl | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/array.c b/src/array.c index d311e5b152d06..20c6cf7706880 100644 --- a/src/array.c +++ b/src/array.c @@ -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 + // 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)))) { diff --git a/test/core.jl b/test/core.jl index f1f11d40af4c7..a159aeccf1d55 100644 --- a/test/core.jl +++ b/test/core.jl @@ -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