Skip to content

Commit

Permalink
Use proper error in packing and unpacking
Browse files Browse the repository at this point in the history
All the relevant functions now return an error instead of
a simple boolean. This greatly approves the feedback to coders.

Spotted some fishy error handling along the way and fix that too.
  • Loading branch information
miekg committed Oct 9, 2012
1 parent 099c19d commit 570bf8d
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 252 deletions.
21 changes: 10 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (c *Client) Exchange(m *Msg, a string) (r *Msg, err error) {
func (c *Client) ExchangeRtt(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
var n int
var w *reply
out, ok := m.Pack()
if !ok {
return nil, 0, ErrPack
out, err := m.Pack()
if err != nil {
return nil, 0, err
}
var in []byte
switch c.Net {
Expand All @@ -123,8 +123,8 @@ func (c *Client) ExchangeRtt(m *Msg, a string) (r *Msg, rtt time.Duration, err e
}
r = new(Msg)
r.Size = n
if ok := r.Unpack(in[:n]); !ok {
return nil, w.rtt, ErrUnpack
if err := r.Unpack(in[:n]); err != nil {
return nil, w.rtt, err
}
return r, w.rtt, nil
}
Expand Down Expand Up @@ -158,8 +158,8 @@ func (w *reply) receive() (*Msg, error) {
return nil, err
}
p = p[:n]
if ok := m.Unpack(p); !ok {
return nil, ErrUnpack
if err := m.Unpack(p); err != nil {
return nil, err
}
w.rtt = time.Since(w.t)
m.Size = n
Expand Down Expand Up @@ -260,10 +260,9 @@ func (w *reply) send(m *Msg) (err error) {
}
w.tsigRequestMAC = mac
} else {
ok := false
out, ok = m.Pack()
if !ok {
return ErrPack
out, err = m.Pack()
if err != nil {
return err
}
}
w.t = time.Now()
Expand Down
28 changes: 14 additions & 14 deletions dnssec.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ func (k *RR_DNSKEY) KeyTag() uint16 {
keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize)
n, ok := PackStruct(keywire, wire, 0)
if !ok {
n, err := PackStruct(keywire, wire, 0)
if err != nil {
return 0
}
wire = wire[:n]
Expand Down Expand Up @@ -157,15 +157,15 @@ func (k *RR_DNSKEY) ToDS(h int) *RR_DS {
keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize)
n, ok := PackStruct(keywire, wire, 0)
if !ok {
n, err := PackStruct(keywire, wire, 0)
if err != nil {
return nil
}
wire = wire[:n]

owner := make([]byte, 255)
off, ok1 := PackDomainName(k.Hdr.Name, owner, 0, nil, false)
if !ok1 {
off, err1 := PackDomainName(k.Hdr.Name, owner, 0, nil, false)
if err1 != nil {
return nil
}
owner = owner[:off]
Expand Down Expand Up @@ -237,9 +237,9 @@ func (rr *RR_RRSIG) Sign(k PrivateKey, rrset []RR) error {

// Create the desired binary blob
signdata := make([]byte, DefaultMsgSize)
n, ok := PackStruct(sigwire, signdata, 0)
if !ok {
return ErrPack
n, err := PackStruct(sigwire, signdata, 0)
if err != nil {
return err
}
signdata = signdata[:n]
wire := rawSignatureData(rrset, rr)
Expand Down Expand Up @@ -349,9 +349,9 @@ func (rr *RR_RRSIG) Verify(k *RR_DNSKEY, rrset []RR) error {
sigwire.SignerName = strings.ToLower(rr.SignerName)
// Create the desired binary blob
signeddata := make([]byte, DefaultMsgSize)
n, ok := PackStruct(sigwire, signeddata, 0)
if !ok {
return ErrPack
n, err := PackStruct(sigwire, signeddata, 0)
if err != nil {
return err
}
signeddata = signeddata[:n]
wire := rawSignatureData(rrset, rr)
Expand Down Expand Up @@ -684,8 +684,8 @@ func rawSignatureData(rrset []RR, s *RR_RRSIG) (buf []byte) {
}
// 6.2. Canonical RR Form. (5) - origTTL
wire := make([]byte, r.Len()*2)
off, ok1 := PackRR(r1, wire, 0, nil, false)
if !ok1 {
off, err1 := PackRR(r1, wire, 0, nil, false)
if err1 != nil {
return nil
}
wire = wire[:off]
Expand Down
Loading

0 comments on commit 570bf8d

Please sign in to comment.