From: Lars Schneider <larsxschnei...@gmail.com>

format_packet() dies if the caller wants to format a packet larger than
LARGE_PACKET_MAX. Certain callers might prefer an error response instead.

Add a parameter `gentle` to define if the function should signal an error
with the return value (gentle=1) or die (gentle=0).

Signed-off-by: Lars Schneider <larsxschnei...@gmail.com>
---
 pkt-line.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/pkt-line.c b/pkt-line.c
index 9400b47..e6b8410 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -108,7 +108,7 @@ static void set_packet_header(char *buf, const int size)
        #undef hex
 }
 
-static void format_packet(struct strbuf *out, const char *fmt, va_list args)
+static int format_packet(int gentle, struct strbuf *out, const char *fmt, 
va_list args)
 {
        size_t orig_len, n;
 
@@ -117,10 +117,15 @@ static void format_packet(struct strbuf *out, const char 
*fmt, va_list args)
        strbuf_vaddf(out, fmt, args);
        n = out->len - orig_len;
 
-       if (n > LARGE_PACKET_MAX)
-               die("protocol error: impossibly long line");
+       if (n > LARGE_PACKET_MAX) {
+               if (gentle)
+                       return -1;
+               else
+                       die("protocol error: impossibly long line");
+       }
 
        set_packet_header(&out->buf[orig_len], n);
+       return 0;
 }
 
 void packet_write(int fd, const char *fmt, ...)
@@ -130,7 +135,7 @@ void packet_write(int fd, const char *fmt, ...)
 
        strbuf_reset(&buf);
        va_start(args, fmt);
-       format_packet(&buf, fmt, args);
+       format_packet(0, &buf, fmt, args);
        va_end(args);
        packet_trace(buf.buf + 4, buf.len - 4, 1);
        write_or_die(fd, buf.buf, buf.len);
@@ -141,7 +146,7 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, 
...)
        va_list args;
 
        va_start(args, fmt);
-       format_packet(buf, fmt, args);
+       format_packet(0, buf, fmt, args);
        va_end(args);
 }
 
-- 
2.9.2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to