provide an initializer for "struct object_info"
authorJeff King <redacted>
Thu, 11 Aug 2016 09:24:35 +0000 (05:24 -0400)
committerJunio C Hamano <redacted>
Thu, 11 Aug 2016 17:42:23 +0000 (10:42 -0700)
commit27b5c1a0653026645bd6908ee5abfdceb1c95082
tree57d9b7f106d335340624d4fec151c018f467af35
parent56dfeb62638760fa78a442a97f19abf1af374d29
provide an initializer for "struct object_info"

An all-zero initializer is fine for this struct, but because
the first element is a pointer, call sites need to know to
use "NULL" instead of "0". Otherwise some static checkers
like "sparse" will complain; see d099b71 (Fix some sparse
warnings, 2013-07-18) for example.  So let's provide an
initializer to make this easier to get right.

But let's also comment that memset() to zero is explicitly
OK[1]. One of the callers embeds object_info in another
struct which is initialized via memset (expand_data in
builtin/cat-file.c). Since our subset of C doesn't allow
assignment from a compound literal, handling this in any
other way is awkward, so we'd like to keep the ability to
initialize by memset(). By documenting this property, it
should make anybody who wants to change the initializer
think twice before doing so.

There's one other caller of interest. In parse_sha1_header(),
we did not initialize the struct fully in the first place.
This turned out not to be a bug because the sub-function it
calls does not look at any other fields except the ones we
did initialize. But that assumption might not hold in the
future, so it's a dangerous construct. This patch switches
it to initializing the whole struct, which protects us
against unexpected reads of the other fields.

[1] Obviously using memset() to initialize a pointer
    violates the C standard, but we long ago decided that it
    was an acceptable tradeoff in the real world.

Signed-off-by: Jeff King <redacted>
Signed-off-by: Junio C Hamano <redacted>
builtin/cat-file.c
cache.h
sha1_file.c
streaming.c
git clone https://git.99rst.org/PROJECT