From: Jeff King Date: Fri, 12 Jun 2015 21:28:08 +0000 (-0400) Subject: pkt-line: simplify starts_with checks in packet tracing X-Git-Tag: v2.6.0-rc0~107^2~2 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/f3612acb9302fc9332958b77c5ca5fc05cacb029?ds=sidebyside;hp=a5fe66802f8c4036badd54ff36ff327d43236e7e pkt-line: simplify starts_with checks in packet tracing We carefully check that our pkt buffer has enough characters before seeing if it starts with "PACK". The intent is to avoid reading random memory if we get a short buffer like "PAC". However, we know that the traced packets are always NUL-terminated. They come from one of these sources: 1. A string literal. 2. `format_packet`, which uses a strbuf. 3. `packet_read`, which defensively NUL-terminates what we read. We can therefore drop the length checks, as we know we will hit the trailing NUL if we have a short input. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- diff --git a/pkt-line.c b/pkt-line.c index 187a2293e7..0477d2eefe 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -24,8 +24,7 @@ static void packet_trace(const char *buf, unsigned int len, int write) strbuf_addf(&out, "packet: %12s%c ", packet_trace_prefix, write ? '>' : '<'); - if ((len >= 4 && starts_with(buf, "PACK")) || - (len >= 5 && starts_with(buf+1, "PACK"))) { + if (starts_with(buf, "PACK") || starts_with(buf + 1, "PACK")) { strbuf_addstr(&out, "PACK ..."); trace_disable(&trace_packet); }