Skip to content

Commit

Permalink
chore: Refactor Sample Code to Separate Files
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
adambkaplan authored and ccojocar committed Dec 8, 2023
1 parent bc03d1c commit 0e2a618
Show file tree
Hide file tree
Showing 38 changed files with 4,461 additions and 4,076 deletions.
52 changes: 52 additions & 0 deletions testutils/cgo_samples.go
Original file line number Diff line number Diff line change
@@ -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()},
}
)
Loading

0 comments on commit 0e2a618

Please sign in to comment.