title: Segmentation violation when using *C.uchar
body: <p>I need to deal with C code inside Go. <a href="http://xmlsoft.org/index.html" rel="nofollow noreferrer">libxml</a> and <a href="https://github.com/jbowtie/gokogiri" rel="nofollow noreferrer">github.com/jbowtie/gokogiri/xml</a> are used to generate XML files. However, the encoding doesn't appear in the final document, so I explicitly set the encoding (<code>docPtr.encoding = enc</code>):</p>
  <pre><code>func xmlSaveTree(docPtr *C.xmlDoc, encoding []byte, options int, outputBuffer []byte) ([]byte, int) {
      var (
          cEncoding *C.char
      )
  
      if len(encoding) &gt; 0 {
          enc := cbuf(encoding)
          docPtr.encoding = enc
          defer C.free(unsafe.Pointer(enc))
  
          cEncoding = C.CString(string(encoding))
          defer C.free(unsafe.Pointer(cEncoding))
      }
  
      wbufferMutex.Lock()
      defer wbufferMutex.Unlock()
      if outputBuffer == nil {
          outputBuffer = make([]byte, 0)
      }
      wbuffer = &amp;WriteBuffer{Buffer: outputBuffer}
  
      ret := int(C.MY_xmlSaveDoc(docPtr, cEncoding, C.int(options)))
      if ret &lt; 0 {
          panic(&quot;output error in xml node serialization: &quot; + strconv.Itoa(ret))
      }
      b, o := wbuffer.Buffer, wbuffer.Offset
      wbuffer = nil
  
      return b, o
  }
  
  func cbuf(buf []byte) *C.uchar {
      var bufptr *byte
      if cap(buf) &gt; 0 {
          bufptr = &amp;(buf[:1][0])
      }
      return (*C.uchar)(bufptr)
  }
  </code></pre>
  <p>Using this gives me a segmentation violation:</p>
  <blockquote>
  <p>fatal error: unexpected signal during runtime execution [signal
  SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7f0519e25c71]</p>
  <p>runtime stack: runtime.throw(0x6bed23, 0x2a)
  /home/usr/.go/src/runtime/panic.go:1116 +0x72 runtime.sigpanic()
  /home/usr/.go/src/runtime/signal_unix.go:704 +0x4ac</p>
  <p>goroutine 39 [syscall]: runtime.cgocall(0x63c840, 0xc000050b48, 0x0)
  /home/usr/.go/src/runtime/cgocall.go:133 +0x5b fp=0xc000050b18 sp=0xc000050ae0 pc=0x40859b
  gitlab.com/company/xmlmix/document/internal._Cfunc_free(0xa64258)
  _cgo_gotypes.go:265 +0x45 fp=0xc000050b48 sp=0xc000050b18 pc=0x57eb05
  gitlab.com/company/xmlmix/document/internal.xmlSaveTree.func1.1()
  /mnt/Entwicklung/project/xmlmix/document/internal/marshal.go:82
  +0x53 fp=0xc000050b80 sp=0xc000050b48 pc=0x57fa33 gitlab.com/company/xmlmix/document/internal.xmlSaveTree(0x27ae130,
  0xa64258, 0x5, 0x5, 0x23, 0xaa7408, 0x0, 0x0, 0xc0001ac000, 0x2534,
  ...)
  /mnt/project/xmlmix/document/internal/marshal.go:102
  +0x2b0 fp=0xc000050c18 sp=0xc000050b80 pc=0x57f710 gitlab.com/company/xmlmix/document/internal.XMLSaveDoc(0x701920,
  0xc0000fa990, 0x23, 0xa64258, 0x5, 0x5, 0xe, 0xf, 0xc000050d48,
  0x555855, ...)
  /mnt/project/xmlmix/document/internal/marshal.go:62
  +0xe7 fp=0xc000050c90 sp=0xc000050c18 pc=0x57f3c7 gitlab.com/company/xmlmix/document.MarshalIndent(0xc0000a68b8, 0x0,
  0x0, 0x0, 0x0, 0x0)
  /mnt/project/xmlmix/document/marshal.go:50 +0x105 fp=0xc000050d58 sp=0xc000050c90 pc=0x5d92c5</p>
  </blockquote>