Skip to content

Commit 39ad208

Browse files
committed
test: add test to verify that string copies don't get optimized away
Fixes #25834 Change-Id: I33e58dabfd04b84dfee1a9a3796796b5d19862e7 Reviewed-on: https://go-review.googlesource.com/118295 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 48987ba commit 39ad208

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/strcopy.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run
2+
3+
// Copyright 2018 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Test that string([]byte(string)) makes a copy and doesn't reduce to
8+
// nothing. (Issue 25834)
9+
10+
package main
11+
12+
import (
13+
"reflect"
14+
"unsafe"
15+
)
16+
17+
func main() {
18+
var (
19+
buf = make([]byte, 2<<10)
20+
large = string(buf)
21+
sub = large[10:12]
22+
subcopy = string([]byte(sub))
23+
subh = *(*reflect.StringHeader)(unsafe.Pointer(&sub))
24+
subcopyh = *(*reflect.StringHeader)(unsafe.Pointer(&subcopy))
25+
)
26+
if subh.Data == subcopyh.Data {
27+
panic("sub and subcopy have the same underlying array")
28+
}
29+
}

0 commit comments

Comments
 (0)