Debugging Invalid Memory Address

-2

I'm building a simple Go application, but when I execute my program on Windows 10, I get this error: Update: The full stacktrace is now included.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x478edd]

goroutine 1 [running]:
github.com/UpperCenter/Amalthea/src/files.(*Files).ScanToEncrypt.func1(0xc000162aa0, 0x4c, 0x4caaa0, 0xc000167b20, 0x0, 0x0, 0x4c, 0x2)
        /home/uppercenter/Amalthea/src/files/files.go:48 +0x13d
path/filepath.walk(0xc000162aa0, 0x4c, 0x4caaa0, 0xc000167b20, 0xc000221d18, 0x0, 0x0)
        /usr/lib/go/src/path/filepath/path.go:360 +0x443
path/filepath.walk(0xc000560e10, 0x30, 0x4caaa0, 0xc000167500, 0xc000221d18, 0x0, 0x0)
        /usr/lib/go/src/path/filepath/path.go:384 +0x31a
path/filepath.walk(0xc0001719b0, 0x24, 0x4caaa0, 0xc000191650, 0xc000221d18, 0x0, 0x0)
        /usr/lib/go/src/path/filepath/path.go:384 +0x31a
path/filepath.walk(0xc000011720, 0x1a, 0x4caaa0, 0xc000180000, 0xc000221d18, 0x0, 0x0)
        /usr/lib/go/src/path/filepath/path.go:384 +0x31a
path/filepath.walk(0xc0000116a0, 0x14, 0x4caaa0, 0xc000175dc0, 0xc000221d18, 0x0, 0x0)
        /usr/lib/go/src/path/filepath/path.go:384 +0x31a
path/filepath.walk(0xc00000acc0, 0xc, 0x4caaa0, 0xc000174af0, 0xc000221d18, 0x0, 0x0)
        /usr/lib/go/src/path/filepath/path.go:384 +0x31a
path/filepath.walk(0x4a4206, 0x9, 0x4caaa0, 0xc00010a000, 0xc000221d18, 0x0, 0x457a58)
        /usr/lib/go/src/path/filepath/path.go:384 +0x31a
path/filepath.Walk(0x4a4206, 0x9, 0xc000077d18, 0x280, 0x26c)
        /usr/lib/go/src/path/filepath/path.go:406 +0x10c
github.com/UpperCenter/Amalthea/src/files.(*Files).ScanToEncrypt(0xc000077f48, 0xc000006018, 0xc000077df8, 0x1, 0x1, 0x26c)
        /home/uppercenter/Amalthea/src/files/files.go:40 +0x86
main.main()
        /home/uppercenter/Amalthea/main.go:63 +0x193

I read that this has happened because I have an error within ScanToEncrypt that isn't being handled.

Here's the ScanToEncrypt function:

// ScanToEncrypt scans all valid files to encrypt
func (f *Files) ScanToEncrypt() ([]string, error) {
    // Store encryptable files as array.
    var files []string
    // Begin "walking" from `rootDir` to get all available folders & subfolders
    err := filepath.Walk(f.rootDir, func(path string, info os.FileInfo, err error) error {
        // Returns info describing a named file.
        stat, _ := os.Stat(path)
        // HasSuffix looks for encoded files.
        if !strings.HasSuffix(path, ".AmaltheaEnc") {
            if !stat.IsDir() {
                // Checks the file is below maximum file size in bytes defined by `size`
                if stat.Size() <= int64(f.size) {
                    for _, ext := range f.exts {
                        if strings.Contains(path, "."+ext) {
                            // Append file & file path to `files`
                            files = append(files, path)
                            break
                        }
                    }
                }
            }
        }
        return nil
    })
    // Return `files` array to use for encryption
    return files, err
}

I thought I was handling errors with return files, err but I guess not. Can anyone help me better understand what I should do here?

windows
go
encryption
asked on Stack Overflow Jan 11, 2021 by UpperCenter • edited Jan 11, 2021 by Flimzy

1 Answer

2

Don't throw away potential error here stat, _ := os.Stat(path). You should handle it, for example, by returning the error if it's not null, or printing an error message

answered on Stack Overflow Jan 11, 2021 by aureliar

User contributions licensed under CC BY-SA 3.0