Skip to content

Commit

Permalink
pkt-line: simplify starts_with checks in packet tracing
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Jun 15, 2015
1 parent a5fe668 commit f3612ac
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions pkt-line.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit f3612ac

Please sign in to comment.