> On 04 Oct 2016, at 21:33, Junio C Hamano <[email protected]> wrote:
>
> [email protected] writes:
>
>> From: Lars Schneider <[email protected]>
>>
>>
>> +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