Skip to content

Commit

Permalink
Writer must not be nil
Browse files Browse the repository at this point in the history
  • Loading branch information
boecklim committed Jan 22, 2024
1 parent 5c3a05e commit 6dd2720
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions wire/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package wire

import (
"bytes"
"errors"
"fmt"
"io"
"unicode/utf8"
Expand Down Expand Up @@ -286,14 +287,14 @@ func discardInput(r io.Reader, n uint64) {
}

// WriteMessageN writes a bitcoin Message to w including the necessary header
// information and returns the number of bytes written. This function is the
// information and returns the number of bytes written. This function is the
// same as WriteMessage except it also returns the number of bytes written.
func WriteMessageN(w io.Writer, msg Message, pver uint32, bsvnet BitcoinNet) (int, error) {
return WriteMessageWithEncodingN(w, msg, pver, bsvnet, BaseEncoding)
}

// WriteMessage writes a bitcoin Message to w including the necessary header
// information. This function is the same as WriteMessageN except it doesn't
// information. This function is the same as WriteMessageN except it
// doesn't return the number of bytes written. This function is mainly provided
// for backwards compatibility with the original API, but it's also useful for
// callers that don't care about byte counts.
Expand All @@ -310,6 +311,10 @@ func WriteMessage(w io.Writer, msg Message, pver uint32, bsvnet BitcoinNet) erro
func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
bsvnet BitcoinNet, encoding MessageEncoding) (int, error) {

if w == nil {
return 0, errors.New("writer must not be nil")
}

totalBytes := 0

// Enforce max command size.
Expand Down Expand Up @@ -366,10 +371,8 @@ func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
// Write header and payload in 1 go.
// This w.Write() is locking, so we don't have to worry about concurrent writes.
var n int
if hw != nil {
n, err = w.Write(append(hw.Bytes(), payload...))
totalBytes += n
}
n, err = w.Write(append(hw.Bytes(), payload...))
totalBytes += n

return totalBytes, err
}
Expand All @@ -378,7 +381,7 @@ func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
// from r for the provided protocol version and bitcoin network. It returns the
// number of bytes read in addition to the parsed Message and raw bytes which
// comprise the message. This function is the same as ReadMessageN except it
// allows the caller to specify which message encoding is to to consult when
// allows the caller to specify which message encoding is to consult when
// decoding wire messages.
func ReadMessageWithEncodingN(r io.Reader, pver uint32, bsvnet BitcoinNet, enc MessageEncoding) (int, Message, []byte, error) {

Expand Down

0 comments on commit 6dd2720

Please sign in to comment.