Commit 351c15f1 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

all: remove public named return values when useless

Named returned values should only be used on public funcs and methods
when it contributes to the documentation.

Named return values should not be used if they're only saving the
programmer a few lines of code inside the body of the function,
especially if that means there's stutter in the documentation or it
was only there so the programmer could use a naked return
statement. (Naked returns should not be used except in very small
functions)

This change is a manual audit & cleanup of public func signatures.

Signatures were not changed if:

* the func was private (wouldn't be in public godoc)
* the documentation referenced it
* the named return value was an interesting name. (i.e. it wasn't
  simply stutter, repeating the name of the type)

There should be no changes in behavior. (At least: none intended)

Change-Id: I3472ef49619678fe786e5e0994bdf2d9de76d109
Reviewed-on: https://go-review.googlesource.com/20024
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarAndrew Gerrand <adg@golang.org>
parent 28ce6f36
...@@ -239,9 +239,9 @@ starts with the name being declared. ...@@ -239,9 +239,9 @@ starts with the name being declared.
</p> </p>
<pre> <pre>
// Compile parses a regular expression and returns, if successful, a Regexp // Compile parses a regular expression and returns, if successful,
// object that can be used to match against text. // a Regexp that can be used to match against text.
func Compile(str string) (regexp *Regexp, err error) { func Compile(str string) (*Regexp, error) {
</pre> </pre>
<p> <p>
......
...@@ -153,19 +153,18 @@ func (f *File) DataOffset() (offset int64, err error) { ...@@ -153,19 +153,18 @@ func (f *File) DataOffset() (offset int64, err error) {
// Open returns a ReadCloser that provides access to the File's contents. // Open returns a ReadCloser that provides access to the File's contents.
// Multiple files may be read concurrently. // Multiple files may be read concurrently.
func (f *File) Open() (rc io.ReadCloser, err error) { func (f *File) Open() (io.ReadCloser, error) {
bodyOffset, err := f.findBodyOffset() bodyOffset, err := f.findBodyOffset()
if err != nil { if err != nil {
return return nil, err
} }
size := int64(f.CompressedSize64) size := int64(f.CompressedSize64)
r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size) r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size)
dcomp := f.zip.decompressor(f.Method) dcomp := f.zip.decompressor(f.Method)
if dcomp == nil { if dcomp == nil {
err = ErrAlgorithm return nil, ErrAlgorithm
return
} }
rc = dcomp(r) var rc io.ReadCloser = dcomp(r)
var desr io.Reader var desr io.Reader
if f.hasDataDescriptor() { if f.hasDataDescriptor() {
desr = io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset+size, dataDescriptorLen) desr = io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset+size, dataDescriptorLen)
...@@ -176,7 +175,7 @@ func (f *File) Open() (rc io.ReadCloser, err error) { ...@@ -176,7 +175,7 @@ func (f *File) Open() (rc io.ReadCloser, err error) {
f: f, f: f,
desr: desr, desr: desr,
} }
return return rc, nil
} }
type checksumReader struct { type checksumReader struct {
......
...@@ -220,7 +220,7 @@ func (b *Reader) Read(p []byte) (n int, err error) { ...@@ -220,7 +220,7 @@ func (b *Reader) Read(p []byte) (n int, err error) {
// ReadByte reads and returns a single byte. // ReadByte reads and returns a single byte.
// If no byte is available, returns an error. // If no byte is available, returns an error.
func (b *Reader) ReadByte() (c byte, err error) { func (b *Reader) ReadByte() (byte, error) {
b.lastRuneSize = -1 b.lastRuneSize = -1
for b.r == b.w { for b.r == b.w {
if b.err != nil { if b.err != nil {
...@@ -228,7 +228,7 @@ func (b *Reader) ReadByte() (c byte, err error) { ...@@ -228,7 +228,7 @@ func (b *Reader) ReadByte() (c byte, err error) {
} }
b.fill() // buffer is empty b.fill() // buffer is empty
} }
c = b.buf[b.r] c := b.buf[b.r]
b.r++ b.r++
b.lastByte = int(c) b.lastByte = int(c)
return c, nil return c, nil
...@@ -395,12 +395,12 @@ func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) { ...@@ -395,12 +395,12 @@ func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) {
// ReadBytes returns err != nil if and only if the returned data does not end in // ReadBytes returns err != nil if and only if the returned data does not end in
// delim. // delim.
// For simple uses, a Scanner may be more convenient. // For simple uses, a Scanner may be more convenient.
func (b *Reader) ReadBytes(delim byte) (line []byte, err error) { func (b *Reader) ReadBytes(delim byte) ([]byte, error) {
// Use ReadSlice to look for array, // Use ReadSlice to look for array,
// accumulating full buffers. // accumulating full buffers.
var frag []byte var frag []byte
var full [][]byte var full [][]byte
var err error
for { for {
var e error var e error
frag, e = b.ReadSlice(delim) frag, e = b.ReadSlice(delim)
...@@ -442,10 +442,9 @@ func (b *Reader) ReadBytes(delim byte) (line []byte, err error) { ...@@ -442,10 +442,9 @@ func (b *Reader) ReadBytes(delim byte) (line []byte, err error) {
// ReadString returns err != nil if and only if the returned data does not end in // ReadString returns err != nil if and only if the returned data does not end in
// delim. // delim.
// For simple uses, a Scanner may be more convenient. // For simple uses, a Scanner may be more convenient.
func (b *Reader) ReadString(delim byte) (line string, err error) { func (b *Reader) ReadString(delim byte) (string, error) {
bytes, err := b.ReadBytes(delim) bytes, err := b.ReadBytes(delim)
line = string(bytes) return string(bytes), err
return line, err
} }
// WriteTo implements io.WriterTo. // WriteTo implements io.WriterTo.
......
...@@ -293,14 +293,14 @@ func (b *Buffer) Next(n int) []byte { ...@@ -293,14 +293,14 @@ func (b *Buffer) Next(n int) []byte {
// ReadByte reads and returns the next byte from the buffer. // ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error io.EOF. // If no byte is available, it returns error io.EOF.
func (b *Buffer) ReadByte() (c byte, err error) { func (b *Buffer) ReadByte() (byte, error) {
b.lastRead = opInvalid b.lastRead = opInvalid
if b.off >= len(b.buf) { if b.off >= len(b.buf) {
// Buffer is empty, reset to recover space. // Buffer is empty, reset to recover space.
b.Truncate(0) b.Truncate(0)
return 0, io.EOF return 0, io.EOF
} }
c = b.buf[b.off] c := b.buf[b.off]
b.off++ b.off++
b.lastRead = opRead b.lastRead = opRead
return c, nil return c, nil
......
...@@ -63,14 +63,14 @@ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { ...@@ -63,14 +63,14 @@ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
return return
} }
func (r *Reader) ReadByte() (b byte, err error) { func (r *Reader) ReadByte() (byte, error) {
r.prevRune = -1 r.prevRune = -1
if r.i >= int64(len(r.s)) { if r.i >= int64(len(r.s)) {
return 0, io.EOF return 0, io.EOF
} }
b = r.s[r.i] b := r.s[r.i]
r.i++ r.i++
return return b, nil
} }
func (r *Reader) UnreadByte() error { func (r *Reader) UnreadByte() error {
......
...@@ -52,7 +52,7 @@ const numMRTests = 64 ...@@ -52,7 +52,7 @@ const numMRTests = 64
// GenerateParameters puts a random, valid set of DSA parameters into params. // GenerateParameters puts a random, valid set of DSA parameters into params.
// This function can take many seconds, even on fast machines. // This function can take many seconds, even on fast machines.
func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) (err error) { func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) error {
// This function doesn't follow FIPS 186-3 exactly in that it doesn't // This function doesn't follow FIPS 186-3 exactly in that it doesn't
// use a verification seed to generate the primes. The verification // use a verification seed to generate the primes. The verification
// seed doesn't appear to be exported or used by other code and // seed doesn't appear to be exported or used by other code and
...@@ -87,9 +87,8 @@ func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes ...@@ -87,9 +87,8 @@ func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes
GeneratePrimes: GeneratePrimes:
for { for {
_, err = io.ReadFull(rand, qBytes) if _, err := io.ReadFull(rand, qBytes); err != nil {
if err != nil { return err
return
} }
qBytes[len(qBytes)-1] |= 1 qBytes[len(qBytes)-1] |= 1
...@@ -101,9 +100,8 @@ GeneratePrimes: ...@@ -101,9 +100,8 @@ GeneratePrimes:
} }
for i := 0; i < 4*L; i++ { for i := 0; i < 4*L; i++ {
_, err = io.ReadFull(rand, pBytes) if _, err := io.ReadFull(rand, pBytes); err != nil {
if err != nil { return err
return
} }
pBytes[len(pBytes)-1] |= 1 pBytes[len(pBytes)-1] |= 1
...@@ -142,7 +140,7 @@ GeneratePrimes: ...@@ -142,7 +140,7 @@ GeneratePrimes:
} }
params.G = g params.G = g
return return nil
} }
} }
......
...@@ -96,17 +96,17 @@ func randFieldElement(c elliptic.Curve, rand io.Reader) (k *big.Int, err error) ...@@ -96,17 +96,17 @@ func randFieldElement(c elliptic.Curve, rand io.Reader) (k *big.Int, err error)
} }
// GenerateKey generates a public and private key pair. // GenerateKey generates a public and private key pair.
func GenerateKey(c elliptic.Curve, rand io.Reader) (priv *PrivateKey, err error) { func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error) {
k, err := randFieldElement(c, rand) k, err := randFieldElement(c, rand)
if err != nil { if err != nil {
return return nil, err
} }
priv = new(PrivateKey) priv := new(PrivateKey)
priv.PublicKey.Curve = c priv.PublicKey.Curve = c
priv.D = k priv.D = k
priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes()) priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
return return priv, nil
} }
// hashToInt converts a hash value to an integer. There is some disagreement // hashToInt converts a hash value to an integer. There is some disagreement
......
...@@ -24,31 +24,32 @@ type PKCS1v15DecryptOptions struct { ...@@ -24,31 +24,32 @@ type PKCS1v15DecryptOptions struct {
SessionKeyLen int SessionKeyLen int
} }
// EncryptPKCS1v15 encrypts the given message with RSA and the padding scheme from PKCS#1 v1.5. // EncryptPKCS1v15 encrypts the given message with RSA and the padding
// The message must be no longer than the length of the public modulus minus 11 bytes. // scheme from PKCS#1 v1.5. The message must be no longer than the
// length of the public modulus minus 11 bytes.
// //
// The rand parameter is used as a source of entropy to ensure that encrypting // The rand parameter is used as a source of entropy to ensure that
// the same message twice doesn't result in the same ciphertext. // encrypting the same message twice doesn't result in the same
// ciphertext.
// //
// WARNING: use of this function to encrypt plaintexts other than session keys // WARNING: use of this function to encrypt plaintexts other than
// is dangerous. Use RSA OAEP in new protocols. // session keys is dangerous. Use RSA OAEP in new protocols.
func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, err error) { func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) ([]byte, error) {
if err := checkPub(pub); err != nil { if err := checkPub(pub); err != nil {
return nil, err return nil, err
} }
k := (pub.N.BitLen() + 7) / 8 k := (pub.N.BitLen() + 7) / 8
if len(msg) > k-11 { if len(msg) > k-11 {
err = ErrMessageTooLong return nil, ErrMessageTooLong
return
} }
// EM = 0x00 || 0x02 || PS || 0x00 || M // EM = 0x00 || 0x02 || PS || 0x00 || M
em := make([]byte, k) em := make([]byte, k)
em[1] = 2 em[1] = 2
ps, mm := em[2:len(em)-len(msg)-1], em[len(em)-len(msg):] ps, mm := em[2:len(em)-len(msg)-1], em[len(em)-len(msg):]
err = nonZeroRandomBytes(ps, rand) err := nonZeroRandomBytes(ps, rand)
if err != nil { if err != nil {
return return nil, err
} }
em[len(em)-len(msg)-1] = 0 em[len(em)-len(msg)-1] = 0
copy(mm, msg) copy(mm, msg)
...@@ -57,8 +58,7 @@ func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, er ...@@ -57,8 +58,7 @@ func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, er
c := encrypt(new(big.Int), pub, m) c := encrypt(new(big.Int), pub, m)
copyWithLeftPad(em, c.Bytes()) copyWithLeftPad(em, c.Bytes())
out = em return em, nil
return
} }
// DecryptPKCS1v15 decrypts a plaintext using RSA and the padding scheme from PKCS#1 v1.5. // DecryptPKCS1v15 decrypts a plaintext using RSA and the padding scheme from PKCS#1 v1.5.
...@@ -69,19 +69,18 @@ func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, er ...@@ -69,19 +69,18 @@ func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, er
// learn whether each instance returned an error then they can decrypt and // learn whether each instance returned an error then they can decrypt and
// forge signatures as if they had the private key. See // forge signatures as if they had the private key. See
// DecryptPKCS1v15SessionKey for a way of solving this problem. // DecryptPKCS1v15SessionKey for a way of solving this problem.
func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (out []byte, err error) { func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) ([]byte, error) {
if err := checkPub(&priv.PublicKey); err != nil { if err := checkPub(&priv.PublicKey); err != nil {
return nil, err return nil, err
} }
valid, out, index, err := decryptPKCS1v15(rand, priv, ciphertext) valid, out, index, err := decryptPKCS1v15(rand, priv, ciphertext)
if err != nil { if err != nil {
return return nil, err
} }
if valid == 0 { if valid == 0 {
return nil, ErrDecryption return nil, ErrDecryption
} }
out = out[index:] return out[index:], nil
return
} }
// DecryptPKCS1v15SessionKey decrypts a session key using RSA and the padding scheme from PKCS#1 v1.5. // DecryptPKCS1v15SessionKey decrypts a session key using RSA and the padding scheme from PKCS#1 v1.5.
...@@ -103,7 +102,7 @@ func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (out [ ...@@ -103,7 +102,7 @@ func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (out [
// a random value was used (because it'll be different for the same ciphertext) // a random value was used (because it'll be different for the same ciphertext)
// and thus whether the padding was correct. This defeats the point of this // and thus whether the padding was correct. This defeats the point of this
// function. Using at least a 16-byte key will protect against this attack. // function. Using at least a 16-byte key will protect against this attack.
func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) (err error) { func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) error {
if err := checkPub(&priv.PublicKey); err != nil { if err := checkPub(&priv.PublicKey); err != nil {
return err return err
} }
...@@ -114,7 +113,7 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by ...@@ -114,7 +113,7 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by
valid, em, index, err := decryptPKCS1v15(rand, priv, ciphertext) valid, em, index, err := decryptPKCS1v15(rand, priv, ciphertext)
if err != nil { if err != nil {
return return err
} }
if len(em) != k { if len(em) != k {
...@@ -125,7 +124,7 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by ...@@ -125,7 +124,7 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by
valid &= subtle.ConstantTimeEq(int32(len(em)-index), int32(len(key))) valid &= subtle.ConstantTimeEq(int32(len(em)-index), int32(len(key)))
subtle.ConstantTimeCopy(valid, key, em[len(em)-len(key):]) subtle.ConstantTimeCopy(valid, key, em[len(em)-len(key):])
return return nil
} }
// decryptPKCS1v15 decrypts ciphertext using priv and blinds the operation if // decryptPKCS1v15 decrypts ciphertext using priv and blinds the operation if
...@@ -213,21 +212,23 @@ var hashPrefixes = map[crypto.Hash][]byte{ ...@@ -213,21 +212,23 @@ var hashPrefixes = map[crypto.Hash][]byte{
crypto.RIPEMD160: {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31, 0x04, 0x14}, crypto.RIPEMD160: {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31, 0x04, 0x14},
} }
// SignPKCS1v15 calculates the signature of hashed using RSASSA-PKCS1-V1_5-SIGN from RSA PKCS#1 v1.5. // SignPKCS1v15 calculates the signature of hashed using
// Note that hashed must be the result of hashing the input message using the // RSASSA-PKCS1-V1_5-SIGN from RSA PKCS#1 v1.5. Note that hashed must
// given hash function. If hash is zero, hashed is signed directly. This isn't // be the result of hashing the input message using the given hash
// function. If hash is zero, hashed is signed directly. This isn't
// advisable except for interoperability. // advisable except for interoperability.
// //
// If rand is not nil then RSA blinding will be used to avoid timing side-channel attacks. // If rand is not nil then RSA blinding will be used to avoid timing
// side-channel attacks.
// //
// This function is deterministic. Thus, if the set of possible messages is // This function is deterministic. Thus, if the set of possible
// small, an attacker may be able to build a map from messages to signatures // messages is small, an attacker may be able to build a map from
// and identify the signed messages. As ever, signatures provide authenticity, // messages to signatures and identify the signed messages. As ever,
// not confidentiality. // signatures provide authenticity, not confidentiality.
func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) (s []byte, err error) { func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error) {
hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed)) hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
if err != nil { if err != nil {
return return nil, err
} }
tLen := len(prefix) + hashLen tLen := len(prefix) + hashLen
...@@ -248,12 +249,11 @@ func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []b ...@@ -248,12 +249,11 @@ func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []b
m := new(big.Int).SetBytes(em) m := new(big.Int).SetBytes(em)
c, err := decryptAndCheck(rand, priv, m) c, err := decryptAndCheck(rand, priv, m)
if err != nil { if err != nil {
return return nil, err
} }
copyWithLeftPad(em, c.Bytes()) copyWithLeftPad(em, c.Bytes())
s = em return em, nil
return
} }
// VerifyPKCS1v15 verifies an RSA PKCS#1 v1.5 signature. // VerifyPKCS1v15 verifies an RSA PKCS#1 v1.5 signature.
...@@ -261,17 +261,16 @@ func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []b ...@@ -261,17 +261,16 @@ func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []b
// function and sig is the signature. A valid signature is indicated by // function and sig is the signature. A valid signature is indicated by
// returning a nil error. If hash is zero then hashed is used directly. This // returning a nil error. If hash is zero then hashed is used directly. This
// isn't advisable except for interoperability. // isn't advisable except for interoperability.
func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) (err error) { func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed)) hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
if err != nil { if err != nil {
return return err
} }
tLen := len(prefix) + hashLen tLen := len(prefix) + hashLen
k := (pub.N.BitLen() + 7) / 8 k := (pub.N.BitLen() + 7) / 8
if k < tLen+11 { if k < tLen+11 {
err = ErrVerification return ErrVerification
return
} }
c := new(big.Int).SetBytes(sig) c := new(big.Int).SetBytes(sig)
......
...@@ -246,7 +246,7 @@ func (opts *PSSOptions) saltLength() int { ...@@ -246,7 +246,7 @@ func (opts *PSSOptions) saltLength() int {
// Note that hashed must be the result of hashing the input message using the // Note that hashed must be the result of hashing the input message using the
// given hash function. The opts argument may be nil, in which case sensible // given hash function. The opts argument may be nil, in which case sensible
// defaults are used. // defaults are used.
func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte, opts *PSSOptions) (s []byte, err error) { func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte, opts *PSSOptions) ([]byte, error) {
saltLength := opts.saltLength() saltLength := opts.saltLength()
switch saltLength { switch saltLength {
case PSSSaltLengthAuto: case PSSSaltLengthAuto:
...@@ -260,8 +260,8 @@ func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte, ...@@ -260,8 +260,8 @@ func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte,
} }
salt := make([]byte, saltLength) salt := make([]byte, saltLength)
if _, err = io.ReadFull(rand, salt); err != nil { if _, err := io.ReadFull(rand, salt); err != nil {
return return nil, err
} }
return signPSSWithSalt(rand, priv, hash, hashed, salt) return signPSSWithSalt(rand, priv, hash, hashed, salt)
} }
......
...@@ -191,7 +191,7 @@ func (priv *PrivateKey) Validate() error { ...@@ -191,7 +191,7 @@ func (priv *PrivateKey) Validate() error {
// GenerateKey generates an RSA keypair of the given bit size using the // GenerateKey generates an RSA keypair of the given bit size using the
// random source random (for example, crypto/rand.Reader). // random source random (for example, crypto/rand.Reader).
func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error) { func GenerateKey(random io.Reader, bits int) (*PrivateKey, error) {
return GenerateMultiPrimeKey(random, 2, bits) return GenerateMultiPrimeKey(random, 2, bits)
} }
...@@ -206,8 +206,8 @@ func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error) { ...@@ -206,8 +206,8 @@ func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error) {
// //
// [1] US patent 4405829 (1972, expired) // [1] US patent 4405829 (1972, expired)
// [2] http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf // [2] http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf
func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (priv *PrivateKey, err error) { func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (*PrivateKey, error) {
priv = new(PrivateKey) priv := new(PrivateKey)
priv.E = 65537 priv.E = 65537
if nprimes < 2 { if nprimes < 2 {
...@@ -234,6 +234,7 @@ NextSetOfPrimes: ...@@ -234,6 +234,7 @@ NextSetOfPrimes:
todo += (nprimes - 2) / 5 todo += (nprimes - 2) / 5
} }
for i := 0; i < nprimes; i++ { for i := 0; i < nprimes; i++ {
var err error
primes[i], err = rand.Prime(random, todo/(nprimes-i)) primes[i], err = rand.Prime(random, todo/(nprimes-i))
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -283,7 +284,7 @@ NextSetOfPrimes: ...@@ -283,7 +284,7 @@ NextSetOfPrimes:
} }
priv.Precompute() priv.Precompute()
return return priv, nil
} }
// incCounter increments a four byte, big-endian counter. // incCounter increments a four byte, big-endian counter.
...@@ -348,15 +349,14 @@ func encrypt(c *big.Int, pub *PublicKey, m *big.Int) *big.Int { ...@@ -348,15 +349,14 @@ func encrypt(c *big.Int, pub *PublicKey, m *big.Int) *big.Int {
// //
// The message must be no longer than the length of the public modulus less // The message must be no longer than the length of the public modulus less
// twice the hash length plus 2. // twice the hash length plus 2.
func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) (out []byte, err error) { func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) ([]byte, error) {
if err := checkPub(pub); err != nil { if err := checkPub(pub); err != nil {
return nil, err return nil, err
} }
hash.Reset() hash.Reset()
k := (pub.N.BitLen() + 7) / 8 k := (pub.N.BitLen() + 7) / 8
if len(msg) > k-2*hash.Size()-2 { if len(msg) > k-2*hash.Size()-2 {
err = ErrMessageTooLong return nil, ErrMessageTooLong
return
} }
hash.Write(label) hash.Write(label)
...@@ -371,9 +371,9 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l ...@@ -371,9 +371,9 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l
db[len(db)-len(msg)-1] = 1 db[len(db)-len(msg)-1] = 1
copy(db[len(db)-len(msg):], msg) copy(db[len(db)-len(msg):], msg)
_, err = io.ReadFull(random, seed) _, err := io.ReadFull(random, seed)
if err != nil { if err != nil {
return return nil, err
} }
mgf1XOR(db, hash, seed) mgf1XOR(db, hash, seed)
...@@ -382,7 +382,7 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l ...@@ -382,7 +382,7 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l
m := new(big.Int) m := new(big.Int)
m.SetBytes(em) m.SetBytes(em)
c := encrypt(new(big.Int), pub, m) c := encrypt(new(big.Int), pub, m)
out = c.Bytes() out := c.Bytes()
if len(out) < k { if len(out) < k {
// If the output is too small, we need to left-pad with zeros. // If the output is too small, we need to left-pad with zeros.
...@@ -391,7 +391,7 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l ...@@ -391,7 +391,7 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l
out = t out = t
} }
return return out, nil
} }
// ErrDecryption represents a failure to decrypt a message. // ErrDecryption represents a failure to decrypt a message.
...@@ -562,22 +562,21 @@ func decryptAndCheck(random io.Reader, priv *PrivateKey, c *big.Int) (m *big.Int ...@@ -562,22 +562,21 @@ func decryptAndCheck(random io.Reader, priv *PrivateKey, c *big.Int) (m *big.Int
// //
// The label parameter must match the value given when encrypting. See // The label parameter must match the value given when encrypting. See
// EncryptOAEP for details. // EncryptOAEP for details.
func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext []byte, label []byte) (msg []byte, err error) { func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext []byte, label []byte) ([]byte, error) {
if err := checkPub(&priv.PublicKey); err != nil { if err := checkPub(&priv.PublicKey); err != nil {
return nil, err return nil, err
} }
k := (priv.N.BitLen() + 7) / 8 k := (priv.N.BitLen() + 7) / 8
if len(ciphertext) > k || if len(ciphertext) > k ||
k < hash.Size()*2+2 { k < hash.Size()*2+2 {
err = ErrDecryption return nil, ErrDecryption
return
} }
c := new(big.Int).SetBytes(ciphertext) c := new(big.Int).SetBytes(ciphertext)
m, err := decrypt(random, priv, c) m, err := decrypt(random, priv, c)
if err != nil { if err != nil {
return return nil, err
} }
hash.Write(label) hash.Write(label)
...@@ -625,12 +624,10 @@ func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext ...@@ -625,12 +624,10 @@ func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext
} }
if firstByteIsZero&lHash2Good&^invalid&^lookingForIndex != 1 { if firstByteIsZero&lHash2Good&^invalid&^lookingForIndex != 1 {
err = ErrDecryption return nil, ErrDecryption
return
} }
msg = rest[index+1:] return rest[index+1:], nil
return
} }
// leftPad returns a new slice of length size. The contents of input are right // leftPad returns a new slice of length size. The contents of input are right
......
...@@ -47,14 +47,13 @@ type listener struct { ...@@ -47,14 +47,13 @@ type listener struct {
} }
// Accept waits for and returns the next incoming TLS connection. // Accept waits for and returns the next incoming TLS connection.
// The returned connection c is a *tls.Conn. // The returned connection is of type *Conn.
func (l *listener) Accept() (c net.Conn, err error) { func (l *listener) Accept() (net.Conn, error) {
c, err = l.Listener.Accept() c, err := l.Listener.Accept()
if err != nil { if err != nil {
return return nil, err
} }
c = Server(c, l.config) return Server(c, l.config), nil
return
} }
// NewListener creates a Listener which accepts connections from an inner // NewListener creates a Listener which accepts connections from an inner
......
...@@ -36,15 +36,14 @@ type pkcs1AdditionalRSAPrime struct { ...@@ -36,15 +36,14 @@ type pkcs1AdditionalRSAPrime struct {
} }
// ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form. // ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
func ParsePKCS1PrivateKey(der []byte) (key *rsa.PrivateKey, err error) { func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
var priv pkcs1PrivateKey var priv pkcs1PrivateKey
rest, err := asn1.Unmarshal(der, &priv) rest, err := asn1.Unmarshal(der, &priv)
if len(rest) > 0 { if len(rest) > 0 {
err = asn1.SyntaxError{Msg: "trailing data"} return nil, asn1.SyntaxError{Msg: "trailing data"}
return
} }
if err != nil { if err != nil {
return return nil, err
} }
if priv.Version > 1 { if priv.Version > 1 {
...@@ -55,7 +54,7 @@ func ParsePKCS1PrivateKey(der []byte) (key *rsa.PrivateKey, err error) { ...@@ -55,7 +54,7 @@ func ParsePKCS1PrivateKey(der []byte) (key *rsa.PrivateKey, err error) {
return nil, errors.New("x509: private key contains zero or negative value") return nil, errors.New("x509: private key contains zero or negative value")
} }
key = new(rsa.PrivateKey) key := new(rsa.PrivateKey)
key.PublicKey = rsa.PublicKey{ key.PublicKey = rsa.PublicKey{
E: priv.E, E: priv.E,
N: priv.N, N: priv.N,
...@@ -80,7 +79,7 @@ func ParsePKCS1PrivateKey(der []byte) (key *rsa.PrivateKey, err error) { ...@@ -80,7 +79,7 @@ func ParsePKCS1PrivateKey(der []byte) (key *rsa.PrivateKey, err error) {
} }
key.Precompute() key.Precompute()
return return key, nil
} }
// MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form. // MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.
......
...@@ -29,7 +29,7 @@ type ecPrivateKey struct { ...@@ -29,7 +29,7 @@ type ecPrivateKey struct {
} }
// ParseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure. // ParseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
func ParseECPrivateKey(der []byte) (key *ecdsa.PrivateKey, err error) { func ParseECPrivateKey(der []byte) (*ecdsa.PrivateKey, error) {
return parseECPrivateKey(nil, der) return parseECPrivateKey(nil, der)
} }
...@@ -41,8 +41,8 @@ func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) { ...@@ -41,8 +41,8 @@ func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) {
} }
privateKeyBytes := key.D.Bytes() privateKeyBytes := key.D.Bytes()
paddedPrivateKey := make([]byte, (key.Curve.Params().N.BitLen() + 7) / 8) paddedPrivateKey := make([]byte, (key.Curve.Params().N.BitLen()+7)/8)
copy(paddedPrivateKey[len(paddedPrivateKey) - len(privateKeyBytes):], privateKeyBytes) copy(paddedPrivateKey[len(paddedPrivateKey)-len(privateKeyBytes):], privateKeyBytes)
return asn1.Marshal(ecPrivateKey{ return asn1.Marshal(ecPrivateKey{
Version: 1, Version: 1,
...@@ -84,7 +84,7 @@ func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *e ...@@ -84,7 +84,7 @@ func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *e
priv.Curve = curve priv.Curve = curve
priv.D = k priv.D = k
privateKey := make([]byte, (curveOrder.BitLen() + 7) / 8) privateKey := make([]byte, (curveOrder.BitLen()+7)/8)
// Some private keys have leading zero padding. This is invalid // Some private keys have leading zero padding. This is invalid
// according to [SEC1], but this code will ignore it. // according to [SEC1], but this code will ignore it.
...@@ -98,7 +98,7 @@ func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *e ...@@ -98,7 +98,7 @@ func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *e
// Some private keys remove all leading zeros, this is also invalid // Some private keys remove all leading zeros, this is also invalid
// according to [SEC1] but since OpenSSL used to do this, we ignore // according to [SEC1] but since OpenSSL used to do this, we ignore
// this too. // this too.
copy(privateKey[len(privateKey) - len(privKey.PrivateKey):], privKey.PrivateKey) copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey)
priv.X, priv.Y = curve.ScalarBaseMult(privateKey) priv.X, priv.Y = curve.ScalarBaseMult(privateKey)
return priv, nil return priv, nil
......
...@@ -641,7 +641,7 @@ var entrustBrokenSPKI = []byte{ ...@@ -641,7 +641,7 @@ var entrustBrokenSPKI = []byte{
// CheckSignatureFrom verifies that the signature on c is a valid signature // CheckSignatureFrom verifies that the signature on c is a valid signature
// from parent. // from parent.
func (c *Certificate) CheckSignatureFrom(parent *Certificate) (err error) { func (c *Certificate) CheckSignatureFrom(parent *Certificate) error {
// RFC 5280, 4.2.1.9: // RFC 5280, 4.2.1.9:
// "If the basic constraints extension is not present in a version 3 // "If the basic constraints extension is not present in a version 3
// certificate, or the extension is present but the cA boolean is not // certificate, or the extension is present but the cA boolean is not
...@@ -669,7 +669,7 @@ func (c *Certificate) CheckSignatureFrom(parent *Certificate) (err error) { ...@@ -669,7 +669,7 @@ func (c *Certificate) CheckSignatureFrom(parent *Certificate) (err error) {
// CheckSignature verifies that signature is a valid signature over signed from // CheckSignature verifies that signature is a valid signature over signed from
// c's public key. // c's public key.
func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) (err error) { func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) error {
return checkSignature(algo, signed, signature, c.PublicKey) return checkSignature(algo, signed, signature, c.PublicKey)
} }
...@@ -737,7 +737,7 @@ func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey ...@@ -737,7 +737,7 @@ func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey
} }
// CheckCRLSignature checks that the signature in crl is from c. // CheckCRLSignature checks that the signature in crl is from c.
func (c *Certificate) CheckCRLSignature(crl *pkix.CertificateList) (err error) { func (c *Certificate) CheckCRLSignature(crl *pkix.CertificateList) error {
algo := getSignatureAlgorithmFromOID(crl.SignatureAlgorithm.Algorithm) algo := getSignatureAlgorithmFromOID(crl.SignatureAlgorithm.Algorithm)
return c.CheckSignature(algo, crl.TBSCertList.Raw, crl.SignatureValue.RightAlign()) return c.CheckSignature(algo, crl.TBSCertList.Raw, crl.SignatureValue.RightAlign())
} }
...@@ -1654,7 +1654,7 @@ var pemType = "X509 CRL" ...@@ -1654,7 +1654,7 @@ var pemType = "X509 CRL"
// encoded CRLs will appear where they should be DER encoded, so this function // encoded CRLs will appear where they should be DER encoded, so this function
// will transparently handle PEM encoding as long as there isn't any leading // will transparently handle PEM encoding as long as there isn't any leading
// garbage. // garbage.
func ParseCRL(crlBytes []byte) (certList *pkix.CertificateList, err error) { func ParseCRL(crlBytes []byte) (*pkix.CertificateList, error) {
if bytes.HasPrefix(crlBytes, pemCRLPrefix) { if bytes.HasPrefix(crlBytes, pemCRLPrefix) {
block, _ := pem.Decode(crlBytes) block, _ := pem.Decode(crlBytes)
if block != nil && block.Type == pemType { if block != nil && block.Type == pemType {
...@@ -1665,8 +1665,8 @@ func ParseCRL(crlBytes []byte) (certList *pkix.CertificateList, err error) { ...@@ -1665,8 +1665,8 @@ func ParseCRL(crlBytes []byte) (certList *pkix.CertificateList, err error) {
} }
// ParseDERCRL parses a DER encoded CRL from the given bytes. // ParseDERCRL parses a DER encoded CRL from the given bytes.
func ParseDERCRL(derBytes []byte) (certList *pkix.CertificateList, err error) { func ParseDERCRL(derBytes []byte) (*pkix.CertificateList, error) {
certList = new(pkix.CertificateList) certList := new(pkix.CertificateList)
if rest, err := asn1.Unmarshal(derBytes, certList); err != nil { if rest, err := asn1.Unmarshal(derBytes, certList); err != nil {
return nil, err return nil, err
} else if len(rest) != 0 { } else if len(rest) != 0 {
...@@ -2071,7 +2071,7 @@ func parseCertificateRequest(in *certificateRequest) (*CertificateRequest, error ...@@ -2071,7 +2071,7 @@ func parseCertificateRequest(in *certificateRequest) (*CertificateRequest, error
return out, nil return out, nil
} }
// CheckSignature verifies that the signature on c is a valid signature // CheckSignature reports whether the signature on c is valid.
func (c *CertificateRequest) CheckSignature() (err error) { func (c *CertificateRequest) CheckSignature() error {
return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificateRequest, c.Signature, c.PublicKey) return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificateRequest, c.Signature, c.PublicKey)
} }
...@@ -122,18 +122,18 @@ func NewFatFile(r io.ReaderAt) (*FatFile, error) { ...@@ -122,18 +122,18 @@ func NewFatFile(r io.ReaderAt) (*FatFile, error) {
// OpenFat opens the named file using os.Open and prepares it for use as a Mach-O // OpenFat opens the named file using os.Open and prepares it for use as a Mach-O
// universal binary. // universal binary.
func OpenFat(name string) (ff *FatFile, err error) { func OpenFat(name string) (*FatFile, error) {
f, err := os.Open(name) f, err := os.Open(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ff, err = NewFatFile(f) ff, err := NewFatFile(f)
if err != nil { if err != nil {
f.Close() f.Close()
return nil, err return nil, err
} }
ff.closer = f ff.closer = f
return return ff, nil
} }
func (ff *FatFile) Close() error { func (ff *FatFile) Close() error {
......
...@@ -37,27 +37,28 @@ func NewWriter(w io.Writer) *Writer { ...@@ -37,27 +37,28 @@ func NewWriter(w io.Writer) *Writer {
// Writer writes a single CSV record to w along with any necessary quoting. // Writer writes a single CSV record to w along with any necessary quoting.
// A record is a slice of strings with each string being one field. // A record is a slice of strings with each string being one field.
func (w *Writer) Write(record []string) (err error) { func (w *Writer) Write(record []string) error {
for n, field := range record { for n, field := range record {
if n > 0 { if n > 0 {
if _, err = w.w.WriteRune(w.Comma); err != nil { if _, err := w.w.WriteRune(w.Comma); err != nil {
return return err
} }
} }
// If we don't have to have a quoted field then just // If we don't have to have a quoted field then just
// write out the field and continue to the next field. // write out the field and continue to the next field.
if !w.fieldNeedsQuotes(field) { if !w.fieldNeedsQuotes(field) {
if _, err = w.w.WriteString(field); err != nil { if _, err := w.w.WriteString(field); err != nil {
return return err
} }
continue continue
} }
if err = w.w.WriteByte('"'); err != nil { if err := w.w.WriteByte('"'); err != nil {
return return err
} }
for _, r1 := range field { for _, r1 := range field {
var err error
switch r1 { switch r1 {
case '"': case '"':
_, err = w.w.WriteString(`""`) _, err = w.w.WriteString(`""`)
...@@ -75,20 +76,21 @@ func (w *Writer) Write(record []string) (err error) { ...@@ -75,20 +76,21 @@ func (w *Writer) Write(record []string) (err error) {
_, err = w.w.WriteRune(r1) _, err = w.w.WriteRune(r1)
} }
if err != nil { if err != nil {
return return err
} }
} }
if err = w.w.WriteByte('"'); err != nil { if err := w.w.WriteByte('"'); err != nil {
return return err
} }
} }
var err error
if w.UseCRLF { if w.UseCRLF {
_, err = w.w.WriteString("\r\n") _, err = w.w.WriteString("\r\n")
} else { } else {
err = w.w.WriteByte('\n') err = w.w.WriteByte('\n')
} }
return return err
} }
// Flush writes any buffered data to the underlying io.Writer. // Flush writes any buffered data to the underlying io.Writer.
...@@ -104,9 +106,9 @@ func (w *Writer) Error() error { ...@@ -104,9 +106,9 @@ func (w *Writer) Error() error {
} }
// WriteAll writes multiple CSV records to w using Write and then calls Flush. // WriteAll writes multiple CSV records to w using Write and then calls Flush.
func (w *Writer) WriteAll(records [][]string) (err error) { func (w *Writer) WriteAll(records [][]string) error {
for _, record := range records { for _, record := range records {
err = w.Write(record) err := w.Write(record)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -237,10 +237,11 @@ func NewDecoder(r io.Reader) *Decoder { ...@@ -237,10 +237,11 @@ func NewDecoder(r io.Reader) *Decoder {
// set to the URL identifying its name space when known. // set to the URL identifying its name space when known.
// If Token encounters an unrecognized name space prefix, // If Token encounters an unrecognized name space prefix,
// it uses the prefix as the Space rather than report an error. // it uses the prefix as the Space rather than report an error.
func (d *Decoder) Token() (t Token, err error) { func (d *Decoder) Token() (Token, error) {
var t Token
var err error
if d.stk != nil && d.stk.kind == stkEOF { if d.stk != nil && d.stk.kind == stkEOF {
err = io.EOF return nil, io.EOF
return
} }
if d.nextToken != nil { if d.nextToken != nil {
t = d.nextToken t = d.nextToken
...@@ -249,7 +250,7 @@ func (d *Decoder) Token() (t Token, err error) { ...@@ -249,7 +250,7 @@ func (d *Decoder) Token() (t Token, err error) {
if err == io.EOF && d.stk != nil && d.stk.kind != stkEOF { if err == io.EOF && d.stk != nil && d.stk.kind != stkEOF {
err = d.syntaxError("unexpected EOF") err = d.syntaxError("unexpected EOF")
} }
return return t, err
} }
if !d.Strict { if !d.Strict {
...@@ -292,7 +293,7 @@ func (d *Decoder) Token() (t Token, err error) { ...@@ -292,7 +293,7 @@ func (d *Decoder) Token() (t Token, err error) {
} }
t = t1 t = t1
} }
return return t, err
} }
const xmlURL = "http://www.w3.org/XML/1998/namespace" const xmlURL = "http://www.w3.org/XML/1998/namespace"
......
...@@ -38,7 +38,7 @@ const ( ...@@ -38,7 +38,7 @@ const (
// the flags and options for the operand's format specifier. // the flags and options for the operand's format specifier.
type State interface { type State interface {
// Write is the function to call to emit formatted output to be printed. // Write is the function to call to emit formatted output to be printed.
Write(b []byte) (ret int, err error) Write(b []byte) (n int, err error)
// Width returns the value of the width option and whether it has been set. // Width returns the value of the width option and whether it has been set.
Width() (wid int, ok bool) Width() (wid int, ok bool)
// Precision returns the value of the precision option and whether it has been set. // Precision returns the value of the precision option and whether it has been set.
......
...@@ -36,8 +36,11 @@ func NotNilFilter(_ string, v reflect.Value) bool { ...@@ -36,8 +36,11 @@ func NotNilFilter(_ string, v reflect.Value) bool {
// struct fields for which f(fieldname, fieldvalue) is true are // struct fields for which f(fieldname, fieldvalue) is true are
// printed; all others are filtered from the output. Unexported // printed; all others are filtered from the output. Unexported
// struct fields are never printed. // struct fields are never printed.
// func Fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) error {
func Fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) (err error) { return fprint(w, fset, x, f)
}
func fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) (err error) {
// setup printer // setup printer
p := printer{ p := printer{
output: w, output: w,
......
...@@ -87,11 +87,11 @@ type Context struct { ...@@ -87,11 +87,11 @@ type Context struct {
// ReadDir returns a slice of os.FileInfo, sorted by Name, // ReadDir returns a slice of os.FileInfo, sorted by Name,
// describing the content of the named directory. // describing the content of the named directory.
// If ReadDir is nil, Import uses ioutil.ReadDir. // If ReadDir is nil, Import uses ioutil.ReadDir.
ReadDir func(dir string) (fi []os.FileInfo, err error) ReadDir func(dir string) ([]os.FileInfo, error)
// OpenFile opens a file (not a directory) for reading. // OpenFile opens a file (not a directory) for reading.
// If OpenFile is nil, Import uses os.Open. // If OpenFile is nil, Import uses os.Open.
OpenFile func(path string) (r io.ReadCloser, err error) OpenFile func(path string) (io.ReadCloser, error)
} }
// joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join. // joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join.
......
...@@ -215,7 +215,9 @@ func (check *Checker) handleBailout(err *error) { ...@@ -215,7 +215,9 @@ func (check *Checker) handleBailout(err *error) {
} }
// Files checks the provided files as part of the checker's package. // Files checks the provided files as part of the checker's package.
func (check *Checker) Files(files []*ast.File) (err error) { func (check *Checker) Files(files []*ast.File) error { return check.checkFiles(files) }
func (check *Checker) checkFiles(files []*ast.File) (err error) {
defer check.handleBailout(&err) defer check.handleBailout(&err)
check.initFiles(files) check.initFiles(files)
......
...@@ -34,7 +34,7 @@ import ( ...@@ -34,7 +34,7 @@ import (
// level untyped constants will return an untyped type rather then the // level untyped constants will return an untyped type rather then the
// respective context-specific type. // respective context-specific type.
// //
func Eval(fset *token.FileSet, pkg *Package, pos token.Pos, expr string) (tv TypeAndValue, err error) { func Eval(fset *token.FileSet, pkg *Package, pos token.Pos, expr string) (TypeAndValue, error) {
// determine scope // determine scope
var scope *Scope var scope *Scope
if pkg == nil { if pkg == nil {
......
...@@ -226,7 +226,7 @@ type WriterAt interface { ...@@ -226,7 +226,7 @@ type WriterAt interface {
// //
// ReadByte reads and returns the next byte from the input. // ReadByte reads and returns the next byte from the input.
type ByteReader interface { type ByteReader interface {
ReadByte() (c byte, err error) ReadByte() (byte, error)
} }
// ByteScanner is the interface that adds the UnreadByte method to the // ByteScanner is the interface that adds the UnreadByte method to the
......
...@@ -103,7 +103,7 @@ type netConn struct { ...@@ -103,7 +103,7 @@ type netConn struct {
// New establishes a new connection to the system log daemon. Each // New establishes a new connection to the system log daemon. Each
// write to the returned writer sends a log message with the given // write to the returned writer sends a log message with the given
// priority and prefix. // priority and prefix.
func New(priority Priority, tag string) (w *Writer, err error) { func New(priority Priority, tag string) (*Writer, error) {
return Dial("", "", priority, tag) return Dial("", "", priority, tag)
} }
...@@ -187,57 +187,57 @@ func (w *Writer) Close() error { ...@@ -187,57 +187,57 @@ func (w *Writer) Close() error {
// Emerg logs a message with severity LOG_EMERG, ignoring the severity // Emerg logs a message with severity LOG_EMERG, ignoring the severity
// passed to New. // passed to New.
func (w *Writer) Emerg(m string) (err error) { func (w *Writer) Emerg(m string) error {
_, err = w.writeAndRetry(LOG_EMERG, m) _, err := w.writeAndRetry(LOG_EMERG, m)
return err return err
} }
// Alert logs a message with severity LOG_ALERT, ignoring the severity // Alert logs a message with severity LOG_ALERT, ignoring the severity
// passed to New. // passed to New.
func (w *Writer) Alert(m string) (err error) { func (w *Writer) Alert(m string) error {
_, err = w.writeAndRetry(LOG_ALERT, m) _, err := w.writeAndRetry(LOG_ALERT, m)
return err return err
} }
// Crit logs a message with severity LOG_CRIT, ignoring the severity // Crit logs a message with severity LOG_CRIT, ignoring the severity
// passed to New. // passed to New.
func (w *Writer) Crit(m string) (err error) { func (w *Writer) Crit(m string) error {
_, err = w.writeAndRetry(LOG_CRIT, m) _, err := w.writeAndRetry(LOG_CRIT, m)
return err return err
} }
// Err logs a message with severity LOG_ERR, ignoring the severity // Err logs a message with severity LOG_ERR, ignoring the severity
// passed to New. // passed to New.
func (w *Writer) Err(m string) (err error) { func (w *Writer) Err(m string) error {
_, err = w.writeAndRetry(LOG_ERR, m) _, err := w.writeAndRetry(LOG_ERR, m)
return err return err
} }
// Warning logs a message with severity LOG_WARNING, ignoring the // Warning logs a message with severity LOG_WARNING, ignoring the
// severity passed to New. // severity passed to New.
func (w *Writer) Warning(m string) (err error) { func (w *Writer) Warning(m string) error {
_, err = w.writeAndRetry(LOG_WARNING, m) _, err := w.writeAndRetry(LOG_WARNING, m)
return err return err
} }
// Notice logs a message with severity LOG_NOTICE, ignoring the // Notice logs a message with severity LOG_NOTICE, ignoring the
// severity passed to New. // severity passed to New.
func (w *Writer) Notice(m string) (err error) { func (w *Writer) Notice(m string) error {
_, err = w.writeAndRetry(LOG_NOTICE, m) _, err := w.writeAndRetry(LOG_NOTICE, m)
return err return err
} }
// Info logs a message with severity LOG_INFO, ignoring the severity // Info logs a message with severity LOG_INFO, ignoring the severity
// passed to New. // passed to New.
func (w *Writer) Info(m string) (err error) { func (w *Writer) Info(m string) error {
_, err = w.writeAndRetry(LOG_INFO, m) _, err := w.writeAndRetry(LOG_INFO, m)
return err return err
} }
// Debug logs a message with severity LOG_DEBUG, ignoring the severity // Debug logs a message with severity LOG_DEBUG, ignoring the severity
// passed to New. // passed to New.
func (w *Writer) Debug(m string) (err error) { func (w *Writer) Debug(m string) error {
_, err = w.writeAndRetry(LOG_DEBUG, m) _, err := w.writeAndRetry(LOG_DEBUG, m)
return err return err
} }
......
...@@ -20,7 +20,11 @@ import ( ...@@ -20,7 +20,11 @@ import (
// a Content-Disposition of "form-data". // a Content-Disposition of "form-data".
// It stores up to maxMemory bytes of the file parts in memory // It stores up to maxMemory bytes of the file parts in memory
// and the remainder on disk in temporary files. // and the remainder on disk in temporary files.
func (r *Reader) ReadForm(maxMemory int64) (f *Form, err error) { func (r *Reader) ReadForm(maxMemory int64) (*Form, error) {
return r.readForm(maxMemory)
}
func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
form := &Form{make(map[string][]string), make(map[string][]*FileHeader)} form := &Form{make(map[string][]string), make(map[string][]*FileHeader)}
defer func() { defer func() {
if err != nil { if err != nil {
......
...@@ -704,7 +704,9 @@ func putTextprotoReader(r *textproto.Reader) { ...@@ -704,7 +704,9 @@ func putTextprotoReader(r *textproto.Reader) {
} }
// ReadRequest reads and parses an incoming request from b. // ReadRequest reads and parses an incoming request from b.
func ReadRequest(b *bufio.Reader) (req *Request, err error) { return readRequest(b, deleteHostHeader) } func ReadRequest(b *bufio.Reader) (*Request, error) {
return readRequest(b, deleteHostHeader)
}
// Constants for readRequest's deleteHostHeader parameter. // Constants for readRequest's deleteHostHeader parameter.
const ( const (
......
...@@ -309,14 +309,14 @@ func (l *UnixListener) AcceptUnix() (*UnixConn, error) { ...@@ -309,14 +309,14 @@ func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
return newUnixConn(fd), nil return newUnixConn(fd), nil
} }
// Accept implements the Accept method in the Listener interface; it // Accept implements the Accept method in the Listener interface.
// waits for the next call and returns a generic Conn. // Returned connections will be of type *UnixConn.
func (l *UnixListener) Accept() (c Conn, err error) { func (l *UnixListener) Accept() (Conn, error) {
c1, err := l.AcceptUnix() c, err := l.AcceptUnix()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return c1, nil return c, nil
} }
// Close stops listening on the Unix address. Already accepted // Close stops listening on the Unix address. Already accepted
......
...@@ -411,10 +411,11 @@ func split(s string, c string, cutc bool) (string, string) { ...@@ -411,10 +411,11 @@ func split(s string, c string, cutc bool) (string, string) {
// Parse parses rawurl into a URL structure. // Parse parses rawurl into a URL structure.
// The rawurl may be relative or absolute. // The rawurl may be relative or absolute.
func Parse(rawurl string) (url *URL, err error) { func Parse(rawurl string) (*URL, error) {
// Cut off #frag // Cut off #frag
u, frag := split(rawurl, "#", true) u, frag := split(rawurl, "#", true)
if url, err = parse(u, false); err != nil { url, err := parse(u, false)
if err != nil {
return nil, err return nil, err
} }
if frag == "" { if frag == "" {
...@@ -431,7 +432,7 @@ func Parse(rawurl string) (url *URL, err error) { ...@@ -431,7 +432,7 @@ func Parse(rawurl string) (url *URL, err error) {
// only as an absolute URI or an absolute path. // only as an absolute URI or an absolute path.
// The string rawurl is assumed not to have a #fragment suffix. // The string rawurl is assumed not to have a #fragment suffix.
// (Web browsers strip #fragment before sending the URL to a web server.) // (Web browsers strip #fragment before sending the URL to a web server.)
func ParseRequestURI(rawurl string) (url *URL, err error) { func ParseRequestURI(rawurl string) (*URL, error) {
return parse(rawurl, true) return parse(rawurl, true)
} }
...@@ -744,10 +745,10 @@ func (v Values) Del(key string) { ...@@ -744,10 +745,10 @@ func (v Values) Del(key string) {
// ParseQuery always returns a non-nil map containing all the // ParseQuery always returns a non-nil map containing all the
// valid query parameters found; err describes the first decoding error // valid query parameters found; err describes the first decoding error
// encountered, if any. // encountered, if any.
func ParseQuery(query string) (m Values, err error) { func ParseQuery(query string) (Values, error) {
m = make(Values) m := make(Values)
err = parseQuery(m, query) err := parseQuery(m, query)
return return m, err
} }
func parseQuery(m Values, query string) (err error) { func parseQuery(m Values, query string) (err error) {
......
...@@ -112,7 +112,7 @@ func Hostname() (name string, err error) { ...@@ -112,7 +112,7 @@ func Hostname() (name string, err error) {
// nil error. If it encounters an error before the end of the // nil error. If it encounters an error before the end of the
// directory, Readdir returns the FileInfo read until that point // directory, Readdir returns the FileInfo read until that point
// and a non-nil error. // and a non-nil error.
func (f *File) Readdir(n int) (fi []FileInfo, err error) { func (f *File) Readdir(n int) ([]FileInfo, error) {
if f == nil { if f == nil {
return nil, ErrInvalid return nil, ErrInvalid
} }
......
...@@ -46,7 +46,7 @@ func findExecutable(file string, exts []string) (string, error) { ...@@ -46,7 +46,7 @@ func findExecutable(file string, exts []string) (string, error) {
return f, nil return f, nil
} }
} }
return ``, os.ErrNotExist return "", os.ErrNotExist
} }
// LookPath searches for an executable binary named file // LookPath searches for an executable binary named file
...@@ -55,9 +55,9 @@ func findExecutable(file string, exts []string) (string, error) { ...@@ -55,9 +55,9 @@ func findExecutable(file string, exts []string) (string, error) {
// LookPath also uses PATHEXT environment variable to match // LookPath also uses PATHEXT environment variable to match
// a suitable candidate. // a suitable candidate.
// The result may be an absolute path or a path relative to the current directory. // The result may be an absolute path or a path relative to the current directory.
func LookPath(file string) (f string, err error) { func LookPath(file string) (string, error) {
x := os.Getenv(`PATHEXT`) x := os.Getenv(`PATHEXT`)
if x == `` { if x == "" {
x = `.COM;.EXE;.BAT;.CMD` x = `.COM;.EXE;.BAT;.CMD`
} }
exts := []string{} exts := []string{}
...@@ -71,22 +71,23 @@ func LookPath(file string) (f string, err error) { ...@@ -71,22 +71,23 @@ func LookPath(file string) (f string, err error) {
exts = append(exts, e) exts = append(exts, e)
} }
if strings.ContainsAny(file, `:\/`) { if strings.ContainsAny(file, `:\/`) {
if f, err = findExecutable(file, exts); err == nil { if f, err := findExecutable(file, exts); err == nil {
return return f, nil
} else {
return "", &Error{file, err}
} }
return ``, &Error{file, err}
} }
if f, err = findExecutable(`.\`+file, exts); err == nil { if f, err := findExecutable(`.\`+file, exts); err == nil {
return return f, nil
} }
if pathenv := os.Getenv(`PATH`); pathenv != `` { if pathenv := os.Getenv(`PATH`); pathenv != "" {
for _, dir := range splitList(pathenv) { for _, dir := range splitList(pathenv) {
if f, err = findExecutable(dir+`\`+file, exts); err == nil { if f, err := findExecutable(dir+`\`+file, exts); err == nil {
return return f, nil
} }
} }
} }
return ``, &Error{file, ErrNotFound} return "", &Error{file, ErrNotFound}
} }
func splitList(path string) []string { func splitList(path string) []string {
...@@ -115,7 +116,7 @@ func splitList(path string) []string { ...@@ -115,7 +116,7 @@ func splitList(path string) []string {
// Remove quotes. // Remove quotes.
for i, s := range list { for i, s := range list {
if strings.Contains(s, `"`) { if strings.Contains(s, `"`) {
list[i] = strings.Replace(s, `"`, ``, -1) list[i] = strings.Replace(s, `"`, "", -1)
} }
} }
......
...@@ -7,7 +7,7 @@ package strconv ...@@ -7,7 +7,7 @@ package strconv
// ParseBool returns the boolean value represented by the string. // ParseBool returns the boolean value represented by the string.
// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. // It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
// Any other value returns an error. // Any other value returns an error.
func ParseBool(str string) (value bool, err error) { func ParseBool(str string) (bool, error) {
switch str { switch str {
case "1", "t", "T", "true", "TRUE", "True": case "1", "t", "T", "true", "TRUE", "True":
return true, nil return true, nil
......
...@@ -528,11 +528,10 @@ func atof64(s string) (f float64, err error) { ...@@ -528,11 +528,10 @@ func atof64(s string) (f float64, err error) {
// If s is syntactically well-formed but is more than 1/2 ULP // If s is syntactically well-formed but is more than 1/2 ULP
// away from the largest floating point number of the given size, // away from the largest floating point number of the given size,
// ParseFloat returns f = ±Inf, err.Err = ErrRange. // ParseFloat returns f = ±Inf, err.Err = ErrRange.
func ParseFloat(s string, bitSize int) (f float64, err error) { func ParseFloat(s string, bitSize int) (float64, error) {
if bitSize == 32 { if bitSize == 32 {
f1, err1 := atof32(s) f, err := atof32(s)
return float64(f1), err1 return float64(f), err
} }
f1, err1 := atof64(s) return atof64(s)
return f1, err1
} }
...@@ -39,7 +39,9 @@ const IntSize = intSize ...@@ -39,7 +39,9 @@ const IntSize = intSize
const maxUint64 = (1<<64 - 1) const maxUint64 = (1<<64 - 1)
// ParseUint is like ParseInt but for unsigned numbers. // ParseUint is like ParseInt but for unsigned numbers.
func ParseUint(s string, base int, bitSize int) (n uint64, err error) { func ParseUint(s string, base int, bitSize int) (uint64, error) {
var n uint64
var err error
var cutoff, maxVal uint64 var cutoff, maxVal uint64
if bitSize == 0 { if bitSize == 0 {
...@@ -196,7 +198,7 @@ func ParseInt(s string, base int, bitSize int) (i int64, err error) { ...@@ -196,7 +198,7 @@ func ParseInt(s string, base int, bitSize int) (i int64, err error) {
} }
// Atoi is shorthand for ParseInt(s, 10, 0). // Atoi is shorthand for ParseInt(s, 10, 0).
func Atoi(s string) (i int, err error) { func Atoi(s string) (int, error) {
i64, err := ParseInt(s, 10, 0) i64, err := ParseInt(s, 10, 0)
return int(i64), err return int(i64), err
} }
...@@ -347,7 +347,7 @@ func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, ...@@ -347,7 +347,7 @@ func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string,
// that s quotes. (If s is single-quoted, it would be a Go // that s quotes. (If s is single-quoted, it would be a Go
// character literal; Unquote returns the corresponding // character literal; Unquote returns the corresponding
// one-character string.) // one-character string.)
func Unquote(s string) (t string, err error) { func Unquote(s string) (string, error) {
n := len(s) n := len(s)
if n < 2 { if n < 2 {
return "", ErrSyntax return "", ErrSyntax
......
...@@ -62,14 +62,14 @@ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { ...@@ -62,14 +62,14 @@ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
return return
} }
func (r *Reader) ReadByte() (b byte, err error) { func (r *Reader) ReadByte() (byte, error) {
r.prevRune = -1 r.prevRune = -1
if r.i >= int64(len(r.s)) { if r.i >= int64(len(r.s)) {
return 0, io.EOF return 0, io.EOF
} }
b = r.s[r.i] b := r.s[r.i]
r.i++ r.i++
return return b, nil
} }
func (r *Reader) UnreadByte() error { func (r *Reader) UnreadByte() error {
......
...@@ -262,24 +262,21 @@ func (s *CheckEqualError) Error() string { ...@@ -262,24 +262,21 @@ func (s *CheckEqualError) Error() string {
// t.Error(err) // t.Error(err)
// } // }
// } // }
func Check(f interface{}, config *Config) (err error) { func Check(f interface{}, config *Config) error {
if config == nil { if config == nil {
config = &defaultConfig config = &defaultConfig
} }
fVal, fType, ok := functionAndType(f) fVal, fType, ok := functionAndType(f)
if !ok { if !ok {
err = SetupError("argument is not a function") return SetupError("argument is not a function")
return
} }
if fType.NumOut() != 1 { if fType.NumOut() != 1 {
err = SetupError("function does not return one value") return SetupError("function does not return one value")
return
} }
if fType.Out(0).Kind() != reflect.Bool { if fType.Out(0).Kind() != reflect.Bool {
err = SetupError("function does not return a bool") return SetupError("function does not return a bool")
return
} }
arguments := make([]reflect.Value, fType.NumIn()) arguments := make([]reflect.Value, fType.NumIn())
...@@ -287,43 +284,39 @@ func Check(f interface{}, config *Config) (err error) { ...@@ -287,43 +284,39 @@ func Check(f interface{}, config *Config) (err error) {
maxCount := config.getMaxCount() maxCount := config.getMaxCount()
for i := 0; i < maxCount; i++ { for i := 0; i < maxCount; i++ {
err = arbitraryValues(arguments, fType, config, rand) err := arbitraryValues(arguments, fType, config, rand)
if err != nil { if err != nil {
return return err
} }
if !fVal.Call(arguments)[0].Bool() { if !fVal.Call(arguments)[0].Bool() {
err = &CheckError{i + 1, toInterfaces(arguments)} return &CheckError{i + 1, toInterfaces(arguments)}
return
} }
} }
return return nil
} }
// CheckEqual looks for an input on which f and g return different results. // CheckEqual looks for an input on which f and g return different results.
// It calls f and g repeatedly with arbitrary values for each argument. // It calls f and g repeatedly with arbitrary values for each argument.
// If f and g return different answers, CheckEqual returns a *CheckEqualError // If f and g return different answers, CheckEqual returns a *CheckEqualError
// describing the input and the outputs. // describing the input and the outputs.
func CheckEqual(f, g interface{}, config *Config) (err error) { func CheckEqual(f, g interface{}, config *Config) error {
if config == nil { if config == nil {
config = &defaultConfig config = &defaultConfig
} }
x, xType, ok := functionAndType(f) x, xType, ok := functionAndType(f)
if !ok { if !ok {
err = SetupError("f is not a function") return SetupError("f is not a function")
return
} }
y, yType, ok := functionAndType(g) y, yType, ok := functionAndType(g)
if !ok { if !ok {
err = SetupError("g is not a function") return SetupError("g is not a function")
return
} }
if xType != yType { if xType != yType {
err = SetupError("functions have different types") return SetupError("functions have different types")
return
} }
arguments := make([]reflect.Value, xType.NumIn()) arguments := make([]reflect.Value, xType.NumIn())
...@@ -331,21 +324,20 @@ func CheckEqual(f, g interface{}, config *Config) (err error) { ...@@ -331,21 +324,20 @@ func CheckEqual(f, g interface{}, config *Config) (err error) {
maxCount := config.getMaxCount() maxCount := config.getMaxCount()
for i := 0; i < maxCount; i++ { for i := 0; i < maxCount; i++ {
err = arbitraryValues(arguments, xType, config, rand) err := arbitraryValues(arguments, xType, config, rand)
if err != nil { if err != nil {
return return err
} }
xOut := toInterfaces(x.Call(arguments)) xOut := toInterfaces(x.Call(arguments))
yOut := toInterfaces(y.Call(arguments)) yOut := toInterfaces(y.Call(arguments))
if !reflect.DeepEqual(xOut, yOut) { if !reflect.DeepEqual(xOut, yOut) {
err = &CheckEqualError{CheckError{i + 1, toInterfaces(arguments)}, xOut, yOut} return &CheckEqualError{CheckError{i + 1, toInterfaces(arguments)}, xOut, yOut}
return
} }
} }
return return nil
} }
// arbitraryValues writes Values to args such that args contains Values // arbitraryValues writes Values to args such that args contains Values
......
...@@ -462,8 +462,11 @@ func handlePanic(err *error, op string) { ...@@ -462,8 +462,11 @@ func handlePanic(err *error, op string) {
// that any data buffered in the Writer is written to output. Any // that any data buffered in the Writer is written to output. Any
// incomplete escape sequence at the end is considered // incomplete escape sequence at the end is considered
// complete for formatting purposes. // complete for formatting purposes.
// func (b *Writer) Flush() error {
func (b *Writer) Flush() (err error) { return b.flush()
}
func (b *Writer) flush() (err error) {
defer b.reset() // even in the presence of errors defer b.reset() // even in the presence of errors
defer handlePanic(&err, "Flush") defer handlePanic(&err, "Flush")
...@@ -478,8 +481,7 @@ func (b *Writer) Flush() (err error) { ...@@ -478,8 +481,7 @@ func (b *Writer) Flush() (err error) {
// format contents of buffer // format contents of buffer
b.format(0, 0, len(b.lines)) b.format(0, 0, len(b.lines))
return nil
return
} }
var hbar = []byte("---\n") var hbar = []byte("---\n")
......
...@@ -164,7 +164,11 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) ...@@ -164,7 +164,11 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{})
// execution stops, but partial results may already have been written to // execution stops, but partial results may already have been written to
// the output writer. // the output writer.
// A template may be executed safely in parallel. // A template may be executed safely in parallel.
func (t *Template) Execute(wr io.Writer, data interface{}) (err error) { func (t *Template) Execute(wr io.Writer, data interface{}) error {
return t.execute(wr, data)
}
func (t *Template) execute(wr io.Writer, data interface{}) (err error) {
defer errRecover(&err) defer errRecover(&err)
value := reflect.ValueOf(data) value := reflect.ValueOf(data)
state := &state{ state := &state{
......
...@@ -48,12 +48,12 @@ func (t *Tree) Copy() *Tree { ...@@ -48,12 +48,12 @@ func (t *Tree) Copy() *Tree {
// templates described in the argument string. The top-level template will be // templates described in the argument string. The top-level template will be
// given the specified name. If an error is encountered, parsing stops and an // given the specified name. If an error is encountered, parsing stops and an
// empty map is returned with the error. // empty map is returned with the error.
func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (treeSet map[string]*Tree, err error) { func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (map[string]*Tree, error) {
treeSet = make(map[string]*Tree) treeSet := make(map[string]*Tree)
t := New(name) t := New(name)
t.text = text t.text = text
_, err = t.Parse(text, leftDelim, rightDelim, treeSet, funcs...) _, err := t.Parse(text, leftDelim, rightDelim, treeSet, funcs...)
return return treeSet, err
} }
// next returns the next token. // next returns the next token.
......
...@@ -945,10 +945,11 @@ func (t Time) MarshalJSON() ([]byte, error) { ...@@ -945,10 +945,11 @@ func (t Time) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in RFC 3339 format. // The time is expected to be a quoted string in RFC 3339 format.
func (t *Time) UnmarshalJSON(data []byte) (err error) { func (t *Time) UnmarshalJSON(data []byte) error {
// Fractional seconds are handled implicitly by Parse. // Fractional seconds are handled implicitly by Parse.
var err error
*t, err = Parse(`"`+RFC3339+`"`, string(data)) *t, err = Parse(`"`+RFC3339+`"`, string(data))
return return err
} }
// MarshalText implements the encoding.TextMarshaler interface. // MarshalText implements the encoding.TextMarshaler interface.
...@@ -964,10 +965,11 @@ func (t Time) MarshalText() ([]byte, error) { ...@@ -964,10 +965,11 @@ func (t Time) MarshalText() ([]byte, error) {
// UnmarshalText implements the encoding.TextUnmarshaler interface. // UnmarshalText implements the encoding.TextUnmarshaler interface.
// The time is expected to be in RFC 3339 format. // The time is expected to be in RFC 3339 format.
func (t *Time) UnmarshalText(data []byte) (err error) { func (t *Time) UnmarshalText(data []byte) error {
// Fractional seconds are handled implicitly by Parse. // Fractional seconds are handled implicitly by Parse.
var err error
*t, err = Parse(RFC3339, string(data)) *t, err = Parse(RFC3339, string(data))
return return err
} }
// Unix returns the local Time corresponding to the given Unix time, // Unix returns the local Time corresponding to the given Unix time,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment