//go:build safegostring

package sqlite3

import "C"

import (
	"unsafe"
)

// There is an issue with GoString which causes
// issues on arm with memory tagging and other systems that track
// technically out of bounds memory access: https://github.com/golang/go/issues/27610
// The following code performs strlen() on an incoming char * and safely
func SafeGoString(cString *C.char) string {

	if cString == nil {
		return ""
	}

	ptr := unsafe.Pointer(cString)
	i := 0
	for {
		charPtr := unsafe.Add(ptr, i)
		b := (*C.char)(charPtr)

		if *b == 0 {
			break
		}

		i += 1
	}

	return C.GoStringN(cString, C.int(i))
}
