From: Lars Schneider <larsxschnei...@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 <larsxschnei...@gmail.com>
---
 pkt-line.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/pkt-line.c b/pkt-line.c
index 19f0271..fc0ac12 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];
+
+       if (size > sizeof(packet_write_buffer) - 4) {
+               return error("packet write failed - data exceeds max packet 
size");
+       }
+       packet_trace(buf, size, 1);
+       size += 4;
+       set_packet_header(packet_write_buffer, size);
+       memcpy(packet_write_buffer + 4, buf, size - 4);
+       if (write_in_full(fd_out, packet_write_buffer, size) == size)
+               return 0;
+       return error("packet write failed");
+}
+
 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
 {
        va_list args;
-- 
2.10.0

Reply via email to