Skip to content

Commit

Permalink
*: Use strconv.ParseUint instead of strconv.Atoi()
Browse files Browse the repository at this point in the history
For the 32-bit platform compatibility.

Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
  • Loading branch information
iwaseyusuke authored and fujita committed Jan 23, 2018
1 parent b391322 commit 549b703
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 93 deletions.
12 changes: 6 additions & 6 deletions api/grpc_server.go
Expand Up @@ -1489,8 +1489,8 @@ func (s *Server) GetDefinedSet(ctx context.Context, arg *GetDefinedSetRequest) (
for _, p := range cs.PrefixList {
exp := regexp.MustCompile("(\\d+)\\.\\.(\\d+)")
elems := exp.FindStringSubmatch(p.MasklengthRange)
min, _ := strconv.Atoi(elems[1])
max, _ := strconv.Atoi(elems[2])
min, _ := strconv.ParseUint(elems[1], 10, 32)
max, _ := strconv.ParseUint(elems[2], 10, 32)

l = append(l, &Prefix{IpPrefix: p.IpPrefix, MaskLengthMin: uint32(min), MaskLengthMax: uint32(max)})
}
Expand Down Expand Up @@ -1664,23 +1664,23 @@ func toStatementApi(s *config.Statement) *Statement {
case "+", "-":
action = MedActionType_MED_MOD
}
value, err := strconv.Atoi(matches[1] + matches[2])
value, err := strconv.ParseInt(matches[1]+matches[2], 10, 64)
if err != nil {
return nil
}
return &MedAction{
Value: int64(value),
Value: value,
Type: action,
}
}(),
AsPrepend: func() *AsPrependAction {
if len(s.Actions.BgpActions.SetAsPathPrepend.As) == 0 {
return nil
}
asn := 0
var asn uint64
useleft := false
if s.Actions.BgpActions.SetAsPathPrepend.As != "last-as" {
asn, _ = strconv.Atoi(s.Actions.BgpActions.SetAsPathPrepend.As)
asn, _ = strconv.ParseUint(s.Actions.BgpActions.SetAsPathPrepend.As, 10, 32)
} else {
useleft = true
}
Expand Down
4 changes: 3 additions & 1 deletion client/client.go
Expand Up @@ -818,7 +818,9 @@ func (cli *Client) GetRPKI() ([]*config.RpkiServer, error) {
}
servers := make([]*config.RpkiServer, 0, len(rsp.Servers))
for _, s := range rsp.Servers {
port, err := strconv.Atoi(s.Conf.RemotePort)
// Note: RpkiServerConfig.Port is uint32 type, but the TCP/UDP port is
// 16-bit length.
port, err := strconv.ParseUint(s.Conf.RemotePort, 10, 16)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions config/util.go
Expand Up @@ -247,25 +247,25 @@ func ParseMaskLength(prefix, mask string) (int, int, error) {
return 0, 0, fmt.Errorf("invalid mask length range: %s", mask)
}
// we've already checked the range is sane by regexp
min, _ := strconv.Atoi(elems[1])
max, _ := strconv.Atoi(elems[2])
min, _ := strconv.ParseUint(elems[1], 10, 8)
max, _ := strconv.ParseUint(elems[2], 10, 8)
if min > max {
return 0, 0, fmt.Errorf("invalid mask length range: %s", mask)
}
if ipv4 := ipNet.IP.To4(); ipv4 != nil {
f := func(i int) bool {
return i >= 0 && i <= 32
f := func(i uint64) bool {
return i <= 32
}
if !f(min) || !f(max) {
return 0, 0, fmt.Errorf("ipv4 mask length range outside scope :%s", mask)
}
} else {
f := func(i int) bool {
return i >= 0 && i <= 128
f := func(i uint64) bool {
return i <= 128
}
if !f(min) || !f(max) {
return 0, 0, fmt.Errorf("ipv6 mask length range outside scope :%s", mask)
}
}
return min, max, nil
return int(min), int(max), nil
}
7 changes: 5 additions & 2 deletions gobgp/cmd/bmp.go
Expand Up @@ -20,9 +20,10 @@ import (
"net"
"strconv"

"github.com/spf13/cobra"

"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet/bmp"
"github.com/spf13/cobra"
)

func modBmpServer(cmdType string, args []string) error {
Expand All @@ -40,7 +41,9 @@ func modBmpServer(cmdType string, args []string) error {
address = args[0]
} else {
address = host
pn, _ := strconv.Atoi(p)
// Note: BmpServerConfig.Port is uint32 type, but the TCP/UDP port is
// 16-bit length.
pn, _ := strconv.ParseUint(p, 10, 16)
port = uint32(pn)
}

Expand Down
4 changes: 2 additions & 2 deletions gobgp/cmd/common.go
Expand Up @@ -112,8 +112,8 @@ var mrtOpts struct {
OutputDir string
FileFormat string
Filename string `long:"filename" description:"MRT file name"`
RecordCount int `long:"count" description:"Number of records to inject"`
RecordSkip int `long:"skip" description:"Number of records to skip before injecting"`
RecordCount int64 `long:"count" description:"Number of records to inject"`
RecordSkip int64 `long:"skip" description:"Number of records to skip before injecting"`
QueueSize int `long:"batch-size" description:"Maximum number of updates to keep queued"`
Best bool `long:"only-best" description:"only keep best path routes"`
SkipV4 bool `long:"no-ipv4" description:"Skip importing IPv4 routes"`
Expand Down
56 changes: 29 additions & 27 deletions gobgp/cmd/global.go
Expand Up @@ -92,7 +92,7 @@ func rateLimitParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
return nil, fmt.Errorf("invalid rate-limit")
}
var rate float32
var as int
var as uint64
if elems[2] == ExtCommNameMap[RATE] {
f, err := strconv.ParseFloat(elems[3]+elems[4], 32)
if err != nil {
Expand All @@ -102,7 +102,7 @@ func rateLimitParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
}
if elems[7] != "" {
var err error
as, err = strconv.Atoi(elems[7])
as, err = strconv.ParseUint(elems[7], 10, 16)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func markParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != ExtCommNameMap[MARK] {
return nil, fmt.Errorf("invalid mark")
}
dscp, err := strconv.Atoi(args[1])
dscp, err := strconv.ParseUint(args[1], 10, 8)
if err != nil {
return nil, fmt.Errorf("invalid mark")
}
Expand Down Expand Up @@ -216,7 +216,7 @@ func esiLabelParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != ExtCommNameMap[ESI_LABEL] {
return nil, fmt.Errorf("invalid esi-label")
}
label, err := strconv.Atoi(args[1])
label, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -418,13 +418,13 @@ func ParseEvpnEthernetAutoDiscoveryArgs(args []string) (bgp.AddrPrefixInterface,
return nil, nil, err
}

e, err := strconv.Atoi(m["etag"][0])
e, err := strconv.ParseUint(m["etag"][0], 10, 32)
if err != nil {
return nil, nil, err
}
etag := uint32(e)

l, err := strconv.Atoi(m["label"][0])
l, err := strconv.ParseUint(m["label"][0], 10, 32)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -518,14 +518,14 @@ func ParseEvpnMacAdvArgs(args []string) (bgp.AddrPrefixInterface, []string, erro
return nil, nil, err
}

eTag, err := strconv.Atoi(eTagStr)
eTag, err := strconv.ParseUint(eTagStr, 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid etag: %s: %s", eTagStr, err)
}

var labels []uint32
for _, l := range strings.SplitN(labelStr, ",", 2) {
label, err := strconv.Atoi(l)
label, err := strconv.ParseUint(l, 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid label: %s: %s", labelStr, err)
}
Expand Down Expand Up @@ -604,7 +604,7 @@ func ParseEvpnMulticastArgs(args []string) (bgp.AddrPrefixInterface, []string, e
ipLen = net.IPv6len * 8
}

eTag, err := strconv.Atoi(eTagStr)
eTag, err := strconv.ParseUint(eTagStr, 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid etag: %s: %s", eTagStr, err)
}
Expand Down Expand Up @@ -727,15 +727,15 @@ func ParseEvpnIPPrefixArgs(args []string) (bgp.AddrPrefixInterface, []string, er
return nil, nil, err
}

e, err := strconv.Atoi(m["etag"][0])
e, err := strconv.ParseUint(m["etag"][0], 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid etag: %s: %s", m["etag"][0], err)
}
etag := uint32(e)

var label uint32
if len(m["label"]) > 0 {
e, err := strconv.Atoi(m["label"][0])
e, err := strconv.ParseUint(m["label"][0], 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid label: %s: %s", m["label"][0], err)
}
Expand Down Expand Up @@ -810,17 +810,17 @@ func extractOrigin(args []string) ([]string, bgp.PathAttributeInterface, error)
func toAs4Value(s string) (uint32, error) {
if strings.Contains(s, ".") {
v := strings.Split(s, ".")
upper, err := strconv.Atoi(v[0])
upper, err := strconv.ParseUint(v[0], 10, 16)
if err != nil {
return 0, nil
}
lower, err := strconv.Atoi(v[1])
lower, err := strconv.ParseUint(v[1], 10, 16)
if err != nil {
return 0, nil
}
return uint32(upper)<<16 + uint32(lower), nil
return uint32(upper<<16 | lower), nil
}
i, err := strconv.Atoi(s)
i, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -898,7 +898,7 @@ func extractNexthop(rf bgp.RouteFamily, args []string) ([]string, string, error)
func extractLocalPref(args []string) ([]string, bgp.PathAttributeInterface, error) {
for idx, arg := range args {
if arg == "local-pref" && len(args) > (idx+1) {
metric, err := strconv.Atoi(args[idx+1])
metric, err := strconv.ParseUint(args[idx+1], 10, 32)
if err != nil {
return nil, nil, err
}
Expand All @@ -912,12 +912,12 @@ func extractLocalPref(args []string) ([]string, bgp.PathAttributeInterface, erro
func extractMed(args []string) ([]string, bgp.PathAttributeInterface, error) {
for idx, arg := range args {
if arg == "med" && len(args) > (idx+1) {
metric, err := strconv.Atoi(args[idx+1])
med, err := strconv.ParseUint(args[idx+1], 10, 32)
if err != nil {
return nil, nil, err
}
args = append(args[:idx], args[idx+2:]...)
return args, bgp.NewPathAttributeMultiExitDisc(uint32(metric)), nil
return args, bgp.NewPathAttributeMultiExitDisc(uint32(med)), nil
}
}
return args, nil, nil
Expand Down Expand Up @@ -970,7 +970,7 @@ func extractAigp(args []string) ([]string, bgp.PathAttributeInterface, error) {
typ := args[idx+1]
switch typ {
case "metric":
metric, err := strconv.Atoi(args[idx+2])
metric, err := strconv.ParseUint(args[idx+2], 10, 64)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -1077,11 +1077,11 @@ func ParsePath(rf bgp.RouteFamily, args []string) (*table.Path, error) {
}

if len(args) > 2 && args[1] == "identifier" {
if id, err := strconv.Atoi(args[2]); err != nil {
id, err := strconv.ParseUint(args[2], 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid format")
} else {
nlri.SetPathIdentifier(uint32(id))
}
nlri.SetPathIdentifier(uint32(id))
extcomms = args[3:]
} else {
extcomms = args[1:]
Expand All @@ -1094,8 +1094,8 @@ func ParsePath(rf bgp.RouteFamily, args []string) (*table.Path, error) {
ip, nw, _ := net.ParseCIDR(args[0])
ones, _ := nw.Mask.Size()

label := 0
if label, err = strconv.Atoi(args[2]); err != nil {
label, err := strconv.ParseUint(args[2], 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid format")
}
mpls := bgp.NewMPLSLabelStack(uint32(label))
Expand Down Expand Up @@ -1397,17 +1397,19 @@ func modGlobalConfig(args []string) error {
if len(m["as"]) != 1 || len(m["router-id"]) != 1 {
return fmt.Errorf("usage: gobgp global as <VALUE> router-id <VALUE> [use-multipath] [listen-port <VALUE>] [listen-addresses <VALUE>...]")
}
asn, err := strconv.Atoi(m["as"][0])
asn, err := strconv.ParseUint(m["as"][0], 10, 32)
if err != nil {
return err
}
id := net.ParseIP(m["router-id"][0])
if id.To4() == nil {
return fmt.Errorf("invalid router-id format")
}
var port int
var port int64
if len(m["listen-port"]) > 0 {
port, err = strconv.Atoi(m["listen-port"][0])
// Note: GlobalConfig.Port is uint32 type, but the TCP/UDP port is
// 16-bit length.
port, err = strconv.ParseInt(m["listen-port"][0], 10, 16)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions gobgp/cmd/mrt.go
Expand Up @@ -40,7 +40,7 @@ func injectMrt() error {
fmt.Println("You should probably specify either --no-ipv4 or --no-ipv6 when overwriting nexthop, unless your dump contains only one type of routes")
}

idx := 0
var idx int64
if mrtOpts.QueueSize < 1 {
return fmt.Errorf("Specified queue size is smaller than 1, refusing to run with unbounded memory usage")
}
Expand Down Expand Up @@ -195,12 +195,12 @@ func NewMrtCmd() *cobra.Command {
mrtOpts.Filename = args[0]
if len(args) > 1 {
var err error
mrtOpts.RecordCount, err = strconv.Atoi(args[1])
mrtOpts.RecordCount, err = strconv.ParseInt(args[1], 10, 64)
if err != nil {
exitWithError(fmt.Errorf("invalid count value: %s", args[1]))
}
if len(args) > 2 {
mrtOpts.RecordSkip, err = strconv.Atoi(args[2])
mrtOpts.RecordSkip, err = strconv.ParseInt(args[2], 10, 64)
if err != nil {
exitWithError(fmt.Errorf("invalid skip value: %s", args[2]))
}
Expand Down
13 changes: 6 additions & 7 deletions gobgp/cmd/neighbor.go
Expand Up @@ -989,10 +989,10 @@ func modNeighbor(cmdType string, args []string) error {
}
}

getConf := func(asn int) (*config.Neighbor, error) {
getConf := func(asn uint32) (*config.Neighbor, error) {
peer := &config.Neighbor{
Config: config.NeighborConfig{
PeerAs: uint32(asn),
PeerAs: asn,
},
}
if unnumbered {
Expand Down Expand Up @@ -1032,7 +1032,7 @@ func modNeighbor(cmdType string, args []string) error {
}
}
if option, ok := m["allow-own-as"]; ok {
as, err := strconv.Atoi(option[0])
as, err := strconv.ParseUint(option[0], 10, 8)
if err != nil {
return nil, err
}
Expand All @@ -1054,16 +1054,15 @@ func modNeighbor(cmdType string, args []string) error {
return peer, nil
}

var as int
var as uint64
if len(m["as"]) > 0 {
var err error
as, err = strconv.Atoi(m["as"][0])
if err != nil {
if as, err = strconv.ParseUint(m["as"][0], 10, 32); err != nil {
return err
}
}

n, err := getConf(as)
n, err := getConf(uint32(as))
if err != nil {
return err
}
Expand Down

0 comments on commit 549b703

Please sign in to comment.