Skip to content

Commit

Permalink
Merge bitcoin#19818: p2p: change CInv::type from int to `uint32_t…
Browse files Browse the repository at this point in the history
…`, fix UBSan warning

7984c39 test framework: serialize/deserialize inv type as unsigned int (Jon Atack)
407175e p2p: change CInv::type from int to uint32_t (Jon Atack)

Pull request description:

  Fixes UBSan implicit-integer-sign-change issue per bitcoin#19610 (comment).

  Credit to Crypt-iQ for finding and reporting the issue and to vasild for the original review suggestion in bitcoin#19590 (review).

  Closes bitcoin#19678.

ACKs for top commit:
  laanwj:
    ACK 7984c39
  MarcoFalke:
    ACK 7984c39 🌻
  vasild:
    ACK 7984c39

Tree-SHA512: 59f3a75f40ce066ca6f0bb1927197254238302b4073af1574bdbfe6ed580876437be804be4e47d51467d604f0d9e3a5875159f7f2edbb2351fdb2bb9465100b5
  • Loading branch information
laanwj authored and knst committed Jan 22, 2024
1 parent 71961be commit 007da48
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ CInv::CInv()
hash.SetNull();
}

CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
CInv::CInv(uint32_t typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}

bool operator<(const CInv& a, const CInv& b)
{
Expand Down
4 changes: 2 additions & 2 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ class CInv
{
public:
CInv();
CInv(int typeIn, const uint256& hashIn);
CInv(uint32_t typeIn, const uint256& hashIn);

SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }

Expand All @@ -555,7 +555,7 @@ class CInv
const char* GetCommandInternal() const;

public:
int type;
uint32_t type;
uint256 hash;
};

Expand Down
6 changes: 3 additions & 3 deletions test/functional/test_framework/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,20 @@ class CInv:
MSG_TX: "TX",
MSG_BLOCK: "Block",
MSG_FILTERED_BLOCK: "filtered Block",
20: "CompactBlock"
MSG_CMPCT_BLOCK: "CompactBlock",
}

def __init__(self, t=0, h=0):
self.type = t
self.hash = h

def deserialize(self, f):
self.type = struct.unpack("<i", f.read(4))[0]
self.type = struct.unpack("<I", f.read(4))[0]
self.hash = deser_uint256(f)

def serialize(self):
r = b""
r += struct.pack("<i", self.type)
r += struct.pack("<I", self.type)
r += ser_uint256(self.hash)
return r

Expand Down

0 comments on commit 007da48

Please sign in to comment.