]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.10] cmd/compile: fix evaluation of "" < s
authorJosh Bleecher Snyder <josharian@gmail.com>
Thu, 12 Apr 2018 15:23:00 +0000 (08:23 -0700)
committerAndrew Bonventre <andybons@golang.org>
Fri, 27 Apr 2018 20:52:26 +0000 (20:52 +0000)
Fixes #24935

Change-Id: Ifa79ab3dfe69297eeef85f7193cd5f85e5982bc5
Reviewed-on: https://go-review.googlesource.com/106655
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-on: https://go-review.googlesource.com/108944
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Andrew Bonventre <andybons@golang.org>

src/cmd/compile/internal/gc/walk.go
test/fixedbugs/issue24817.go [new file with mode: 0644]

index f48513dc73d1efef0c0e58f61a0d0d4d96773239..ca3025bbaab34046a39c86e3fff167a0afddeeb4 100644 (file)
@@ -1261,6 +1261,13 @@ opswitch:
                }
                if cs != nil {
                        cmp := Op(n.Etype)
+                       // Our comparison below assumes that the non-constant string
+                       // is on the left hand side, so rewrite "" cmp x to x cmp "".
+                       // See issue 24817.
+                       if Isconst(n.Left, CTSTR) {
+                               cmp = brrev(cmp)
+                       }
+
                        // maxRewriteLen was chosen empirically.
                        // It is the value that minimizes cmd/go file size
                        // across most architectures.
diff --git a/test/fixedbugs/issue24817.go b/test/fixedbugs/issue24817.go
new file mode 100644 (file)
index 0000000..ba2a138
--- /dev/null
@@ -0,0 +1,64 @@
+// run
+
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Check all ways to compare a non-constant string to the empty string.
+
+package main
+
+import (
+       "fmt"
+       "os"
+)
+
+var (
+       s      = "abc"
+       e      = ""
+       failed bool
+)
+
+func main() {
+       want(true, "" < s, `"" < s`)
+       want(false, s < "", `s < ""`)
+       want(false, "" < e, `"" < e`)
+       want(false, e < "", `e < ""`)
+
+       want(true, "" <= s, `"" <= s`)
+       want(false, s <= "", `s <= ""`)
+       want(true, "" <= e, `"" <= e`)
+       want(true, e <= "", `e <= ""`)
+
+       want(false, "" > s, `"" > s`)
+       want(true, s > "", `s > ""`)
+       want(false, "" > e, `"" > e`)
+       want(false, e > "", `e > ""`)
+
+       want(false, "" >= s, `"" >= s`)
+       want(true, s >= "", `s >= ""`)
+       want(true, "" >= e, `"" >= e`)
+       want(true, e >= "", `e >= ""`)
+
+       want(false, "" == s, `"" == s`)
+       want(false, s == "", `s == ""`)
+       want(true, "" == e, `"" == e`)
+       want(true, e == "", `e == ""`)
+
+       want(true, "" != s, `"" != s`)
+       want(true, s != "", `s != ""`)
+       want(false, "" != e, `"" != e`)
+       want(false, e != "", `e != ""`)
+
+       if failed {
+               os.Exit(1)
+       }
+}
+
+//go:noinline
+func want(b bool, have bool, msg string) {
+       if b != have {
+               fmt.Println(msg)
+               failed = true
+       }
+}