mirror of
https://github.com/securego/gosec.git
synced 2025-11-25 22:22:17 +02:00
chore: Refactor Sample Code to Separate Files
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>
This commit is contained in:
committed by
Cosmin Cojocar
parent
bc03d1c1bc
commit
0e2a61899a
52
testutils/cgo_samples.go
Normal file
52
testutils/cgo_samples.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package testutils
|
||||
|
||||
import "github.com/securego/gosec/v2"
|
||||
|
||||
var (
|
||||
// SampleCodeCgo - Cgo file sample
|
||||
SampleCodeCgo = []CodeSample{
|
||||
{[]string{`
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int printData(unsigned char *data) {
|
||||
return printf("cData: %lu \"%s\"\n", (long unsigned int)strlen(data), data);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func main() {
|
||||
// Allocate C data buffer.
|
||||
width, height := 8, 2
|
||||
lenData := width * height
|
||||
// add string terminating null byte
|
||||
cData := (*C.uchar)(C.calloc(C.size_t(lenData+1), C.sizeof_uchar))
|
||||
|
||||
// When no longer in use, free C allocations.
|
||||
defer C.free(unsafe.Pointer(cData))
|
||||
|
||||
// Go slice reference to C data buffer,
|
||||
// minus string terminating null byte
|
||||
gData := (*[1 << 30]byte)(unsafe.Pointer(cData))[:lenData:lenData]
|
||||
|
||||
// Write and read cData via gData.
|
||||
for i := range gData {
|
||||
gData[i] = '.'
|
||||
}
|
||||
copy(gData[0:], "Data")
|
||||
gData[len(gData)-1] = 'X'
|
||||
fmt.Printf("gData: %d %q\n", len(gData), gData)
|
||||
C.printData(cData)
|
||||
}
|
||||
`}, 0, gosec.NewConfig()},
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user