]> git.ipfire.org Git - thirdparty/git.git/commit - builtin/index-pack.c
index-pack: make pointer-alias fallbacks safer
authorJeff King <peff@peff.net>
Thu, 16 Mar 2017 14:27:20 +0000 (10:27 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Mar 2017 18:33:43 +0000 (11:33 -0700)
commitf20754802a280c57a1e5886605b6805bbf040c63
treed25588005dd09e4f03cc851ee1ab39f8854b8e05
parentba47a3088f04ac3d2833bea56ee366be1054db8d
index-pack: make pointer-alias fallbacks safer

The final() function accepts a NULL value for certain
parameters, and falls back to writing into a reusable "name"
buffer, and then either:

  1. For "keep_name", requiring all uses to do "keep_name ?
     keep_name : name.buf". This is awkward, and it's easy
     to accidentally look at the maybe-NULL keep_name.

  2. For "final_index_name" and "final_pack_name", aliasing
     those pointers to the "name" buffer. This is easier to
     use, but the aliased pointers become invalid after the
     buffer is reused (this isn't a bug now, but it's a
     potential pitfall).

One way to make this safer would be to introduce an extra
pointer to do the aliasing, and have its lifetime match the
validity of the "name" buffer. But it's still easy to
accidentally use the wrong name (i.e., to use
"final_pack_name" instead of the aliased pointer).

Instead, let's use three separate buffers that will remain
valid through the function. That makes it safe to alias the
pointers and use them consistently. The extra allocations
shouldn't matter, as this function is not performance
sensitive.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/index-pack.c