Skip to content

Commit 93cfaa0

Browse files
josharianandybons
authored andcommitted
[release-branch.go1.9] cmd/compile: fix evaluation of "" < s
Fixes #24934 Change-Id: Ifa79ab3dfe69297eeef85f7193cd5f85e5982bc5 Reviewed-on: https://go-review.googlesource.com/106655 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-on: https://go-review.googlesource.com/108943 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]>
1 parent f69b0c6 commit 93cfaa0

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/cmd/compile/internal/gc/walk.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,13 @@ opswitch:
12721272
}
12731273
if cs != nil {
12741274
cmp := Op(n.Etype)
1275+
// Our comparison below assumes that the non-constant string
1276+
// is on the left hand side, so rewrite "" cmp x to x cmp "".
1277+
// See issue 24817.
1278+
if Isconst(n.Left, CTSTR) {
1279+
cmp = brrev(cmp)
1280+
}
1281+
12751282
// maxRewriteLen was chosen empirically.
12761283
// It is the value that minimizes cmd/go file size
12771284
// across most architectures.

test/fixedbugs/issue24817.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// Check all ways to compare a non-constant string to the empty string.
8+
9+
package main
10+
11+
import (
12+
"fmt"
13+
"os"
14+
)
15+
16+
var (
17+
s = "abc"
18+
e = ""
19+
failed bool
20+
)
21+
22+
func main() {
23+
want(true, "" < s, `"" < s`)
24+
want(false, s < "", `s < ""`)
25+
want(false, "" < e, `"" < e`)
26+
want(false, e < "", `e < ""`)
27+
28+
want(true, "" <= s, `"" <= s`)
29+
want(false, s <= "", `s <= ""`)
30+
want(true, "" <= e, `"" <= e`)
31+
want(true, e <= "", `e <= ""`)
32+
33+
want(false, "" > s, `"" > s`)
34+
want(true, s > "", `s > ""`)
35+
want(false, "" > e, `"" > e`)
36+
want(false, e > "", `e > ""`)
37+
38+
want(false, "" >= s, `"" >= s`)
39+
want(true, s >= "", `s >= ""`)
40+
want(true, "" >= e, `"" >= e`)
41+
want(true, e >= "", `e >= ""`)
42+
43+
want(false, "" == s, `"" == s`)
44+
want(false, s == "", `s == ""`)
45+
want(true, "" == e, `"" == e`)
46+
want(true, e == "", `e == ""`)
47+
48+
want(true, "" != s, `"" != s`)
49+
want(true, s != "", `s != ""`)
50+
want(false, "" != e, `"" != e`)
51+
want(false, e != "", `e != ""`)
52+
53+
if failed {
54+
os.Exit(1)
55+
}
56+
}
57+
58+
//go:noinline
59+
func want(b bool, have bool, msg string) {
60+
if b != have {
61+
fmt.Println(msg)
62+
failed = true
63+
}
64+
}

0 commit comments

Comments
 (0)