Re: [PATCH v9 09/14] pkt-line: add packet_write_gently()
- From
Lars Schneider <larsxschneider@gmail.com>
- Date
- Oct 5, 2016, 19:06 UTC
- Message-ID
- <B1968DDD-7EAA-477B-B088-5F2A83F29C8C@gmail.com>
- In-Reply-To
- <xmqqd1jfucwo.fsf@gitster.mtv.corp.google.com>
Show 20 quoted lines
> On 04 Oct 2016, at 21:33, Junio C Hamano <gitster@pobox.com> wrote:
>
> larsxschneider@gmail.com writes:
>
>> From: Lars Schneider <larsxschneider@gmail.com>
>>
>>
>> +static int packet_write_gently(const int fd_out, const char *buf, size_t size)
>> +{
>> + static char packet_write_buffer[LARGE_PACKET_MAX];
>> + const size_t packet_size = size + 4;
>> +
>> + if (packet_size > sizeof(packet_write_buffer))
>> + return error("packet write failed - data exceeds max packet size");
>
> Hmph, in the previous round, this used to be "is the size larger
> than sizeof(..) - 4?", which avoided integer overflow issue rather
> nicely and more idiomatic. If size is near the size_t's max,
> packet_size may wrap around to become very small, and we won't hit
> this error, will we?You are right. Would the solution below be acceptable? I would like to keep the `packet_size` variable as it eases the rest of the function.
const size_t packet_size = size + 4;
- if (packet_size > sizeof(packet_write_buffer))
+ if (size > sizeof(packet_write_buffer) - 4)
return error("packet write failed - data exceeds max packet size");Thanks, Lars