all: remove calls to deprecated ioutil package

Fixes produced via semgrep and https://github.com/dgryski/semgrep-go/blob/master/ioutil.yml
Этот коммит содержится в:
Damian Gryski 2022-08-05 12:26:15 -07:00 коммит произвёл Ron Evans
родитель 13f21477b1
коммит edbbca5614
13 изменённых файлов: 34 добавлений и 39 удалений

Просмотреть файл

@ -14,7 +14,6 @@ import (
"fmt" "fmt"
"go/types" "go/types"
"hash/crc32" "hash/crc32"
"io/ioutil"
"math/bits" "math/bits"
"os" "os"
"os/exec" "os/exec"
@ -103,7 +102,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
} }
// Create a temporary directory for intermediary files. // Create a temporary directory for intermediary files.
dir, err := ioutil.TempDir("", "tinygo") dir, err := os.MkdirTemp("", "tinygo")
if err != nil { if err != nil {
return err return err
} }
@ -370,7 +369,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Packages are compiled independently anyway. // Packages are compiled independently anyway.
for _, cgoHeader := range pkg.CGoHeaders { for _, cgoHeader := range pkg.CGoHeaders {
// Store the header text in a temporary file. // Store the header text in a temporary file.
f, err := ioutil.TempFile(dir, "cgosnippet-*.c") f, err := os.CreateTemp(dir, "cgosnippet-*.c")
if err != nil { if err != nil {
return err return err
} }
@ -445,7 +444,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Write to a temporary path that is renamed to the destination // Write to a temporary path that is renamed to the destination
// file to avoid race conditions with other TinyGo invocatiosn // file to avoid race conditions with other TinyGo invocatiosn
// that might also be compiling this package at the same time. // that might also be compiling this package at the same time.
f, err := ioutil.TempFile(filepath.Dir(job.result), filepath.Base(job.result)) f, err := os.CreateTemp(filepath.Dir(job.result), filepath.Base(job.result))
if err != nil { if err != nil {
return err return err
} }
@ -589,7 +588,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
return err return err
} }
defer llvmBuf.Dispose() defer llvmBuf.Dispose()
return ioutil.WriteFile(outpath, llvmBuf.Bytes(), 0666) return os.WriteFile(outpath, llvmBuf.Bytes(), 0666)
case ".bc": case ".bc":
var buf llvm.MemoryBuffer var buf llvm.MemoryBuffer
if config.UseThinLTO() { if config.UseThinLTO() {
@ -598,10 +597,10 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
buf = llvm.WriteBitcodeToMemoryBuffer(mod) buf = llvm.WriteBitcodeToMemoryBuffer(mod)
} }
defer buf.Dispose() defer buf.Dispose()
return ioutil.WriteFile(outpath, buf.Bytes(), 0666) return os.WriteFile(outpath, buf.Bytes(), 0666)
case ".ll": case ".ll":
data := []byte(mod.String()) data := []byte(mod.String())
return ioutil.WriteFile(outpath, data, 0666) return os.WriteFile(outpath, data, 0666)
default: default:
panic("unreachable") panic("unreachable")
} }
@ -629,7 +628,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
} }
} }
defer llvmBuf.Dispose() defer llvmBuf.Dispose()
return ioutil.WriteFile(objfile, llvmBuf.Bytes(), 0666) return os.WriteFile(objfile, llvmBuf.Bytes(), 0666)
}, },
} }

Просмотреть файл

@ -10,7 +10,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -93,7 +92,7 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
// Load dependencies file, if possible. // Load dependencies file, if possible.
depfileName := "dep-" + depfileNameHash + ".json" depfileName := "dep-" + depfileNameHash + ".json"
depfileCachePath := filepath.Join(goenv.Get("GOCACHE"), depfileName) depfileCachePath := filepath.Join(goenv.Get("GOCACHE"), depfileName)
depfileBuf, err := ioutil.ReadFile(depfileCachePath) depfileBuf, err := os.ReadFile(depfileCachePath)
var dependencies []string // sorted list of dependency paths var dependencies []string // sorted list of dependency paths
if err == nil { if err == nil {
// There is a dependency file, that's great! // There is a dependency file, that's great!
@ -117,12 +116,12 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
return "", err return "", err
} }
objTmpFile, err := ioutil.TempFile(goenv.Get("GOCACHE"), "tmp-*"+ext) objTmpFile, err := os.CreateTemp(goenv.Get("GOCACHE"), "tmp-*"+ext)
if err != nil { if err != nil {
return "", err return "", err
} }
objTmpFile.Close() objTmpFile.Close()
depTmpFile, err := ioutil.TempFile(tmpdir, "dep-*.d") depTmpFile, err := os.CreateTemp(tmpdir, "dep-*.d")
if err != nil { if err != nil {
return "", err return "", err
} }
@ -166,7 +165,7 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
sort.Strings(dependencySlice) sort.Strings(dependencySlice)
// Write dependencies file. // Write dependencies file.
f, err := ioutil.TempFile(filepath.Dir(depfileCachePath), depfileName) f, err := os.CreateTemp(filepath.Dir(depfileCachePath), depfileName)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -267,7 +266,7 @@ func hashFile(path string) (string, error) {
// allowed on Windows, but of course can be used on POSIX like systems. Still, // allowed on Windows, but of course can be used on POSIX like systems. Still,
// it's the most sane of any of the formats so readDepFile will use that format. // it's the most sane of any of the formats so readDepFile will use that format.
func readDepFile(filename string) ([]string, error) { func readDepFile(filename string) ([]string, error) {
buf, err := ioutil.ReadFile(filename) buf, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Просмотреть файл

@ -13,7 +13,7 @@ import (
"debug/elf" "debug/elf"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io/ioutil" "os"
"sort" "sort"
"strings" "strings"
) )
@ -189,5 +189,5 @@ func makeESPFirmareImage(infile, outfile, format string) error {
} }
// Write the image to the output file. // Write the image to the output file.
return ioutil.WriteFile(outfile, outf.Bytes(), 0666) return os.WriteFile(outfile, outf.Bytes(), 0666)
} }

Просмотреть файл

@ -1,7 +1,6 @@
package builder package builder
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -94,7 +93,7 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
target := config.Triple() target := config.Triple()
if l.makeHeaders != nil { if l.makeHeaders != nil {
if _, err = os.Stat(headerPath); err != nil { if _, err = os.Stat(headerPath); err != nil {
temporaryHeaderPath, err := ioutil.TempDir(outdir, "include.tmp*") temporaryHeaderPath, err := os.MkdirTemp(outdir, "include.tmp*")
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -189,7 +188,7 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
defer once.Do(unlock) defer once.Do(unlock)
// Create an archive of all object files. // Create an archive of all object files.
f, err := ioutil.TempFile(outdir, "libc.a.tmp*") f, err := os.CreateTemp(outdir, "libc.a.tmp*")
if err != nil { if err != nil {
return err return err
} }
@ -250,7 +249,7 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
run: func(*compileJob) error { run: func(*compileJob) error {
var compileArgs []string var compileArgs []string
compileArgs = append(compileArgs, args...) compileArgs = append(compileArgs, args...)
tmpfile, err := ioutil.TempFile(outdir, "crt1.o.tmp*") tmpfile, err := os.CreateTemp(outdir, "crt1.o.tmp*")
if err != nil { if err != nil {
return err return err
} }

Просмотреть файл

@ -3,7 +3,6 @@ package builder
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -35,7 +34,7 @@ var Musl = Library{
filepath.Join(muslDir, "include", "alltypes.h.in"), filepath.Join(muslDir, "include", "alltypes.h.in"),
} }
for _, infile := range infiles { for _, infile := range infiles {
data, err := ioutil.ReadFile(infile) data, err := os.ReadFile(infile)
if err != nil { if err != nil {
return err return err
} }
@ -63,7 +62,7 @@ var Musl = Library{
if err != nil { if err != nil {
return err return err
} }
data, err := ioutil.ReadFile(filepath.Join(muslDir, "arch", arch, "bits", "syscall.h.in")) data, err := os.ReadFile(filepath.Join(muslDir, "arch", arch, "bits", "syscall.h.in"))
if err != nil { if err != nil {
return err return err
} }

Просмотреть файл

@ -2,7 +2,7 @@ package builder
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"os/exec" "os/exec"
"github.com/tinygo-org/tinygo/compileopts" "github.com/tinygo-org/tinygo/compileopts"
@ -18,7 +18,7 @@ func makeDFUFirmwareImage(options *compileopts.Options, infile, outfile string)
} }
cmd := exec.Command(cmdLine[0], cmdLine[1:]...) cmd := exec.Command(cmdLine[0], cmdLine[1:]...)
cmd.Stdout = ioutil.Discard cmd.Stdout = io.Discard
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
return fmt.Errorf("could not run nrfutil pkg generate: %w", err) return fmt.Errorf("could not run nrfutil pkg generate: %w", err)

Просмотреть файл

@ -2,7 +2,7 @@ package builder
import ( import (
"debug/elf" "debug/elf"
"io/ioutil" "io"
"os" "os"
"sort" "sort"
@ -87,7 +87,7 @@ func extractROM(path string) (uint64, []byte, error) {
// Pad the difference // Pad the difference
rom = append(rom, make([]byte, diff)...) rom = append(rom, make([]byte, diff)...)
} }
data, err := ioutil.ReadAll(prog.Open()) data, err := io.ReadAll(prog.Open())
if err != nil { if err != nil {
return 0, nil, objcopyError{"failed to extract segment from ELF file: " + path, err} return 0, nil, objcopyError{"failed to extract segment from ELF file: " + path, err}
} }

Просмотреть файл

@ -10,7 +10,7 @@ package builder
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"io/ioutil" "os"
"strconv" "strconv"
) )
@ -26,7 +26,7 @@ func convertELFFileToUF2File(infile, outfile string, uf2FamilyID string) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(outfile, output, 0644) return os.WriteFile(outfile, output, 0644)
} }
// convertBinToUF2 converts the binary bytes in input to UF2 formatted data. // convertBinToUF2 converts the binary bytes in input to UF2 formatted data.

Просмотреть файл

@ -4,7 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -54,10 +54,10 @@ func GetGorootVersion(goroot string) (major, minor int, err error) {
// toolchain for the given GOROOT path. It is usually of the form `go1.x.y` but // toolchain for the given GOROOT path. It is usually of the form `go1.x.y` but
// can have some variations (for beta releases, for example). // can have some variations (for beta releases, for example).
func GorootVersionString(goroot string) (string, error) { func GorootVersionString(goroot string) (string, error) {
if data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION")); err == nil { if data, err := os.ReadFile(filepath.Join(goroot, "VERSION")); err == nil {
return string(data), nil return string(data), nil
} else if data, err := ioutil.ReadFile(filepath.Join( } else if data, err := os.ReadFile(filepath.Join(
goroot, "src", "internal", "buildcfg", "zbootstrap.go")); err == nil { goroot, "src", "internal", "buildcfg", "zbootstrap.go")); err == nil {
r := regexp.MustCompile("const version = `(.*)`") r := regexp.MustCompile("const version = `(.*)`")

Просмотреть файл

@ -83,7 +83,7 @@ func GetCachedGoroot(config *compileopts.Config) (string, error) {
} }
// Create a temporary directory to construct the goroot within. // Create a temporary directory to construct the goroot within.
tmpgoroot, err := ioutil.TempDir(goenv.Get("GOCACHE"), cachedGorootName+".tmp") tmpgoroot, err := os.MkdirTemp(goenv.Get("GOCACHE"), cachedGorootName+".tmp")
if err != nil { if err != nil {
return "", err return "", err
} }

Просмотреть файл

@ -13,7 +13,6 @@ import (
"go/token" "go/token"
"go/types" "go/types"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -335,7 +334,7 @@ func (p *Package) OriginalDir() string {
// parseFile is a wrapper around parser.ParseFile. // parseFile is a wrapper around parser.ParseFile.
func (p *Package) parseFile(path string, mode parser.Mode) (*ast.File, error) { func (p *Package) parseFile(path string, mode parser.Mode) (*ast.File, error) {
originalPath := p.program.getOriginalPath(path) originalPath := p.program.getOriginalPath(path)
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Просмотреть файл

@ -251,7 +251,7 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
} }
// create a new temp directory just for this run, announce it to os.TempDir() via TMPDIR // create a new temp directory just for this run, announce it to os.TempDir() via TMPDIR
tmpdir, err := ioutil.TempDir("", "tinygotmp") tmpdir, err := os.MkdirTemp("", "tinygotmp")
if err != nil { if err != nil {
return fmt.Errorf("failed to create temporary directory: %w", err) return fmt.Errorf("failed to create temporary directory: %w", err)
} }
@ -1467,7 +1467,7 @@ func main() {
fmt.Fprintf(os.Stderr, "Unknown library: %s\n", name) fmt.Fprintf(os.Stderr, "Unknown library: %s\n", name)
os.Exit(1) os.Exit(1)
} }
tmpdir, err := ioutil.TempDir("", "tinygo*") tmpdir, err := os.MkdirTemp("", "tinygo*")
if err != nil { if err != nil {
handleCompilerError(err) handleCompilerError(err)
} }

4
testdata/filesystem.go предоставленный
Просмотреть файл

@ -1,7 +1,7 @@
package main package main
import ( import (
"io/ioutil" "io"
"os" "os"
) )
@ -28,7 +28,7 @@ func main() {
} }
}() }()
data, err := ioutil.ReadAll(f) data, err := io.ReadAll(f)
if err != nil { if err != nil {
panic(err) panic(err)
} }