From 1556424e40b131ae08ab2819f4a7e9481090e238 Mon Sep 17 00:00:00 2001 From: Bulat Khasanov Date: Wed, 17 Apr 2024 13:51:43 +0300 Subject: [PATCH] Fix uintptr usage in structs --- clib/clib.go | 330 ++++++++++++------------ clib/interface.go | 7 +- dom/dom.go | 3 +- dom/interface.go | 5 +- dom/node.go | 10 +- dom/node_document.go | 6 +- dom/node_namespace.go | 2 +- dom/node_wrap.go | 17 +- html.go | 2 +- internal/cmd/genwrapnode/genwrapnode.go | 7 +- parser/interface.go | 7 +- parser/parser.go | 7 +- types/interface.go | 8 +- xpath/interface.go | 8 +- xpath/iterator.go | 6 +- xpath/xpath.go | 9 +- xsd/interface.go | 8 +- xsd/xsd.go | 6 +- 18 files changed, 239 insertions(+), 209 deletions(-) diff --git a/clib/clib.go b/clib/clib.go index e8319ae..3f5de60 100644 --- a/clib/clib.go +++ b/clib/clib.go @@ -362,8 +362,8 @@ func validDocumentPtr(doc PtrSource) (*C.xmlDoc, error) { return nil, ErrInvalidDocument } - if dptr := doc.Pointer(); dptr != 0 { - return (*C.xmlDoc)(unsafe.Pointer(dptr)), nil + if dptr := doc.Pointer(); dptr != nil { + return (*C.xmlDoc)(dptr), nil } return nil, ErrInvalidDocument } @@ -373,8 +373,8 @@ func validParserCtxtPtr(s PtrSource) (*C.xmlParserCtxt, error) { return nil, ErrInvalidParser } - if ptr := s.Pointer(); ptr != 0 { - return (*C.xmlParserCtxt)(unsafe.Pointer(ptr)), nil + if ptr := s.Pointer(); ptr != nil { + return (*C.xmlParserCtxt)(ptr), nil } return nil, ErrInvalidParser } @@ -385,11 +385,11 @@ func validNodePtr(n PtrSource) (*C.xmlNode, error) { } nptr := n.Pointer() - if nptr == 0 { + if nptr == nil { return nil, ErrInvalidNode } - return (*C.xmlNode)(unsafe.Pointer(nptr)), nil + return (*C.xmlNode)(nptr), nil } func validAttributePtr(n PtrSource) (*C.xmlAttr, error) { @@ -397,8 +397,8 @@ func validAttributePtr(n PtrSource) (*C.xmlAttr, error) { return nil, ErrInvalidAttribute } - if nptr := n.Pointer(); nptr != 0 { - return (*C.xmlAttr)(unsafe.Pointer(nptr)), nil + if nptr := n.Pointer(); nptr != nil { + return (*C.xmlAttr)(nptr), nil } return nil, ErrInvalidAttribute @@ -409,8 +409,8 @@ func validXPathContextPtr(x PtrSource) (*C.xmlXPathContext, error) { return nil, ErrInvalidXPathContext } - if xptr := x.Pointer(); xptr != 0 { - return (*C.xmlXPathContext)(unsafe.Pointer(xptr)), nil + if xptr := x.Pointer(); xptr != nil { + return (*C.xmlXPathContext)(xptr), nil } return nil, ErrInvalidXPathContext } @@ -420,8 +420,8 @@ func validXPathExpressionPtr(x PtrSource) (*C.xmlXPathCompExpr, error) { return nil, ErrInvalidXPathExpression } - if xptr := x.Pointer(); xptr != 0 { - return (*C.xmlXPathCompExpr)(unsafe.Pointer(xptr)), nil + if xptr := x.Pointer(); xptr != nil { + return (*C.xmlXPathCompExpr)(xptr), nil } return nil, ErrInvalidXPathExpression } @@ -431,8 +431,8 @@ func validXPathObjectPtr(x PtrSource) (*C.xmlXPathObject, error) { return nil, ErrInvalidXPathObject } - if xptr := x.Pointer(); xptr != 0 { - return (*C.xmlXPathObject)(unsafe.Pointer(xptr)), nil + if xptr := x.Pointer(); xptr != nil { + return (*C.xmlXPathObject)(xptr), nil } return nil, ErrInvalidXPathObject } @@ -467,8 +467,8 @@ func xmlCtxtLastError(ctx PtrSource) error { return xmlCtxtLastErrorRaw(ctx.Pointer()) } -func xmlCtxtLastErrorRaw(ctx uintptr) error { - e := C.MY_xmlCtxtLastError(unsafe.Pointer(ctx)) +func xmlCtxtLastErrorRaw(ctx unsafe.Pointer) error { + e := C.MY_xmlCtxtLastError(ctx) if e == nil { return errors.New(`unknown error`) } @@ -487,31 +487,31 @@ func stringToXMLChar(s string) *C.xmlChar { return (*C.xmlChar)(unsafe.Pointer(C.CString(s))) } -func XMLSchemaParseFromFile(path string) (uintptr, error) { +func XMLSchemaParseFromFile(path string) (unsafe.Pointer, error) { parserCtx := C.xmlSchemaNewParserCtxt(C.CString(path)) if parserCtx == nil { - return 0, errors.New("failed to create parser") + return nil, errors.New("failed to create parser") } defer C.xmlSchemaFreeParserCtxt(parserCtx) s := C.xmlSchemaParse(parserCtx) if s == nil { - return 0, errors.New("failed to parse schema") + return nil, errors.New("failed to parse schema") } - return uintptr(unsafe.Pointer(s)), nil + return unsafe.Pointer(s), nil } -func XMLCreateMemoryParserCtxt(s string, o int) (uintptr, error) { +func XMLCreateMemoryParserCtxt(s string, o int) (unsafe.Pointer, error) { cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) ctx := C.xmlCreateMemoryParserCtxt(cs, C.int(len(s))) if ctx == nil { - return 0, errors.New("error creating parser") + return nil, errors.New("error creating parser") } C.xmlCtxtUseOptions(ctx, C.int(o)) - return uintptr(unsafe.Pointer(ctx)), nil + return unsafe.Pointer(ctx), nil } func XMLParseDocument(ctx PtrSource) error { @@ -536,7 +536,7 @@ func XMLFreeParserCtxt(ctx PtrSource) error { return nil } -func HTMLReadDoc(content, url, encoding string, opts int) (uintptr, error) { +func HTMLReadDoc(content, url, encoding string, opts int) (unsafe.Pointer, error) { // TODO: use htmlCtxReadDoc later, so we can get the error ccontent := C.CString(content) curl := C.CString(url) @@ -553,13 +553,13 @@ func HTMLReadDoc(content, url, encoding string, opts int) (uintptr, error) { ) if doc == nil { - return 0, errors.New("failed to parse document") + return nil, errors.New("failed to parse document") } - return uintptr(unsafe.Pointer(doc)), nil + return unsafe.Pointer(doc), nil } -func XMLCreateDocument(version, encoding string) uintptr { +func XMLCreateDocument(version, encoding string) unsafe.Pointer { cver := stringToXMLChar(version) defer C.free(unsafe.Pointer(cver)) @@ -570,7 +570,7 @@ func XMLCreateDocument(version, encoding string) uintptr { doc.encoding = C.xmlStrdup(cenc) } - return uintptr(unsafe.Pointer(doc)) + return unsafe.Pointer(doc) } func XMLEncodeEntitiesReentrant(docptr *C.xmlDoc, s string) (*C.xmlChar, error) { @@ -620,25 +620,25 @@ func validNamespacePtr(s PtrSource) (*C.xmlNs, error) { return nil, ErrInvalidNamespace } - if ptr := s.Pointer(); ptr != 0 { - return (*C.xmlNs)(unsafe.Pointer(ptr)), nil + if ptr := s.Pointer(); ptr != nil { + return (*C.xmlNs)(ptr), nil } return nil, ErrInvalidNamespace } -func XMLNewNode(ns PtrSource, name string) (uintptr, error) { +func XMLNewNode(ns PtrSource, name string) (unsafe.Pointer, error) { nsptr, err := validNamespacePtr(ns) if err != nil { - return 0, err + return nil, err } if err := isSafeName(name); err != nil { - return 0, err + return nil, err } var cname [MaxElementNameLength]C.xmlChar if len(name) > MaxElementNameLength { - return 0, ErrElementNameTooLong + return nil, ErrElementNameTooLong } for i := 0; i < len(name); i++ { cname[i] = C.xmlChar(name[i]) @@ -649,21 +649,21 @@ func XMLNewNode(ns PtrSource, name string) (uintptr, error) { (*C.xmlNs)(unsafe.Pointer(nsptr)), (*C.xmlChar)(cnameptr), ) - return uintptr(unsafe.Pointer(n)), nil + return unsafe.Pointer(n), nil } -func XMLNewDocProp(doc PtrSource, k, v string) (uintptr, error) { +func XMLNewDocProp(doc PtrSource, k, v string) (unsafe.Pointer, error) { docptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } if err := isSafeName(k); err != nil { - return 0, err + return nil, err } if len(k) > MaxAttributeNameLength { - return 0, ErrAttributeNameTooLong + return nil, ErrAttributeNameTooLong } var kx [MaxAttributeNameLength]C.xmlChar @@ -676,25 +676,25 @@ func XMLNewDocProp(doc PtrSource, k, v string) (uintptr, error) { ent, err := XMLEncodeEntitiesReentrant(docptr, v) if err != nil { - return 0, err + return nil, err } attr := C.xmlNewDocProp(docptr, (*C.xmlChar)(kxptr), ent) - return uintptr(unsafe.Pointer(attr)), nil + return unsafe.Pointer(attr), nil } -func XMLSearchNsByHref(doc PtrSource, _ PtrSource, uri string) (uintptr, error) { +func XMLSearchNsByHref(doc PtrSource, _ PtrSource, uri string) (unsafe.Pointer, error) { docptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } nptr, err := validNodePtr(doc) if err != nil { - return 0, err + return nil, err } if len(uri) > MaxNamespaceURILength { - return 0, ErrNamespaceURITooLong + return nil, ErrNamespaceURITooLong } var xcuri [MaxNamespaceURILength]C.xmlChar @@ -709,20 +709,20 @@ func XMLSearchNsByHref(doc PtrSource, _ PtrSource, uri string) (uintptr, error) (*C.xmlChar)(xcuriptr), ) if ns == nil { - return 0, ErrNamespaceNotFound{Target: uri} + return nil, ErrNamespaceNotFound{Target: uri} } - return uintptr(unsafe.Pointer(ns)), nil + return unsafe.Pointer(ns), nil } -func XMLSearchNs(doc PtrSource, n PtrSource, prefix string) (uintptr, error) { +func XMLSearchNs(doc PtrSource, n PtrSource, prefix string) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } docptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } cprefix := stringToXMLChar(prefix) @@ -730,20 +730,20 @@ func XMLSearchNs(doc PtrSource, n PtrSource, prefix string) (uintptr, error) { ns := C.xmlSearchNs(docptr, nptr, cprefix) if ns == nil { - return 0, ErrNamespaceNotFound{Target: prefix} + return nil, ErrNamespaceNotFound{Target: prefix} } - return uintptr(unsafe.Pointer(ns)), nil + return unsafe.Pointer(ns), nil } -func XMLNewDocNode(doc PtrSource, ns PtrSource, local, content string) (uintptr, error) { +func XMLNewDocNode(doc PtrSource, ns PtrSource, local, content string) (unsafe.Pointer, error) { docptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } nsptr, err := validNamespacePtr(ns) if err != nil { - return 0, err + return nil, err } var c *C.xmlChar @@ -757,15 +757,15 @@ func XMLNewDocNode(doc PtrSource, ns PtrSource, local, content string) (uintptr, ptr := C.xmlNewDocNode(docptr, nsptr, clocal, c) if ptr == nil { - return 0, errors.New("failed to create node") + return nil, errors.New("failed to create node") } - return uintptr(unsafe.Pointer(ptr)), nil + return unsafe.Pointer(ptr), nil } -func XMLNewNs(n PtrSource, nsuri, prefix string) (uintptr, error) { +func XMLNewNs(n PtrSource, nsuri, prefix string) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } var cprefix *C.xmlChar @@ -779,9 +779,9 @@ func XMLNewNs(n PtrSource, nsuri, prefix string) (uintptr, error) { nsptr := C.xmlNewNs(nptr, cnsuri, cprefix) if nsptr == nil { - return 0, errors.New("failed to create namespace") + return nil, errors.New("failed to create namespace") } - return uintptr(unsafe.Pointer(nsptr)), nil + return unsafe.Pointer(nsptr), nil } func XMLSetNs(n PtrSource, ns PtrSource) error { @@ -799,41 +799,41 @@ func XMLSetNs(n PtrSource, ns PtrSource) error { return nil } -func XMLNewCDataBlock(doc PtrSource, txt string) (uintptr, error) { +func XMLNewCDataBlock(doc PtrSource, txt string) (unsafe.Pointer, error) { dptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } ctxt := stringToXMLChar(txt) defer C.free(unsafe.Pointer(ctxt)) ptr := C.xmlNewCDataBlock(dptr, ctxt, C.int(len(txt))) if ptr == nil { - return 0, errors.New("failed to create CDATA block") + return nil, errors.New("failed to create CDATA block") } - return uintptr(unsafe.Pointer(ptr)), nil + return unsafe.Pointer(ptr), nil } -func XMLNewComment(txt string) (uintptr, error) { +func XMLNewComment(txt string) (unsafe.Pointer, error) { ctxt := stringToXMLChar(txt) defer C.free(unsafe.Pointer(ctxt)) ptr := C.xmlNewComment(ctxt) if ptr == nil { - return 0, errors.New("failed to create comment node") + return nil, errors.New("failed to create comment node") } - return uintptr(unsafe.Pointer(ptr)), nil + return unsafe.Pointer(ptr), nil } -func XMLNewText(txt string) (uintptr, error) { +func XMLNewText(txt string) (unsafe.Pointer, error) { ctxt := stringToXMLChar(txt) defer C.free(unsafe.Pointer(ctxt)) ptr := C.xmlNewText(ctxt) if ptr == nil { - return 0, errors.New("failed to create text node") + return nil, errors.New("failed to create text node") } - return uintptr(unsafe.Pointer(ptr)), nil + return unsafe.Pointer(ptr), nil } func XMLNodeName(n PtrSource) (string, error) { @@ -917,29 +917,29 @@ func XMLAddChild(n PtrSource, child PtrSource) error { return nil } -func XMLOwnerDocument(n PtrSource) (uintptr, error) { +func XMLOwnerDocument(n PtrSource) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } if nptr.doc == nil { - return 0, ErrInvalidDocument + return nil, ErrInvalidDocument } - return uintptr(unsafe.Pointer(nptr.doc)), nil + return unsafe.Pointer(nptr.doc), nil } -func XMLFirstChild(n PtrSource) (uintptr, error) { +func XMLFirstChild(n PtrSource) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } if !XMLHasChildNodes(n) { - return 0, errors.New("no children") + return nil, errors.New("no children") } - return uintptr(unsafe.Pointer(nptr.children)), nil + return unsafe.Pointer(nptr.children), nil } func XMLHasChildNodes(n PtrSource) bool { @@ -950,12 +950,12 @@ func XMLHasChildNodes(n PtrSource) bool { return nptr.children != nil } -func XMLLastChild(n PtrSource) (uintptr, error) { +func XMLLastChild(n PtrSource) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } - return uintptr(unsafe.Pointer(nptr.last)), nil + return unsafe.Pointer(nptr.last), nil } func XMLLocalName(n PtrSource) string { @@ -986,20 +986,20 @@ func XMLNamespaceURI(n PtrSource) string { return "" } -func XMLNextSibling(n PtrSource) (uintptr, error) { +func XMLNextSibling(n PtrSource) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } - return uintptr(unsafe.Pointer(nptr.next)), nil + return unsafe.Pointer(nptr.next), nil } -func XMLParentNode(n PtrSource) (uintptr, error) { +func XMLParentNode(n PtrSource) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } - return uintptr(unsafe.Pointer(nptr.parent)), nil + return unsafe.Pointer(nptr.parent), nil } func XMLPrefix(n PtrSource) string { @@ -1017,12 +1017,12 @@ func XMLPrefix(n PtrSource) string { return "" } -func XMLPreviousSibling(n PtrSource) (uintptr, error) { +func XMLPreviousSibling(n PtrSource) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } - return uintptr(unsafe.Pointer(nptr.prev)), nil + return unsafe.Pointer(nptr.prev), nil } func XMLSetNodeName(n PtrSource, name string) error { @@ -1129,8 +1129,8 @@ func XMLLookupNamespaceURI(n PtrSource, prefix string) (string, error) { return xmlCharToString(ns.href), nil } -func XMLGetNodeTypeRaw(n uintptr) XMLNodeType { - nptr := (*C.xmlNode)(unsafe.Pointer(n)) +func XMLGetNodeTypeRaw(n unsafe.Pointer) XMLNodeType { + nptr := (*C.xmlNode)(n) return XMLNodeType(nptr._type) } @@ -1142,15 +1142,15 @@ func XMLGetNodeType(n PtrSource) XMLNodeType { return XMLNodeType(nptr._type) } -func XMLChildNodes(n PtrSource) ([]uintptr, error) { +func XMLChildNodes(n PtrSource) ([]unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { return nil, errors.Wrap(err, "failed to get valid node for XMLChildNodes") } - ret := []uintptr(nil) + var ret []unsafe.Pointer for chld := nptr.children; chld != nil; chld = chld.next { - ret = append(ret, uintptr(unsafe.Pointer(chld))) + ret = append(ret, unsafe.Pointer(chld)) } return ret, nil } @@ -1332,25 +1332,25 @@ func XMLNamespaceFree(n PtrSource) { C.MY_xmlFree(unsafe.Pointer(nsptr)) } -func XMLCreateAttributeNS(doc PtrSource, uri, k, v string) (uintptr, error) { +func XMLCreateAttributeNS(doc PtrSource, uri, k, v string) (unsafe.Pointer, error) { dptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } rootptr := C.xmlDocGetRootElement(dptr) if rootptr == nil { - return 0, errors.New("no document element found") + return nil, errors.New("no document element found") } if err := isSafeName(k); err != nil { - return 0, err + return nil, err } prefix, local := SplitPrefixLocal(k) if len(uri) > MaxNamespaceURILength { - return 0, ErrNamespaceURITooLong + return nil, ErrNamespaceURITooLong } var xcuri [MaxNamespaceURILength]C.xmlChar @@ -1366,7 +1366,7 @@ func XMLCreateAttributeNS(doc PtrSource, uri, k, v string) (uintptr, error) { ) if ns == nil { if len(prefix) > MaxAttributeNameLength { - return 0, ErrAttributeNameTooLong + return nil, ErrAttributeNameTooLong } var xcprefix [MaxAttributeNameLength]C.xmlChar for i := 0; i < len(prefix); i++ { @@ -1380,7 +1380,7 @@ func XMLCreateAttributeNS(doc PtrSource, uri, k, v string) (uintptr, error) { (*C.xmlChar)(xcprefixptr), ) if ns == nil { - return 0, errors.New("failed to create namespace") + return nil, errors.New("failed to create namespace") } } @@ -1389,7 +1389,7 @@ func XMLCreateAttributeNS(doc PtrSource, uri, k, v string) (uintptr, error) { ent := C.xmlEncodeEntitiesReentrant(dptr, xcv) if ent == nil { - return 0, errors.New("failed to encode value") + return nil, errors.New("failed to encode value") } xclocal := stringToXMLChar(local) @@ -1399,18 +1399,18 @@ func XMLCreateAttributeNS(doc PtrSource, uri, k, v string) (uintptr, error) { C.xmlSetNs((*C.xmlNode)(unsafe.Pointer(attr)), ns) - return uintptr(unsafe.Pointer(attr)), nil + return unsafe.Pointer(attr), nil } -func XMLCreateElement(d PtrSource, name string) (uintptr, error) { +func XMLCreateElement(d PtrSource, name string) (unsafe.Pointer, error) { dptr, err := validDocumentPtr(d) if err != nil { - return 0, err + return nil, err } var xcname [MaxElementNameLength]C.xmlChar if len(name) > MaxElementNameLength { - return 0, ErrElementNameTooLong + return nil, ErrElementNameTooLong } for i := 0; i < len(name); i++ { xcname[i] = C.xmlChar(name[i]) @@ -1419,24 +1419,24 @@ func XMLCreateElement(d PtrSource, name string) (uintptr, error) { nptr := C.MY_xmlCreateElement(dptr, (*C.xmlChar)(xcnameptr)) if nptr == nil { - return 0, errors.New("element creation failed") + return nil, errors.New("element creation failed") } - return uintptr(unsafe.Pointer(nptr)), nil + return unsafe.Pointer(nptr), nil } -func XMLCreateElementNS(doc PtrSource, nsuri, name string) (uintptr, error) { +func XMLCreateElementNS(doc PtrSource, nsuri, name string) (unsafe.Pointer, error) { if nsuri == "" { return XMLCreateElement(doc, name) } dptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } if len(nsuri) > MaxNamespaceURILength { - return 0, ErrNamespaceURITooLong + return nil, ErrNamespaceURITooLong } var xcnsuri [MaxNamespaceURILength]C.xmlChar @@ -1457,10 +1457,10 @@ func XMLCreateElementNS(doc PtrSource, nsuri, name string) (uintptr, error) { (*C.xmlChar)(xcnameptr), ) if nptr == nil { - return 0, errors.New("failed to create element") + return nil, errors.New("failed to create element") } - return uintptr(unsafe.Pointer(nptr)), nil + return unsafe.Pointer(nptr), nil } func XMLDocumentEncoding(doc PtrSource) string { @@ -1495,17 +1495,17 @@ func XMLDocumentVersion(doc PtrSource) string { return xmlCharToString(dptr.version) } -func XMLDocumentElement(doc PtrSource) (uintptr, error) { +func XMLDocumentElement(doc PtrSource) (unsafe.Pointer, error) { dptr, err := validDocumentPtr(doc) if err != nil { - return 0, err + return nil, err } ptr := C.xmlDocGetRootElement(dptr) if ptr == nil { - return 0, errors.New("no document element found") + return nil, errors.New("no document element found") } - return uintptr(unsafe.Pointer(ptr)), nil + return unsafe.Pointer(ptr), nil } func XMLFreeDoc(doc PtrSource) error { @@ -1647,26 +1647,26 @@ func XMLSetProp(n PtrSource, name, value string) error { return nil } -func XMLElementAttributes(n PtrSource) ([]uintptr, error) { +func XMLElementAttributes(n PtrSource) ([]unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { return nil, errors.Wrap(err, "failed to get valid node for XMLElementAttributes") } - attrs := []uintptr{} + attrs := []unsafe.Pointer{} for attr := nptr.properties; attr != nil; attr = attr.next { - attrs = append(attrs, uintptr(unsafe.Pointer(attr))) + attrs = append(attrs, unsafe.Pointer(attr)) } return attrs, nil } -func XMLElementNamespaces(n PtrSource) ([]uintptr, error) { +func XMLElementNamespaces(n PtrSource) ([]unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { return nil, errors.Wrap(err, "failed to get valid node for XMLElementNamespaces") } - ret := []uintptr{} + ret := []unsafe.Pointer{} for ns := nptr.nsDef; ns != nil; ns = ns.next { if ns.prefix == nil && ns.href == nil { continue @@ -1677,15 +1677,15 @@ func XMLElementNamespaces(n PtrSource) ([]uintptr, error) { continue } - ret = append(ret, uintptr(unsafe.Pointer(newns))) + ret = append(ret, unsafe.Pointer(newns)) } return ret, nil } -func XMLElementGetAttributeNode(n PtrSource, name string) (uintptr, error) { +func XMLElementGetAttributeNode(n PtrSource, name string) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } // if this is "xmlns", look for the first namespace without @@ -1727,10 +1727,10 @@ func XMLElementGetAttributeNode(n PtrSource, name string) (uintptr, error) { } if prop == nil || XMLNodeType(prop._type) != AttributeNode { - return 0, ErrAttributeNotFound + return nil, ErrAttributeNotFound } - return uintptr(unsafe.Pointer(prop)), nil + return unsafe.Pointer(prop), nil } func XMLFreeProp(attr PtrSource) error { @@ -1843,23 +1843,23 @@ func XMLAppendText(n PtrSource, s string) error { return nil } -func XMLDocCopyNode(n PtrSource, d PtrSource, extended int) (uintptr, error) { +func XMLDocCopyNode(n PtrSource, d PtrSource, extended int) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } dptr, err := validDocumentPtr(d) if err != nil { - return 0, err + return nil, err } ret := C.xmlDocCopyNode(nptr, dptr, C.int(extended)) if ret == nil { - return 0, errors.New("copy node failed") + return nil, errors.New("copy node failed") } - return uintptr(unsafe.Pointer(ret)), nil + return unsafe.Pointer(ret), nil } func XMLSetTreeDoc(n PtrSource, d PtrSource) error { @@ -1877,23 +1877,23 @@ func XMLSetTreeDoc(n PtrSource, d PtrSource) error { return nil } -func XMLParseInNodeContext(n PtrSource, data string, o int) (uintptr, error) { +func XMLParseInNodeContext(n PtrSource, data string, o int) (unsafe.Pointer, error) { nptr, err := validNodePtr(n) if err != nil { - return 0, err + return nil, err } var ret C.xmlNodePtr cdata := C.CString(data) defer C.free(unsafe.Pointer(cdata)) if C.xmlParseInNodeContext(nptr, cdata, C.int(len(data)), C.int(o), &ret) != 0 { - return 0, errors.New("XXX PLACE HOLDER XXX") + return nil, errors.New("XXX PLACE HOLDER XXX") } - return uintptr(unsafe.Pointer(ret)), nil + return unsafe.Pointer(ret), nil } -func XMLXPathNewContext(n PtrSource) (uintptr, error) { +func XMLXPathNewContext(n PtrSource) (unsafe.Pointer, error) { ctx := C.xmlXPathNewContext(nil) ctx.namespaces = nil @@ -1902,7 +1902,7 @@ func XMLXPathNewContext(n PtrSource) (uintptr, error) { ctx.node = (*C.xmlNode)(unsafe.Pointer(nptr)) } - return uintptr(unsafe.Pointer(ctx)), nil + return unsafe.Pointer(ctx), nil } func XMLXPathContextSetContextNode(x PtrSource, n PtrSource) error { @@ -1920,9 +1920,9 @@ func XMLXPathContextSetContextNode(x PtrSource, n PtrSource) error { return nil } -func XMLXPathCompile(s string) (uintptr, error) { +func XMLXPathCompile(s string) (unsafe.Pointer, error) { if len(s) > MaxXPathExpressionLength { - return 0, ErrXPathExpressionTooLong + return nil, ErrXPathExpressionTooLong } var xcs [MaxXPathExpressionLength]C.xmlChar @@ -1932,9 +1932,9 @@ func XMLXPathCompile(s string) (uintptr, error) { xcsptr := unsafe.Pointer(&xcs[0]) if p := C.xmlXPathCompile((*C.xmlChar)(xcsptr)); p != nil { - return uintptr(unsafe.Pointer(p)), nil + return unsafe.Pointer(p), nil } - return 0, ErrXPathCompileFailure + return nil, ErrXPathCompileFailure } func XMLXPathFreeCompExpr(x PtrSource) error { @@ -2001,15 +2001,15 @@ func XMLXPathRegisterNS(x PtrSource, prefix, nsuri string) error { return nil } -func XMLEvalXPath(x PtrSource, expr PtrSource) (uintptr, error) { +func XMLEvalXPath(x PtrSource, expr PtrSource) (unsafe.Pointer, error) { xptr, err := validXPathContextPtr(x) if err != nil { - return 0, err + return nil, err } exprptr, err := validXPathExpressionPtr(expr) if err != nil { - return 0, err + return nil, err } // If there is no document associated with this context, @@ -2028,10 +2028,10 @@ func XMLEvalXPath(x PtrSource, expr PtrSource) (uintptr, error) { res := C.xmlXPathCompiledEval(exprptr, xptr) if res == nil { - return 0, ErrXPathEmptyResult + return nil, ErrXPathEmptyResult } - return uintptr(unsafe.Pointer(res)), nil + return unsafe.Pointer(res), nil } func XMLXPathFreeObject(x PtrSource) { @@ -2084,7 +2084,7 @@ func XMLXPathObjectBool(x PtrSource) bool { return xptr.boolval == 1 } -func XMLXPathObjectNodeList(x PtrSource) ([]uintptr, error) { +func XMLXPathObjectNodeList(x PtrSource) ([]unsafe.Pointer, error) { // Probably needs NodeList iterator xptr, err := validXPathObjectPtr(x) if err != nil { @@ -2102,9 +2102,9 @@ func XMLXPathObjectNodeList(x PtrSource) ([]uintptr, error) { nodes := unsafe.Slice(nodeset.nodeTab, nodeset.nodeNr) - ret := make([]uintptr, nodeset.nodeNr) + ret := make([]unsafe.Pointer, nodeset.nodeNr) for i := 0; i < int(nodeset.nodeNr); i++ { - ret[i] = uintptr(unsafe.Pointer(nodes[i])) + ret[i] = unsafe.Pointer(nodes[i]) } return ret, nil @@ -2118,7 +2118,7 @@ func XMLTextData(n PtrSource) string { return xmlCharToString(nptr.content) } -func XMLSchemaParse(buf []byte, options ...option.Interface) (uintptr, error) { +func XMLSchemaParse(buf []byte, options ...option.Interface) (unsafe.Pointer, error) { var uri string var encoding string var coptions int @@ -2132,7 +2132,7 @@ func XMLSchemaParse(buf []byte, options ...option.Interface) (uintptr, error) { docctx := C.xmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf))) if docctx == nil { - return 0, errors.New("error creating doc parser") + return nil, errors.New("error creating doc parser") } var curi *C.char @@ -2149,22 +2149,22 @@ func XMLSchemaParse(buf []byte, options ...option.Interface) (uintptr, error) { doc := C.xmlCtxtReadMemory(docctx, (*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)), curi, cencoding, C.int(coptions)) if doc == nil { - return 0, errors.Errorf("failed to read schema from memory: %v", - xmlCtxtLastErrorRaw(uintptr(unsafe.Pointer(docctx)))) + return nil, errors.Errorf("failed to read schema from memory: %v", + xmlCtxtLastErrorRaw(unsafe.Pointer(docctx))) } parserCtx := C.xmlSchemaNewDocParserCtxt((*C.xmlDoc)(unsafe.Pointer(doc))) if parserCtx == nil { - return 0, errors.New("failed to create parser") + return nil, errors.New("failed to create parser") } defer C.xmlSchemaFreeParserCtxt(parserCtx) s := C.xmlSchemaParse(parserCtx) if s == nil { - return 0, errors.New("failed to parse schema") + return nil, errors.New("failed to parse schema") } - return uintptr(unsafe.Pointer(s)), nil + return unsafe.Pointer(s), nil } func XMLSchemaValidateDocument(schema PtrSource, document PtrSource, options ...int) []error { @@ -2209,11 +2209,11 @@ func validSchemaPtr(schema PtrSource) (*C.xmlSchema, error) { return nil, ErrInvalidSchema } sptr := schema.Pointer() - if sptr == 0 { + if sptr == nil { return nil, ErrInvalidSchema } - return (*C.xmlSchema)(unsafe.Pointer(sptr)), nil + return (*C.xmlSchema)(sptr), nil } func XMLSchemaFree(s PtrSource) error { @@ -2226,10 +2226,10 @@ func XMLSchemaFree(s PtrSource) error { return nil } -func XMLCtxtReadMemory(ctx PtrSource, file string, baseURL string, encoding string, options int) (uintptr, error) { +func XMLCtxtReadMemory(ctx PtrSource, file string, baseURL string, encoding string, options int) (unsafe.Pointer, error) { ctxptr, err := validParserCtxtPtr(ctx) if err != nil { - return 0, errors.Wrap(err, "not a valid pointer") + return nil, errors.Wrap(err, "not a valid pointer") } var cfile, cbaseURL, cencoding *C.char @@ -2250,7 +2250,7 @@ func XMLCtxtReadMemory(ctx PtrSource, file string, baseURL string, encoding stri doc := C.xmlCtxtReadMemory(ctxptr, cfile, C.int(len(file)), cbaseURL, cencoding, C.int(options)) if doc == nil { - return 0, errors.Errorf("failed to read document from memory: %v", xmlCtxtLastError(ctx)) + return nil, errors.Errorf("failed to read document from memory: %v", xmlCtxtLastError(ctx)) } - return uintptr(unsafe.Pointer(doc)), nil + return unsafe.Pointer(doc), nil } diff --git a/clib/interface.go b/clib/interface.go index 835e3cd..effeb9c 100644 --- a/clib/interface.go +++ b/clib/interface.go @@ -1,6 +1,9 @@ package clib -import "errors" +import ( + "errors" + "unsafe" +) const ( MaxEncodingLength = 256 @@ -23,7 +26,7 @@ type C14NMode int // where a Document node is expected, but it is the caller's // responsibility to align the argument list. type PtrSource interface { - Pointer() uintptr + Pointer() unsafe.Pointer } // XMLNodeType identifies the type of the underlying C struct diff --git a/dom/dom.go b/dom/dom.go index c8ecaf8..961702b 100644 --- a/dom/dom.go +++ b/dom/dom.go @@ -2,6 +2,7 @@ package dom import ( "sync" + "unsafe" "github.com/lestrrat-go/libxml2/xpath" ) @@ -20,7 +21,7 @@ func SetupXPathCallback() { xpath.WrapNodeFunc = WrapNode } -func WrapDocument(n uintptr) *Document { +func WrapDocument(n unsafe.Pointer) *Document { //nolint:forcetypeassert doc := docPool.Get().(*Document) doc.mortal = false diff --git a/dom/interface.go b/dom/interface.go index 622b738..5dc1e00 100644 --- a/dom/interface.go +++ b/dom/interface.go @@ -2,6 +2,7 @@ package dom import ( "errors" + "unsafe" "github.com/lestrrat-go/libxml2/clib" ) @@ -39,7 +40,7 @@ const ( ) type XMLNode struct { - ptr uintptr // *C.xmlNode + ptr unsafe.Pointer // *C.xmlNode mortal bool } @@ -64,7 +65,7 @@ type Element struct { } type Document struct { - ptr uintptr // *C.xmlDoc + ptr unsafe.Pointer // *C.xmlDoc mortal bool } diff --git a/dom/node.go b/dom/node.go index c262b01..e68ea27 100644 --- a/dom/node.go +++ b/dom/node.go @@ -1,6 +1,8 @@ package dom import ( + "unsafe" + "github.com/lestrrat-go/libxml2/clib" "github.com/lestrrat-go/libxml2/types" "github.com/lestrrat-go/libxml2/xpath" @@ -29,7 +31,7 @@ func (n *XMLNode) RemoveChild(t types.Node) error { } // Pointer returns the pointer to the underlying C struct -func (n *XMLNode) Pointer() uintptr { +func (n *XMLNode) Pointer() unsafe.Pointer { return n.ptr } @@ -45,7 +47,7 @@ func (n *XMLNode) OwnerDocument() (types.Document, error) { return nil, errors.Wrap(err, "failed to get valid owner document") } - if ptr == 0 { + if ptr == nil { return nil, errors.Wrap(clib.ErrInvalidDocument, "failed to get valid owner document") } return WrapDocument(ptr), nil @@ -169,7 +171,7 @@ func (n *XMLNode) NextSibling() (types.Node, error) { if err != nil { return nil, errors.Wrap(err, "failed to get valid pointer to next child") } - if ptr == 0 { + if ptr == nil { return nil, nil } return WrapNode(ptr) @@ -257,7 +259,7 @@ func (n *XMLNode) MakePersistent() { // Free releases the underlying C struct func (n *XMLNode) Free() { _ = clib.XMLFreeNode(n) - n.ptr = 0 + n.ptr = nil } func walk(n types.Node, fn func(types.Node) error) error { diff --git a/dom/node_document.go b/dom/node_document.go index 045f3cb..e6e3aea 100644 --- a/dom/node_document.go +++ b/dom/node_document.go @@ -1,6 +1,8 @@ package dom import ( + "unsafe" + "github.com/lestrrat-go/libxml2/clib" "github.com/lestrrat-go/libxml2/types" "github.com/pkg/errors" @@ -18,7 +20,7 @@ func NewDocument(version, encoding string) *Document { } // Pointer returns the pointer to the underlying C struct -func (d *Document) Pointer() uintptr { +func (d *Document) Pointer() unsafe.Pointer { return d.ptr } @@ -251,7 +253,7 @@ func (d *Document) Encoding() string { // Free releases the underlying C struct func (d *Document) Free() { _ = clib.XMLFreeDoc(d) - d.ptr = 0 + d.ptr = nil docPool.Put(d) } diff --git a/dom/node_namespace.go b/dom/node_namespace.go index 4f9595a..cf40fdd 100644 --- a/dom/node_namespace.go +++ b/dom/node_namespace.go @@ -17,5 +17,5 @@ func (n *Namespace) Prefix() string { // Free releases the underlying C struct func (n *Namespace) Free() { clib.XMLNamespaceFree(n) - n.ptr = 0 + n.ptr = nil } diff --git a/dom/node_wrap.go b/dom/node_wrap.go index 29dc199..17ff2fa 100644 --- a/dom/node_wrap.go +++ b/dom/node_wrap.go @@ -4,48 +4,49 @@ package dom import ( "fmt" + "unsafe" "github.com/lestrrat-go/libxml2/clib" "github.com/lestrrat-go/libxml2/types" ) -func wrapNamespaceNode(ptr uintptr) *Namespace { +func wrapNamespaceNode(ptr unsafe.Pointer) *Namespace { var n Namespace n.ptr = ptr return &n } -func wrapAttributeNode(ptr uintptr) *Attribute { +func wrapAttributeNode(ptr unsafe.Pointer) *Attribute { var n Attribute n.ptr = ptr return &n } -func wrapCDataSectionNode(ptr uintptr) *CDataSection { +func wrapCDataSectionNode(ptr unsafe.Pointer) *CDataSection { var n CDataSection n.ptr = ptr return &n } -func wrapCommentNode(ptr uintptr) *Comment { +func wrapCommentNode(ptr unsafe.Pointer) *Comment { var n Comment n.ptr = ptr return &n } -func wrapElementNode(ptr uintptr) *Element { +func wrapElementNode(ptr unsafe.Pointer) *Element { var n Element n.ptr = ptr return &n } -func wrapTextNode(ptr uintptr) *Text { +func wrapTextNode(ptr unsafe.Pointer) *Text { var n Text n.ptr = ptr return &n } -func wrapPiNode(ptr uintptr) *Pi { +func wrapPiNode(ptr unsafe.Pointer) *Pi { var n Pi n.ptr = ptr return &n @@ -54,7 +55,7 @@ func wrapPiNode(ptr uintptr) *Pi { // WrapNode is a function created with the sole purpose of allowing // go-libxml2 consumers that can generate a C.xmlNode pointer to // create libxml2.Node types, e.g. go-xmlsec. -func WrapNode(n uintptr) (types.Node, error) { +func WrapNode(n unsafe.Pointer) (types.Node, error) { switch typ := clib.XMLGetNodeTypeRaw(n); typ { case clib.AttributeNode: return wrapAttributeNode(n), nil diff --git a/html.go b/html.go index f4a2a40..ee5e4f9 100644 --- a/html.go +++ b/html.go @@ -31,7 +31,7 @@ func ParseHTMLString(content string, options ...parser.HTMLOption) (types.Docume return nil, errors.Wrap(err, "failed to read document") } - if docptr == 0 { + if docptr == nil { return nil, errors.Wrap(clib.ErrInvalidDocument, "failed to get valid document pointer") } return dom.WrapDocument(docptr), nil diff --git a/internal/cmd/genwrapnode/genwrapnode.go b/internal/cmd/genwrapnode/genwrapnode.go index 4ce6ca3..2962c24 100644 --- a/internal/cmd/genwrapnode/genwrapnode.go +++ b/internal/cmd/genwrapnode/genwrapnode.go @@ -26,6 +26,7 @@ func _main() error { buf.WriteString("\n\n// Auto-generated by internal/cmd/genwrapnode/genwrapnode.go. DO NOT EDIT!") buf.WriteString("\n\nimport (") buf.WriteString("\n\"fmt\"\n") + buf.WriteString("\n\"unsafe\"\n") for _, lib := range []string{"github.com/lestrrat-go/libxml2/clib", "github.com/lestrrat-go/libxml2/types"} { fmt.Fprintf(&buf, "\n%s", strconv.Quote(lib)) } @@ -42,7 +43,7 @@ func _main() error { } for _, typ := range nodeTypes { - fmt.Fprintf(&buf, "\n\nfunc wrap%sNode(ptr uintptr) *%s {", typ, typ) + fmt.Fprintf(&buf, "\n\nfunc wrap%sNode(ptr unsafe.Pointer) *%s {", typ, typ) fmt.Fprintf(&buf, "\nvar n %s", typ) buf.WriteString("\nn.ptr = ptr") buf.WriteString("\nreturn &n") @@ -52,7 +53,7 @@ func _main() error { buf.WriteString("\n\n// WrapNode is a function created with the sole purpose of allowing") buf.WriteString("\n// go-libxml2 consumers that can generate a C.xmlNode pointer to") buf.WriteString("\n// create libxml2.Node types, e.g. go-xmlsec.") - buf.WriteString("\nfunc WrapNode(n uintptr) (types.Node, error) {") + buf.WriteString("\nfunc WrapNode(n unsafe.Pointer) (types.Node, error) {") buf.WriteString("\nswitch typ := clib.XMLGetNodeTypeRaw(n); typ {") for _, typ := range nodeTypes { @@ -66,7 +67,7 @@ func _main() error { } buf.WriteString("\ndefault:") - buf.WriteString("\nreturn nil, fmt.Errorf(\"unknown node: %%d\", typ)") + buf.WriteString("\nreturn nil, fmt.Errorf(\"unknown node: %d\", typ)") buf.WriteString("\n}") buf.WriteString("\n}") diff --git a/parser/interface.go b/parser/interface.go index b6ae3bd..95e5656 100644 --- a/parser/interface.go +++ b/parser/interface.go @@ -1,6 +1,9 @@ package parser -import "errors" +import ( + "errors" + "unsafe" +) var ( // ErrMalformedXML is returned when the XML source is malformed @@ -73,7 +76,7 @@ const ( // Parser, but if you for some reason need to do more low-level // magic you will have to tinker with this struct type Ctxt struct { - ptr uintptr // *C.xmlParserCtxt + ptr unsafe.Pointer // *C.xmlParserCtxt } // Parser represents the high-level parser. diff --git a/parser/parser.go b/parser/parser.go index 9b707f3..ad8958f 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -3,6 +3,7 @@ package parser import ( "bytes" "io" + "unsafe" "github.com/lestrrat-go/libxml2/clib" "github.com/lestrrat-go/libxml2/dom" @@ -106,7 +107,7 @@ func (p *Parser) ParseString(s string) (types.Document, error) { return nil, errors.Wrap(err, "failed to create parse input") } - if docptr != 0 { + if docptr != nil { return dom.WrapDocument(docptr), nil } return nil, errors.New("failed to generate document pointer") @@ -132,7 +133,7 @@ func NewCtxt(s string, o Option) (*Ctxt, error) { } // Pointer returns the underlying C struct -func (ctx Ctxt) Pointer() uintptr { +func (ctx Ctxt) Pointer() unsafe.Pointer { return ctx.ptr } @@ -147,6 +148,6 @@ func (ctx *Ctxt) Free() error { return errors.Wrap(err, "failed to free parser context") } - ctx.ptr = 0 + ctx.ptr = nil return nil } diff --git a/types/interface.go b/types/interface.go index a79a715..4234dcb 100644 --- a/types/interface.go +++ b/types/interface.go @@ -1,6 +1,10 @@ package types -import "github.com/lestrrat-go/libxml2/clib" +import ( + "unsafe" + + "github.com/lestrrat-go/libxml2/clib" +) // PtrSource defines the interface for things that is backed by // a C backend @@ -9,7 +13,7 @@ type PtrSource interface { // method to allow various internal go-libxml2 packages to interoperate // on each other. End users are STRONGLY advised not to touch this // method or its return values - Pointer() uintptr + Pointer() unsafe.Pointer // Free releases the underlying resources Free() diff --git a/xpath/interface.go b/xpath/interface.go index 70585dd..6a4cb7f 100644 --- a/xpath/interface.go +++ b/xpath/interface.go @@ -1,6 +1,8 @@ package xpath import ( + "unsafe" + "github.com/lestrrat-go/libxml2/clib" "github.com/lestrrat-go/libxml2/types" ) @@ -21,7 +23,7 @@ const ( // Object is the concrete implementation of Result (types.XPathResult). // This struct contains the result of evaluating an XPath expression. type Object struct { - ptr uintptr // *C.xmlObject + ptr unsafe.Pointer // *C.xmlObject // This flag controls if the StringValue should use the *contents* (literal value) // of the nodeset instead of stringifying the node ForceLiteral bool @@ -30,12 +32,12 @@ type Object struct { // Context holds the current XPath context. You may register namespaces and // context nodes to evaluate your XPath expressions with it. type Context struct { - ptr uintptr // *C.xmlContext + ptr unsafe.Pointer // *C.xmlContext } // Expression is a compiled XPath expression type Expression struct { - ptr uintptr // *C.xmlCompExpr + ptr unsafe.Pointer // *C.xmlCompExpr // This exists mainly for debugging purposes expr string } diff --git a/xpath/iterator.go b/xpath/iterator.go index a2e248e..bb19339 100644 --- a/xpath/iterator.go +++ b/xpath/iterator.go @@ -1,6 +1,8 @@ package xpath import ( + "unsafe" + "github.com/lestrrat-go/libxml2/types" ) @@ -10,10 +12,10 @@ type NodeIterator struct { cur int curnode types.Node nlen int - nodes []uintptr + nodes []unsafe.Pointer } -func NewNodeIterator(nodes []uintptr) *NodeIterator { +func NewNodeIterator(nodes []unsafe.Pointer) *NodeIterator { return &NodeIterator{ cur: -1, nlen: len(nodes), diff --git a/xpath/xpath.go b/xpath/xpath.go index 98961a2..c370b36 100644 --- a/xpath/xpath.go +++ b/xpath/xpath.go @@ -22,6 +22,7 @@ package xpath import ( "fmt" + "unsafe" "github.com/lestrrat-go/libxml2/clib" "github.com/lestrrat-go/libxml2/types" @@ -29,7 +30,7 @@ import ( ) // Pointer returns the underlying C struct -func (x Object) Pointer() uintptr { +func (x Object) Pointer() unsafe.Pointer { return x.ptr } @@ -61,7 +62,7 @@ func (x Object) Bool() bool { // So this WrapNodeFunc is our workaround for this problem: when // github.com/lestrrat-go/libxml2/dom is loaded, it automatically // initializes this function to an appropriate function on the fly. -var WrapNodeFunc func(uintptr) (types.Node, error) +var WrapNodeFunc func(pointer unsafe.Pointer) (types.Node, error) // NodeList returns the list of nodes included in this Object func (x Object) NodeList() types.NodeList { @@ -133,7 +134,7 @@ func NewExpression(s string) (*Expression, error) { } // Pointer returns the underlying C struct -func (x *Expression) Pointer() uintptr { +func (x *Expression) Pointer() unsafe.Pointer { return x.ptr } @@ -167,7 +168,7 @@ func NewContext(n ...types.Node) (*Context, error) { } // Pointer returns a pointer to the underlying C struct -func (x *Context) Pointer() uintptr { +func (x *Context) Pointer() unsafe.Pointer { return x.ptr } diff --git a/xsd/interface.go b/xsd/interface.go index 6177f41..e40aa55 100644 --- a/xsd/interface.go +++ b/xsd/interface.go @@ -1,10 +1,14 @@ package xsd -import "github.com/lestrrat-go/libxml2/internal/option" +import ( + "unsafe" + + "github.com/lestrrat-go/libxml2/internal/option" +) // Schema represents an XML schema. type Schema struct { - ptr uintptr // *C.xmlSchema + ptr unsafe.Pointer // *C.xmlSchema } // SchemaValidationError is returned when the Validate() function diff --git a/xsd/xsd.go b/xsd/xsd.go index 81fdd5d..9067909 100644 --- a/xsd/xsd.go +++ b/xsd/xsd.go @@ -16,6 +16,8 @@ package xsd import ( + "unsafe" + "github.com/lestrrat-go/libxml2/clib" "github.com/lestrrat-go/libxml2/types" "github.com/pkg/errors" @@ -49,7 +51,7 @@ func ParseFromFile(path string) (*Schema, error) { } // Pointer returns the underlying C struct -func (s *Schema) Pointer() uintptr { +func (s *Schema) Pointer() unsafe.Pointer { return s.ptr } @@ -58,7 +60,7 @@ func (s *Schema) Free() { if err := clib.XMLSchemaFree(s); err != nil { return } - s.ptr = 0 + s.ptr = nil } // Validate takes in a XML document and validates it against