larsxschneider@xxxxxxxxx writes: > From: Lars Schneider <larsxschneider@xxxxxxxxx> > > 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@xxxxxxxxx> > Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx> > --- > 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? > + 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;