Skip to content

Commit f3612ac

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5fe668 commit f3612ac

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

pkt-line.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ static void packet_trace(const char *buf, unsigned int len, int write)
2424
strbuf_addf(&out, "packet: %12s%c ",
2525
packet_trace_prefix, write ? '>' : '<');
2626

27-
if ((len >= 4 && starts_with(buf, "PACK")) ||
28-
(len >= 5 && starts_with(buf+1, "PACK"))) {
27+
if (starts_with(buf, "PACK") || starts_with(buf + 1, "PACK")) {
2928
strbuf_addstr(&out, "PACK ...");
3029
trace_disable(&trace_packet);
3130
}

0 commit comments

Comments
 (0)