From 75caa6bdbdcf2313d28bf6767192ba3cc9bbab30 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Fri, 1 May 2020 16:04:36 -0700 Subject: [PATCH] avoid calling the method WriteTo if we don't satisfy its contract --- cid.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cid.go b/cid.go index 9955b09..09f49f5 100644 --- a/cid.go +++ b/cid.go @@ -418,12 +418,19 @@ func (c Cid) ByteLen() int { return len(c.str) } -// WriteTo writes the CID bytes to the given writer. +// WriteBytes writes the CID bytes to the given writer. // This method works without incurring any allocation. // // (See also the ByteLen method for other important operations that work without allocation.) -func (c Cid) WriteTo(w io.Writer) (int, error) { - return io.WriteString(w, c.str) +func (c Cid) WriteBytes(w io.Writer) (int, error) { + n, err := io.WriteString(w, c.str) + if err != nil { + return n, err + } + if n != len(c.str) { + return n, fmt.Errorf("failed to write entire cid string") + } + return n, nil } // MarshalBinary is equivalent to Bytes(). It implements the -- GitLab