string to int signedness cast "uint64(id)", might result in lost or misinterpreted data
1948 idStr, role = splits[0], placement.PeerRoleType(splits[1])
1949 }
1950 id, _ := strconv.Atoi(idStr)
1951 peer := &metapb.Peer{Id: uint64(id), StoreId: uint64(id), Role: role.MetaPeerRole()}1952 regionMeta.Peers = append(regionMeta.Peers, peer)
1953 if role == placement.Leader {
1954 leader = peer
string to int signedness cast "uint64(id)", might result in lost or misinterpreted data
1948 idStr, role = splits[0], placement.PeerRoleType(splits[1])
1949 }
1950 id, _ := strconv.Atoi(idStr)
1951 peer := &metapb.Peer{Id: uint64(id), StoreId: uint64(id), Role: role.MetaPeerRole()}1952 regionMeta.Peers = append(regionMeta.Peers, peer)
1953 if role == placement.Leader {
1954 leader = peer
Description
It is possible for integer casting, if not appropriately done to lose the signedness of the integer and might cause overflow as well.
For example:
- int64
must not be casted to: "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32"
int64
can lose signedness if converted to unsigned integer and if converted to
a signed integer but a type other than int64
, then overflow might occur.
uint64
must not be casted to: "uint8", "uint16", "uint32", "int8", "int16", "int32", "int64"uint64
would lose signedness if converted to a signed integer and might overflow. If converted to an unsigned integer, i.e., a type other thanuint64
, overflow might occur.
Bad practice
n, err := strconv.Atoi("42")
if err != nil {
panic(err)
}
// `n` is of type `int`, so it is either `int64` or `int32` depending on the platform.
// Casting it to `uint8` might result in loss of signedness of the integer and might
// not give expected results.
_ = uint8(n)