Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Go 1.4 #79

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions xml/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
)

Expand Down Expand Up @@ -311,3 +312,26 @@ func TestUnparsedEntityURI(t *testing.T) {
t.Errorf("Expected '%s', but got '%s' calling doc.UnparsedEntityURI", expected, actual)
}
}

// Check we survive runtime moving our stack during String() call
func TestStringStackMove(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 25000; i++ {
wg.Add(1)
go func(j int) {
stringWithStack(j, &wg)
}(i)
}
wg.Wait()
}

func stringWithStack(n int, wg *sync.WaitGroup) string {
content := []byte("<hello>world " + strings.Repeat("a", n) + "</hello>")
d, err := Parse(content, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
if err != nil {
return err.Error()
}
defer d.Free()
defer wg.Done()
return d.String()
}
25 changes: 20 additions & 5 deletions xml/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import "C"

import (
"errors"
. "github.com/moovweb/gokogiri/util"
"github.com/moovweb/gokogiri/xpath"
"strconv"
"sync"
"unsafe"
. "github.com/moovweb/gokogiri/util"
"github.com/moovweb/gokogiri/xpath"
)

var (
Expand Down Expand Up @@ -150,6 +151,10 @@ var ErrTooLarge = errors.New("Output buffer too large")
//pre-allocate a buffer for serializing the document
const initialOutputBufferSize = 10 //100K

// store pointers during serialize->cgo->xmlNodeWriteCallback, in case runtime moves them
var pendingWrites = make(map[uintptr]*WriteBuffer)
var pendingWritesLock sync.RWMutex

/*
XmlNode implements the Node interface, and as such is the heart of the API.
*/
Expand Down Expand Up @@ -770,10 +775,18 @@ func (xmlNode *XmlNode) serialize(format SerializationOption, encoding, outputBu
wbuffer := &WriteBuffer{Node: xmlNode, Buffer: outputBuffer}
wbufferPtr := unsafe.Pointer(wbuffer)

pendingWritesLock.Lock()
pendingWrites[uintptr(wbufferPtr)] = wbuffer
pendingWritesLock.Unlock()

ret := int(C.xmlSaveNode(wbufferPtr, nodePtr, encodingPtr, C.int(format)))

pendingWritesLock.Lock()
delete(pendingWrites, uintptr(wbufferPtr))
pendingWritesLock.Unlock()

if ret < 0 {
panic("output error in xml node serialization: " + strconv.Itoa(ret))
return nil, 0
}

return wbuffer.Buffer, wbuffer.Offset
Expand Down Expand Up @@ -964,9 +977,11 @@ func (xmlNode *XmlNode) ParseFragment(input, url []byte, options ParseOption) (f

//export xmlNodeWriteCallback
func xmlNodeWriteCallback(wbufferObj unsafe.Pointer, data unsafe.Pointer, data_len C.int) {
wbuffer := (*WriteBuffer)(wbufferObj)
offset := wbuffer.Offset
pendingWritesLock.RLock()
wbuffer := pendingWrites[uintptr(wbufferObj)]
pendingWritesLock.RUnlock()

offset := wbuffer.Offset
if offset > len(wbuffer.Buffer) {
panic("fatal error in xmlNodeWriteCallback")
}
Expand Down
10 changes: 6 additions & 4 deletions xpath/xpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ import "runtime"
import "errors"

type XPath struct {
ContextPtr *C.xmlXPathContext
ResultPtr *C.xmlXPathObject
ContextPtr *C.xmlXPathContext
ResultPtr *C.xmlXPathObject
VariableScope VariableScope
}

type XPathObjectType int
Expand Down Expand Up @@ -208,8 +209,9 @@ func (xpath *XPath) ResultAsBoolean() (val bool, err error) {

// Add a variable resolver.
func (xpath *XPath) SetResolver(v VariableScope) {
C.set_var_lookup(xpath.ContextPtr, unsafe.Pointer(&v))
C.set_function_lookup(xpath.ContextPtr, unsafe.Pointer(&v))
xpath.VariableScope = v
C.set_var_lookup(xpath.ContextPtr, unsafe.Pointer(&xpath.VariableScope))
C.set_function_lookup(xpath.ContextPtr, unsafe.Pointer(&xpath.VariableScope))
}

// SetContextPosition sets the internal values needed to
Expand Down