Skip to content

Instantly share code, notes, and snippets.

@azhuox

azhuox/block7.go Secret

Created December 1, 2020 04:35
Show Gist options
  • Save azhuox/a67a6e1cea706a787e017f0786479e13 to your computer and use it in GitHub Desktop.
Save azhuox/a67a6e1cea706a787e017f0786479e13 to your computer and use it in GitHub Desktop.
package main
import "io"
import "bytes"
import "fmt"
func main() {
var w io.Writer
fmt.Println(w == nil) // True
var b *bytes.Buffer
w = b
fmt.Println(w == nil) // false
write(w) // panic: runtime error: invalid memory address or nil pointer dereference
}
// If out is non-nil, output will be written to it.
//
func write(out io.Writer) {
// ...do something...
if out != nil { // This guard is not secure enough
out.Write([]byte("done!\n"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment