Skip to content

Commit

Permalink
treewide: Fix integer conversions
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Payne <[email protected]>
  • Loading branch information
twpayne authored and gandro committed Jan 12, 2021
1 parent 4e7fde7 commit cd083b8
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 44 deletions.
7 changes: 4 additions & 3 deletions cilium/cmd/bpf_lb_maglev_get.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Authors of Cilium
// Copyright 2020-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,11 +35,12 @@ var bpfMaglevGetCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
common.RequireRootPrivilege("cilium bpf lb maglev get")

svcID, err := strconv.Atoi(args[0])
svcIDUint64, err := strconv.ParseUint(args[0], 10, 16)
if err != nil {
Fatalf("Unable to parse %s: %s", args[0], err)
}
key := &lbmap.MaglevOuterKey{RevNatID: uint16(svcID)}
svcID := uint16(svcIDUint64)
key := &lbmap.MaglevOuterKey{RevNatID: svcID}
key = key.ToNetwork()
val := &lbmap.MaglevOuterVal{}

Expand Down
16 changes: 9 additions & 7 deletions cilium/cmd/bpf_metrics_list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Authors of Cilium
// Copyright 2018-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,7 +39,7 @@ const (
)

type metricsRow struct {
reasonCode int
reasonCode uint8
reasonDesc string
direction string
packets int
Expand Down Expand Up @@ -87,7 +87,7 @@ func listMetrics(m metricsmap.MetricsMap) {
}

func listJSONMetrics(bpfMetricsList map[string]string) {
metricsByReason := map[int]jsonMetric{}
metricsByReason := map[uint8]jsonMetric{}

for key, value := range bpfMetricsList {
row, err := extractRow(key, value)
Expand Down Expand Up @@ -171,15 +171,17 @@ func extractRow(key, value string) (*metricsRow, error) {
return nil, fmt.Errorf("cannot extract reason and traffic direction from map's key \"%s\"", key)
}

reasonCode, err := strconv.Atoi(reasonCodeStr)
reasonCodeUint64, err := strconv.ParseUint(reasonCodeStr, 10, 8)
if err != nil {
return nil, fmt.Errorf("cannot parse reason: %s", err)
}
reasonCode := uint8(reasonCodeUint64)

directionCode, err := strconv.Atoi(directionCodeStr)
directionCodeUint64, err := strconv.ParseUint(directionCodeStr, 10, 8)
if err != nil {
return nil, fmt.Errorf("cannot parse direction: %s", err)
}
directionCode := uint8(directionCodeUint64)

packetsStr, bytesStr, ok := extractTwoValues(value)
if !ok {
Expand All @@ -196,8 +198,8 @@ func extractRow(key, value string) (*metricsRow, error) {
return nil, fmt.Errorf("cannot parse bytes counter: %s", err)
}

reasonDesc := monitorAPI.DropReason(uint8(reasonCode))
direction := metricsmap.MetricDirection(uint8(directionCode))
reasonDesc := monitorAPI.DropReason(reasonCode)
direction := metricsmap.MetricDirection(directionCode)

return &metricsRow{reasonCode, reasonDesc, direction, packets, bytes}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions cilium/cmd/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2020 Authors of Cilium
// Copyright 2016-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -251,9 +251,9 @@ func endpointToPolicyMapPath(endpointID string) (string, error) {
}

var mapName string
id, err := strconv.Atoi(endpointID)
idUint64, err := strconv.ParseUint(endpointID, 10, 16)
if err == nil {
mapName = bpf.LocalMapName(policymap.MapName, uint16(id))
mapName = bpf.LocalMapName(policymap.MapName, uint16(idUint64))
} else if numericIdentity := identity.GetReservedID(endpointID); numericIdentity != identity.IdentityUnknown {
mapSuffix := "reserved_" + strconv.FormatUint(uint64(numericIdentity), 10)
mapName = fmt.Sprintf("%s%s", policymap.MapName, mapSuffix)
Expand Down
10 changes: 5 additions & 5 deletions cilium/cmd/policy_trace.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2020 Authors of Cilium
// Copyright 2017-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -276,18 +276,18 @@ func parseL4PortsSlice(slice []string) ([]*models.Port, error) {
default:
return nil, fmt.Errorf("invalid format %q. Should be <port>[/<protocol>]", v)
}
port := 0
var port uint16
portStr := vSplit[0]
if !iana.IsSvcName(portStr) {
var err error
port, err = strconv.Atoi(portStr)
portUint64, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid port %q: %s", portStr, err)
}
port = uint16(portUint64)
portStr = ""
}
l4 := &models.Port{
Port: uint16(port),
Port: port,
Name: portStr,
Protocol: protoStr,
}
Expand Down
20 changes: 12 additions & 8 deletions daemon/cmd/fqdn.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Authors of Cilium
// Copyright 2019-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -410,22 +410,26 @@ func (d *Daemon) notifyOnDNSMsg(lookupTime time.Time, ep *endpoint.Endpoint, epI
flowType = accesslog.TypeRequest
}

var epPort, serverPort int
var epPort, serverPort uint16
_, epPortStr, err := net.SplitHostPort(epIPPort)
if err != nil {
log.WithError(err).Error("cannot extract source IP from DNS request")
} else {
if epPort, err = strconv.Atoi(epPortStr); err != nil {
if epPortUint64, err := strconv.ParseUint(epPortStr, 10, 16); err != nil {
log.WithError(err).WithField(logfields.Port, epPortStr).Error("cannot parse source port")
} else {
epPort = uint16(epPortUint64)
}
}

serverIP, serverPortStr, err := net.SplitHostPort(serverAddr)
if err != nil {
log.WithError(err).Error("cannot extract destination IP from DNS request")
} else {
if serverPort, err = strconv.Atoi(serverPortStr); err != nil {
if serverPortUint64, err := strconv.ParseUint(serverPortStr, 10, 16); err != nil {
log.WithError(err).WithField(logfields.Port, serverPortStr).Error("cannot parse destination port")
} else {
serverPort = uint16(serverPortUint64)
}
}
if ep == nil {
Expand All @@ -445,7 +449,7 @@ func (d *Daemon) notifyOnDNSMsg(lookupTime time.Time, ep *endpoint.Endpoint, epI
log.WithError(err).Error("cannot extract DNS message details")
}

ep.UpdateProxyStatistics(strings.ToUpper(protocol), uint16(serverPort), false, !msg.Response, verdict)
ep.UpdateProxyStatistics(strings.ToUpper(protocol), serverPort, false, !msg.Response, verdict)
record := logger.NewLogRecord(proxy.DefaultEndpointInfoRegistry, ep, flowType, false,
func(lr *logger.LogRecord) { lr.LogRecord.TransportProtocol = accesslog.TransportProtocol(protoID) },
logger.LogTags.Verdict(verdict, reason),
Expand All @@ -462,7 +466,7 @@ func (d *Daemon) notifyOnDNSMsg(lookupTime time.Time, ep *endpoint.Endpoint, epI
Labels: ep.GetLabels(),
LabelsSHA256: ep.GetLabelsSHA(),
Identity: uint64(ep.GetIdentity()),
Port: uint16(epPort),
Port: epPort,
}

// When the server is an endpoint, get all the data for it.
Expand All @@ -475,15 +479,15 @@ func (d *Daemon) notifyOnDNSMsg(lookupTime time.Time, ep *endpoint.Endpoint, epI
Labels: serverEP.GetLabels(),
LabelsSHA256: serverEP.GetLabelsSHA(),
Identity: uint64(serverEP.GetIdentity()),
Port: uint16(serverPort),
Port: serverPort,
}
} else if serverSecID, exists := ipcache.IPIdentityCache.LookupByIP(serverIP); exists {
// TODO: handle IPv6
lr.LogRecord.DestinationEndpoint = accesslog.EndpointInfo{
IPv4: serverIP,
// IPv6: serverEP.GetIPv6Address(),
Identity: uint64(serverSecID.ID.Uint32()),
Port: uint16(serverPort),
Port: serverPort,
}
if secID := d.identityAllocator.LookupIdentityByID(d.ctx, serverSecID.ID); secID != nil {
lr.LogRecord.DestinationEndpoint.Labels = secID.Labels.GetModel()
Expand Down
6 changes: 3 additions & 3 deletions pkg/common/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2020 Authors of Cilium
// Copyright 2016-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,11 +39,11 @@ func C2GoArray(str string) []byte {
hexStr := strings.Split(str, ", ")
for _, hexDigit := range hexStr {
strDigit := strings.TrimPrefix(hexDigit, "0x")
digit, err := strconv.ParseInt(strDigit, 16, 9)
digitUint64, err := strconv.ParseUint(strDigit, 16, 8)
if err != nil {
return nil
}
ret = append(ret, byte(digit))
ret = append(ret, byte(digitUint64))
}
return ret
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/datapath/iptables/iptables.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2020 Authors of Cilium
// Copyright 2016-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -817,12 +817,12 @@ func (m *IptablesManager) GetProxyPort(name string) uint16 {
re := regexp.MustCompile(name + ".*TPROXY redirect 0.0.0.0:([1-9][0-9]*) mark")
str := re.FindString(string(res))
portStr := re.ReplaceAllString(str, "$1")
portInt, err := strconv.Atoi(portStr)
portUInt64, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
log.WithError(err).Debugf("Port number cannot be parsed: %s", portStr)
return 0
}
return uint16(portInt)
return uint16(portUInt64)
}

func (m *IptablesManager) RemoveProxyRules(proxyPort uint16, ingress bool, name string) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/monitor/format/flags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Authors of Cilium
// Copyright 2018-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,11 +38,11 @@ func (i *Uint16Flags) String() string {
// Set converts the specified value into an integer and appends it to the flags.
// Returns an error if the value cannot be converted to a 16-bit unsigned value.
func (i *Uint16Flags) Set(value string) error {
v, err := strconv.Atoi(value)
vUint64, err := strconv.ParseUint(value, 10, 16)
if err != nil {
return err
}
*i = append(*i, uint16(v))
*i = append(*i, uint16(vUint64))
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/node/address.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2020 Authors of Cilium
// Copyright 2016-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -440,13 +440,13 @@ func getCiliumHostIPsFromFile(nodeConfig string) (ipv4GW, ipv6Router net.IP) {
continue
}
ipv4GWHex := strings.TrimPrefix(defineLine[2], "0x")
ipv4GWint64, err := strconv.ParseInt(ipv4GWHex, 16, 0)
ipv4GWUint64, err := strconv.ParseUint(ipv4GWHex, 16, 32)
if err != nil {
continue
}
if ipv4GWint64 != int64(0) {
if ipv4GWUint64 != 0 {
bs := make([]byte, net.IPv4len)
byteorder.NetworkToHostPut(bs, uint32(ipv4GWint64))
byteorder.NetworkToHostPut(bs, uint32(ipv4GWUint64))
ipv4GW = net.IPv4(bs[0], bs[1], bs[2], bs[3])
hasIPv4 = true
}
Expand Down
10 changes: 5 additions & 5 deletions proxylib/testparsers/blockparser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Authors of Cilium
// Copyright 2018-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@ import (

. "github.com/cilium/cilium/proxylib/proxylib"

"github.com/cilium/proxy/go/cilium/api"
cilium "github.com/cilium/proxy/go/cilium/api"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -74,11 +74,11 @@ func getBlock(data [][]byte) ([]byte, int, int, error) {

// Now 'block' contains everything before the ':', parse it as a decimal number
// indicating the length of the frame AFTER the ':'
len64, err := strconv.ParseUint(block.String(), 10, 64)
if err != nil {
if lenUint64, err := strconv.ParseUint(block.String(), 10, 64); err != nil {
return block.Bytes(), 0, 0, err
} else {
block_len = int(lenUint64)
}
block_len = int(len64)
if block_len <= block.Len() {
return block.Bytes(), 0, 0, fmt.Errorf("Block length too short")
}
Expand Down

0 comments on commit cd083b8

Please sign in to comment.