diff options
author | Ayke van Laethem <[email protected]> | 2021-09-15 15:31:56 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2021-09-15 19:04:26 +0200 |
commit | c7c874a487dc0ceb2b7c7c2ba19b31bc4809cb93 (patch) | |
tree | c272c35a76dcae21d7629b7ea5f583a0d4d72ca8 /loader | |
parent | c528a168ee713b7a422643365a58e07be0850c45 (diff) | |
download | tinygo-c7c874a487dc0ceb2b7c7c2ba19b31bc4809cb93.tar.gz tinygo-c7c874a487dc0ceb2b7c7c2ba19b31bc4809cb93.zip |
loader: fix panic in CGo files with syntax errors
If all of the Go files presented to the compiler have syntax errors,
cgo.Process gets an empty files slice and will panic:
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/tinygo-org/tinygo/cgo.Process({0x0, 0x4e8e36, 0x0}, {0xc000024104, 0x18}, 0xc000090fc0, {0xc000899780, 0x7, 0xc00018ce68})
/home/ayke/src/github.com/tinygo-org/tinygo/cgo/cgo.go:186 +0x22ee
github.com/tinygo-org/tinygo/loader.(*Package).parseFiles(0xc0001ccf00)
/home/ayke/src/github.com/tinygo-org/tinygo/loader/loader.go:400 +0x51e
This is simple to work around: just don't try to run CGo when there are
no files to process. It just means there are bugs to fix before CGo can
properly run.
(This is perhaps not the nicest solution but certainly the simplest).
Diffstat (limited to 'loader')
-rw-r--r-- | loader/loader.go | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/loader/loader.go b/loader/loader.go index eed57e6d6..a201b1526 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -386,7 +386,11 @@ func (p *Package) parseFiles() ([]*ast.File, error) { } // Do CGo processing. - if len(p.CgoFiles) != 0 { + // This is done when there are any CgoFiles at all. In that case, len(files) + // should be non-zero. However, if len(GoFiles) == 0 and len(CgoFiles) == 1 + // and there is a syntax error in a CGo file, len(files) may be 0. Don't try + // to call cgo.Process in that case as it will only cause issues. + if len(p.CgoFiles) != 0 && len(files) != 0 { var initialCFlags []string initialCFlags = append(initialCFlags, p.program.config.CFlags()...) initialCFlags = append(initialCFlags, "-I"+p.Dir) |