mirror of
https://github.com/securego/gosec.git
synced 2025-03-19 21:08:30 +02:00
Split the code in `source.go` to individual sample files, one per rule. This will help contributors submit samples for new rules, or improvements to existing rules. The cgo sample was all that was left after refactoring, which resulted in its own sample file. Sample code was also formatted to have some level of consistency. Each sample go "file" attempts to keep the formatting of `gofmt`, and each code sample is in its own section in the sample file. Signed-off-by: Adam Kaplan <adam@adambkaplan.com>
68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package testutils
|
|
|
|
import "github.com/securego/gosec/v2"
|
|
|
|
var (
|
|
// SampleCodeG103 find instances of unsafe blocks for auditing purposes
|
|
SampleCodeG103 = []CodeSample{
|
|
{[]string{`
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
)
|
|
|
|
type Fake struct{}
|
|
|
|
func (Fake) Good() {}
|
|
|
|
func main() {
|
|
unsafeM := Fake{}
|
|
unsafeM.Good()
|
|
intArray := [...]int{1, 2}
|
|
fmt.Printf("\nintArray: %v\n", intArray)
|
|
intPtr := &intArray[0]
|
|
fmt.Printf("\nintPtr=%p, *intPtr=%d.\n", intPtr, *intPtr)
|
|
addressHolder := uintptr(unsafe.Pointer(intPtr))
|
|
intPtr = (*int)(unsafe.Pointer(addressHolder))
|
|
fmt.Printf("\nintPtr=%p, *intPtr=%d.\n\n", intPtr, *intPtr)
|
|
}
|
|
`}, 2, gosec.NewConfig()},
|
|
{[]string{`
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
)
|
|
|
|
func main() {
|
|
chars := [...]byte{1, 2}
|
|
charsPtr := &chars[0]
|
|
str := unsafe.String(charsPtr, len(chars))
|
|
fmt.Printf("%s\n", str)
|
|
ptr := unsafe.StringData(str)
|
|
fmt.Printf("ptr: %p\n", ptr)
|
|
}
|
|
`}, 2, gosec.NewConfig()},
|
|
{[]string{`
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
)
|
|
|
|
func main() {
|
|
chars := [...]byte{1, 2}
|
|
charsPtr := &chars[0]
|
|
slice := unsafe.Slice(charsPtr, len(chars))
|
|
fmt.Printf("%v\n", slice)
|
|
ptr := unsafe.SliceData(slice)
|
|
fmt.Printf("ptr: %p\n", ptr)
|
|
}
|
|
`}, 2, gosec.NewConfig()},
|
|
}
|
|
)
|