Go "signal SIGSEGV: segmentation violation" in Tests - Fix in CI
The test binary received a SIGSEGV - it accessed memory it should not. In pure Go this is almost always a nil dereference the runtime reports; with cgo or unsafe, it can be a genuine bad memory access in C code.
What this error means
A test run aborts with signal SIGSEGV: segmentation violation followed by a stack trace and a [signal SIGSEGV: ... addr=0x0 pc=...] line. addr=0x0 strongly suggests a nil pointer.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x...]
goroutine 6 [running]:
app.(*Server).Handle(...)
/app/server.go:52Common causes
A nil pointer dereference
With addr=0x0, the code dereferenced a nil pointer, interface, or map - the most common SIGSEGV in pure Go.
A bad cgo or unsafe access
cgo passing an invalid pointer to C, or unsafe.Pointer arithmetic gone wrong, causes a real segfault in native code.
How to fix it
Trace the nil dereference and guard it
- Read the stack frame just under the SIGSEGV line - that is where the bad access happened.
- Add a nil check (or initialize the value) before the dereference.
- Cover the nil path with a test so it cannot regress.
Inspect cgo / unsafe usage
When cgo or unsafe is involved, verify pointers are valid for the C call and not freed or moved.
go vet ./... # flags some unsafe.Pointer misuse
go test -race ./... # surfaces memory issues with cgoHow to prevent it
- Initialize pointers/interfaces before use and check for nil at boundaries.
- Follow the cgo pointer rules and run
go veton unsafe code. - Add tests for nil and error paths, not just the happy path.