Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Fix uintptr usage in structs #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
330 changes: 165 additions & 165 deletions clib/clib.go

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions clib/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package clib

import "errors"
import (
"errors"
"unsafe"
)

const (
MaxEncodingLength = 256
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion dom/dom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dom

import (
"sync"
"unsafe"

"github.com/lestrrat-go/libxml2/xpath"
)
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions dom/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dom

import (
"errors"
"unsafe"

"github.com/lestrrat-go/libxml2/clib"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ const (
)

type XMLNode struct {
ptr uintptr // *C.xmlNode
ptr unsafe.Pointer // *C.xmlNode
mortal bool
}

Expand All @@ -64,7 +65,7 @@ type Element struct {
}

type Document struct {
ptr uintptr // *C.xmlDoc
ptr unsafe.Pointer // *C.xmlDoc
mortal bool
}

Expand Down
10 changes: 6 additions & 4 deletions dom/node.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions dom/node_document.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dom

import (
"unsafe"

"github.com/lestrrat-go/libxml2/clib"
"github.com/lestrrat-go/libxml2/types"
"github.com/pkg/errors"
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion dom/node_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
17 changes: 9 additions & 8 deletions dom/node_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/genwrapnode/genwrapnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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")
Expand All @@ -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 {
Expand All @@ -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}")

Expand Down
7 changes: 5 additions & 2 deletions parser/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package parser

import "errors"
import (
"errors"
"unsafe"
)

var (
// ErrMalformedXML is returned when the XML source is malformed
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"bytes"
"io"
"unsafe"

"github.com/lestrrat-go/libxml2/clib"
"github.com/lestrrat-go/libxml2/dom"
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand All @@ -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
}
8 changes: 6 additions & 2 deletions types/interface.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions xpath/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xpath

import (
"unsafe"

"github.com/lestrrat-go/libxml2/clib"
"github.com/lestrrat-go/libxml2/types"
)
Expand All @@ -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
Expand All @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions xpath/iterator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xpath

import (
"unsafe"

"github.com/lestrrat-go/libxml2/types"
)

Expand All @@ -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),
Expand Down
Loading
Loading