Corrupted gzip make my program crash

Hello Gophers!
In my program, I read a compressed tar.gz archive: works fine if the archive is valid but, despire all the error handling possibile, the gzip library make my program crash if the archive is corrupted.

What am I doing wrong?

Here is the stack trace:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x68 pc=0x47fc62]

goroutine 1 [running]:
panic(0x5d02c0, 0xc4200120c0)
	/usr/lib/go-1.7/src/runtime/panic.go:500 +0x1a1
compress/gzip.(*Reader).Close(0x0, 0x6d28a0, 0xc420012140)
	/usr/lib/go-1.7/src/compress/gzip/gunzip.go:287 +0x22
myutils.GetFilesFromArchive(0xc420fc5d60, 0x45, 0x0, 0x6d28a0, 0xc420012140)
	/home/gopher/.go/src/myutils/myutils.go:362 +0x480
myutils.ExtractArchive(0xc42006e990, 0x22, 0xc420fc5d60, 0x45, 0x603076, 0x4, 0x0, 0x0, 0x0)
	/home/gopher/.go/src/myutils/myutils.go:180 +0x41d
main.installPackage(0x60e2de, 0x49, 0xc42006e990, 0x22, 0x0, 0x0, 0x0)
	/home/gopher/.go/src/non_comm/non_comm.go:166 +0x70d
main.main()
	/home/gopher/.go/src/non_comm/non_comm.go:108 +0x302

here are the calls mentioned in the stack trace:

func GetFilesFromArchive(tarFile string) (*[]string, error) {

	// open file
	gz, err := os.Open(tarFile)
	defer gz.Close()
	if err != nil {
		return nil, err
	}

	gzr, err := gzip.NewReader(gz)
	defer gzr.Close()
	if err != nil {
		return nil, err // <==== this is myutils.go:362 
	}
func ExtractArchive(destDir, tarFile, ext string) (*ExtractionResult, error) {

	// ...

	filesInArchive, err := GetFilesFromArchive(tarFile)
	if err != nil {
		return nil, fmt.Errorf("error while reading the archive: %s", err) // <==== this is myutils.go:180
	}

Any ideas?

gzr is nil and you close it. defer only after checking error return values.

Similar is true in other places of the code you have shown, but in the panic exactly this part was mentioned.

2 Likes

thanks a lot Norbert! It makes totally sense, but somehow I didn’t noticed it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.