Re: [PATCH v9 09/14] pkt-line: add packet_write_gently()
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 4, 2016, 19:33 UTC
- Message-ID
- <xmqqd1jfucwo.fsf@gitster.mtv.corp.google.com>
- In-Reply-To
- <20161004125947.67104-10-larsxschneider@gmail.com>
larsxschneider@gmail.com writes:
Show 31 quoted lines
> From: Lars Schneider <larsxschneider@gmail.com>
>
> packet_write_fmt_gently() uses format_packet() which lets the caller
> only send string data via "%s". That means it cannot be used for
> arbitrary data that may contain NULs.
>
> Add packet_write_gently() which writes arbitrary data and does not die
> in case of an error. The function is used by other pkt-line functions in
> a subsequent patch.
>
> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
> pkt-line.c | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
> diff --git a/pkt-line.c b/pkt-line.c
> index 286eb09..3fd4dc0 100644
> --- a/pkt-line.c
> +++ b/pkt-line.c
> @@ -171,6 +171,22 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)
> return status;
> }
>
> +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?
Show 11 quoted lines
> + packet_trace(buf, size, 1);
> + set_packet_header(packet_write_buffer, packet_size);
> + memcpy(packet_write_buffer + 4, buf, size);
> + if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)
> + return 0;
> + return error("packet write failed");
> +}
> +
> void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
> {
> va_list args;