mirror of
https://github.com/golang/go
synced 2024-11-15 00:20:30 -07:00
319b75ed33
Fixes #58141 Co-authored-by: Richard Musiol <neelance@gmail.com> Co-authored-by: Achille Roussel <achille.roussel@gmail.com> Co-authored-by: Julien Fabre <ju.pryz@gmail.com> Co-authored-by: Evan Phoenix <evan@phx.io> Change-Id: I49b66946acc90fdf09ed9223096bfec9a1e5b923 Reviewed-on: https://go-review.googlesource.com/c/go/+/479627 Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Bypass: Ian Lance Taylor <iant@golang.org>
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
// run
|
|
|
|
// Copyright 2021 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 that the compiler refuses excessively long constants.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// testProg creates a package called name, with path dir/name.go,
|
|
// which declares an untyped constant of the given length.
|
|
// testProg compiles this package and checks for the absence or
|
|
// presence of a constant literal error.
|
|
func testProg(dir, name string, length int, msg string) {
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf,
|
|
"package %s; const _ = 0b%s // %d bits",
|
|
name, strings.Repeat("1", length), length,
|
|
)
|
|
|
|
filename := filepath.Join(dir, fmt.Sprintf("%s.go", name))
|
|
if err := os.WriteFile(filename, buf.Bytes(), 0666); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
cmd := exec.Command("go", "tool", "compile", "-p=p", filename)
|
|
cmd.Dir = dir
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if msg == "" {
|
|
// no error expected
|
|
if err != nil {
|
|
log.Fatalf("%s: compile failed unexpectedly: %v", name, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// error expected
|
|
if err == nil {
|
|
log.Fatalf("%s: compile succeeded unexpectedly", name)
|
|
}
|
|
if !bytes.Contains(output, []byte(msg)) {
|
|
log.Fatalf("%s: wrong compiler error message:\n%s\n", name, output)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
if runtime.GOOS == "js" || runtime.GOOS == "wasip1" || runtime.Compiler != "gc" {
|
|
return
|
|
}
|
|
|
|
dir, err := ioutil.TempDir("", "const7_")
|
|
if err != nil {
|
|
log.Fatalf("creating temp dir: %v\n", err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
|
|
const bitLimit = 512
|
|
const charLimit = 10000 // compiler-internal constant length limit
|
|
testProg(dir, "x1", bitLimit, "")
|
|
testProg(dir, "x2", bitLimit+1, "constant overflow")
|
|
testProg(dir, "x3", charLimit-2, "constant overflow") // -2 because literal contains 0b prefix
|
|
testProg(dir, "x4", charLimit-1, "excessively long constant")
|
|
}
|