Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trivial: silent most golang-ci-lint errors in production code #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions dhcp4/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,27 @@ func (o Options) marshalLimited(w io.Writer, nBytes int, skip52 bool) (Options,
continue
}

w.Write([]byte{byte(n), byte(len(opt))})
w.Write(opt)
_, err := w.Write([]byte{byte(n), byte(len(opt))})
if err != nil {
return nil, err
}
_, err = w.Write(opt)
if err != nil {
return nil, err
}
nBytes -= len(opt) + 2
}

w.Write([]byte{255})
_, err := w.Write([]byte{255})
if err != nil {
return nil, err
}
nBytes--
if nBytes > 0 {
w.Write(make([]byte, nBytes))
_, err = w.Write(make([]byte, nBytes))
if err != nil {
return nil, err
}
}

return ret, nil
Expand Down
28 changes: 21 additions & 7 deletions dhcp4/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,22 @@ func (p *Packet) Marshal() ([]byte, error) {
ret.Write([]byte{0, 0})
}

writeIP(ret, p.ClientAddr)
writeIP(ret, p.YourAddr)
writeIP(ret, p.ServerAddr)
writeIP(ret, p.RelayAddr)
err = writeIP(ret, p.ClientAddr)
if err != nil {
return nil, err
}
err = writeIP(ret, p.YourAddr)
if err != nil {
return nil, err
}
err = writeIP(ret, p.ServerAddr)
if err != nil {
return nil, err
}
err = writeIP(ret, p.RelayAddr)
if err != nil {
return nil, err
}

// MAC address + 10 bytes of padding
ret.Write([]byte(p.HardwareAddr))
Expand Down Expand Up @@ -237,12 +249,14 @@ func (p *Packet) Marshal() ([]byte, error) {
return ret.Bytes(), nil
}

func writeIP(w io.Writer, ip net.IP) {
func writeIP(w io.Writer, ip net.IP) error {
ip = ip.To4()
if ip == nil {
w.Write([]byte{0, 0, 0, 0})
_, err := w.Write([]byte{0, 0, 0, 0})
return err
} else {
w.Write([]byte(ip))
_, err := w.Write([]byte(ip))
return err
}
}

Expand Down
6 changes: 3 additions & 3 deletions dhcp6/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *Packet) Marshal() ([]byte, error) {
return nil, fmt.Errorf("packet has malformed options section: %s", err)
}

ret := make([]byte, len(marshalledOptions)+4, len(marshalledOptions)+4)
ret := make([]byte, len(marshalledOptions)+4)
ret[0] = byte(p.Type)
copy(ret[1:], p.TransactionID[:])
copy(ret[4:], marshalledOptions)
Expand Down Expand Up @@ -99,7 +99,7 @@ func shouldDiscardRequest(p *Packet, serverDuid []byte) error {
if !options.HasServerID() {
return fmt.Errorf("'Request' packet has no server id option")
}
if bytes.Compare(options.ServerID(), serverDuid) != 0 {
if !bytes.Equal(options.ServerID(), serverDuid) {
return fmt.Errorf("'Request' packet's server id option (%d) is different from ours (%d)", options.ServerID(), serverDuid)
}
return nil
Expand All @@ -113,7 +113,7 @@ func shouldDiscardInformationRequest(p *Packet, serverDuid []byte) error {
if options.HasIaNa() || options.HasIaTa() {
return fmt.Errorf("'Information-request' packet has an IA option present")
}
if options.HasServerID() && (bytes.Compare(options.ServerID(), serverDuid) != 0) {
if options.HasServerID() && (!bytes.Equal(options.ServerID(), serverDuid)) {
return fmt.Errorf("'Information-request' packet's server id option (%d) is different from ours (%d)", options.ServerID(), serverDuid)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions dhcp6/packet_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (b *PacketBuilder) makeMsgReleaseReply(transactionID [3]byte, serverDUID, c

retOptions.Add(MakeOption(OptClientID, clientID))
retOptions.Add(MakeOption(OptServerID, serverDUID))
v := make([]byte, 19, 19)
v := make([]byte, 19)
copy(v[2:], []byte("Release received."))
retOptions.Add(MakeOption(OptStatusCode, v))

Expand Down Expand Up @@ -177,6 +177,6 @@ func iasWithoutAddesses(availableAssociations []*IdentityAssociation, allIAs [][

func calculateIAIDHash(interfaceID []byte) uint64 {
h := fnv.New64a()
h.Write(interfaceID)
_, _ = h.Write(interfaceID)
return h.Sum64()
}
7 changes: 4 additions & 3 deletions dhcp6/pool/random_address_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package pool

import (
"fmt"
"go.universe.tf/netboot/dhcp6"
"hash/fnv"
"math/big"
"math/rand"
"net"
"sync"
"time"

"go.universe.tf/netboot/dhcp6"
)

type associationExpiration struct {
Expand Down Expand Up @@ -153,7 +154,7 @@ func (p *RandomAddressPool) calculateAssociationExpiration(now time.Time) time.T

func (p *RandomAddressPool) calculateIAIDHash(clientID, interfaceID []byte) uint64 {
h := fnv.New64a()
h.Write(clientID)
h.Write(interfaceID)
_, _ = h.Write(clientID)
_, _ = h.Write(interfaceID)
return h.Sum64()
}
5 changes: 4 additions & 1 deletion pixiecore/api-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ var (
func main() {
flag.Parse()
http.HandleFunc("/v1/boot/", api)
http.ListenAndServe(":"+strconv.Itoa(*port), nil)
err := http.ListenAndServe(":"+strconv.Itoa(*port), nil)
if err != nil {
panic(err)
}
}

func api(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 4 additions & 1 deletion pixiecore/boot_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func (bc *APIBootConfiguration) GetBootURL(id []byte, clientArchType uint16) ([]
defer resp.Body.Close()

buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
url, _ := bc.makeURLAbsolute(buf.String())

return []byte(url), nil
Expand Down
5 changes: 4 additions & 1 deletion pixiecore/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func serverConfigFlags(cmd *cobra.Command) {

// Development flags, hidden from normal use.
cmd.Flags().String("ui-assets-dir", "", "UI assets directory (used for development)")
cmd.Flags().MarkHidden("ui-assets-dir")
err := cmd.Flags().MarkHidden("ui-assets-dir")
if err != nil {
panic(err)
}
}

func mustFile(path string) []byte {
Expand Down
5 changes: 3 additions & 2 deletions pixiecore/dhcpv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pixiecore

import (
"fmt"

"go.universe.tf/netboot/dhcp6"
)

Expand All @@ -23,10 +24,10 @@ func (s *ServerV6) serveDHCP(conn *dhcp6.Conn) error {
if err != nil {
s.log("dhcpv6", fmt.Sprintf("Error creating response for transaction: %d: %s", pkt.TransactionID, err))
if response == nil {
s.log("dhcpv6", fmt.Sprintf("Dropping the packet"))
s.log("dhcpv6", "Dropping the packet")
continue
} else {
s.log("dhcpv6", fmt.Sprintf("Will notify the client"))
s.log("dhcpv6", "Will notify the client")
}
}
if response == nil {
Expand Down
5 changes: 4 additions & 1 deletion pixiecore/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ func (s *Server) handleIpxe(w http.ResponseWriter, r *http.Request) {
start = time.Now()
s.machineEvent(mac, machineStateIpxeScript, "Sent iPXE boot script")
w.Header().Set("Content-Type", "text/plain")
w.Write(script)
_, err = w.Write(script)
if err != nil {
s.debug("HTTP", "writing response caused an error:%v", err)
}
s.debug("HTTP", "Writing ipxe script to %s took %s", mac, time.Since(start))
s.debug("HTTP", "handleIpxe for %s took %s", mac, time.Since(overallStart))
}
Expand Down
8 changes: 0 additions & 8 deletions pixiecore/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package pixiecore

import (
"encoding/base64"
"fmt"
"net"
"time"
Expand Down Expand Up @@ -100,10 +99,3 @@ func (s *Server) debug(subsystem, format string, args ...interface{}) {
}
s.Debug(subsystem, fmt.Sprintf(format, args...))
}

func (s *Server) debugPacket(subsystem string, layer int, packet []byte) {
if s.Debug == nil {
return
}
s.Debug(subsystem, fmt.Sprintf("PKT %d %s END", layer, base64.StdEncoding.EncodeToString(packet)))
}
5 changes: 4 additions & 1 deletion tftp/tftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ func (s *Server) transfer(addr net.Addr, req *rrq) error {

file, size, err := s.Handler(req.Filename, addr)
if err != nil {
conn.Write(tftpError("failed to get file"))
_, cerr := conn.Write(tftpError("failed to get file"))
if cerr != nil {
return fmt.Errorf("getting file bytes: %s, write err: %s", err, cerr)
}
return fmt.Errorf("getting file bytes: %s", err)
}
defer file.Close()
Expand Down