Fix #9361, a2051ba: [Network] Off by one in CanWriteToPacket #9365
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Motivation / Problem
Fixes #9361. Also partly fixing a2051ba, though it was also wrong before as well in some parts.
The old implementation (pre a2051ba) of
Send_uint*
/Send_bool
function ofPacket
didassert(this->size < SEND_MTU - sizeof(data));
. After a2051ba this effectively becameassert(this->size + sizeof(data) < SEND_MTU);
, which is still the same functionality wise, and that was the logicCanWriteToPacket
was based upon.However, other functions such as
Send_string
and custom checks in other places did things likeassert(this->size + strlen(data) + 1 <= SEND_MTU);
. In a2051ba they lost the=
of<=
, whereas theSend_uint
ones should have actually gained the=
as they were wrong/off-by-one.Note: this is an off-by-one in the safe direction, as it would just prevent the last valid byte of the packet to be used by
Send_uint*
. It is NOT writing beyond its buffer.Description
Change
bytes_in_packet + bytes_to_add < MAX_BYTES_IN_PACKET
tobytes_in_packet + bytes_to_add <= MAX_BYTES_IN_PACKET
. Presumebytes_in_packet
to be 1,bytes_to_add
to be 2 andMAX_BYTES_IN_PACKET
to be 3. Then the two bytes can be written to the packet, whereas the previousCanWriteToPacket
would not allow it anymore.Limitations
None
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.