Compare commits

..

2 Commits

Author SHA1 Message Date
Canelo Hill ec9a86625d
Merge eb890c85fd into 1d5465562b 2024-06-20 03:35:45 +00:00
Canelo Hill eb890c85fd Handle errcheck warnings
The package ignored errors from net.Conn Set*Deadline in a few places.
Update the package to return these errors to the caller.

Ignore all other errors reported by errcheck. These errors are safe to
ignore because
- The function is making a best effort to cleanup while handling another
  error.
- The function call is guaranteed to succeed.
- The error is ignored in a test.
2024-06-19 21:35:26 -06:00
3 changed files with 22 additions and 11 deletions

View File

@ -305,9 +305,15 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
}) })
} }
// Close the network connection when returning an error. The variable
// netConn is set to nil before the success return at the end of the
// function.
defer func() { defer func() {
if netConn != nil { if netConn != nil {
netConn.Close() // It's safe to ignore the error from Close() because this code is
// only executed when returning a more important error to the
// application.
_ = netConn.Close()
} }
}() }()
@ -399,11 +405,11 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol") conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
if err := netConn.SetDeadline(time.Time{}); err != nil { if err := netConn.SetDeadline(time.Time{}); err != nil {
return nil, nil, err return nil, resp, err
} }
// Set netConn to nil to avoid call to netConn.Close() in // Success! Set netConn to nil to stop the deferred function above from
// deferred function call. // closing the network connection.
netConn = nil netConn = nil
return conn, resp, nil return conn, resp, nil

10
conn.go
View File

@ -371,7 +371,9 @@ func (c *Conn) read(n int) ([]byte, error) {
if err == io.EOF { if err == io.EOF {
err = errUnexpectedEOF err = errUnexpectedEOF
} }
_, _ = c.br.Discard(len(p)) // guaranteed to succeed // Discard is guaranteed to succeed because the number of bytes to discard
// is less than or equal to the number of bytes buffered.
_, _ = c.br.Discard(len(p))
return p, err return p, err
} }
@ -819,7 +821,7 @@ func (c *Conn) advanceFrame() (int, error) {
rsv2 := p[0]&rsv2Bit != 0 rsv2 := p[0]&rsv2Bit != 0
rsv3 := p[0]&rsv3Bit != 0 rsv3 := p[0]&rsv3Bit != 0
mask := p[1]&maskBit != 0 mask := p[1]&maskBit != 0
_ = c.setReadRemaining(int64(p[1] & 0x7f)) // will not overflow _ = c.setReadRemaining(int64(p[1] & 0x7f)) // will not fail because argument is >= 0
c.readDecompress = false c.readDecompress = false
if rsv1 { if rsv1 {
@ -937,7 +939,7 @@ func (c *Conn) advanceFrame() (int, error) {
var payload []byte var payload []byte
if c.readRemaining > 0 { if c.readRemaining > 0 {
payload, err = c.read(int(c.readRemaining)) payload, err = c.read(int(c.readRemaining))
_ = c.setReadRemaining(0) // will not overflow _ = c.setReadRemaining(0) // will not fail because argument is >= 0
if err != nil { if err != nil {
return noFrame, err return noFrame, err
} }
@ -1058,7 +1060,7 @@ func (r *messageReader) Read(b []byte) (int, error) {
} }
rem := c.readRemaining rem := c.readRemaining
rem -= int64(n) rem -= int64(n)
_ = c.setReadRemaining(rem) // will not overflow _ = c.setReadRemaining(rem) // rem is guaranteed to be >= 0
if c.readRemaining > 0 && c.readErr == io.EOF { if c.readRemaining > 0 && c.readErr == io.EOF {
c.readErr = errUnexpectedEOF c.readErr = errUnexpectedEOF
} }

View File

@ -178,10 +178,13 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
"websocket: hijack: "+err.Error()) "websocket: hijack: "+err.Error())
} }
// Close the network connection when returning an error. The variable
// netConn is set to nil before the success return at the end of the
// function.
defer func() { defer func() {
if netConn != nil { if netConn != nil {
// It's safe to ignore the error from Close() because this code is // It's safe to ignore the error from Close() because this code is
// only executed when returning a more important to the // only executed when returning a more important error to the
// application. // application.
_ = netConn.Close() _ = netConn.Close()
} }
@ -271,8 +274,8 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
} }
} }
// Set netConn to nil to avoid call to netConn.Close() in // Success! Set netConn to nil to stop the deferred function above from
// deferred function call. // closing the network connection.
netConn = nil netConn = nil
return c, nil return c, nil