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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 11, 2016 at 05:24 UTC 27b5c1a0653026645bd6908ee5abfdceb1c95082
4 files changed +12 -8
builtin/cat-file.c
+2 -3
@@ -28,7 +28,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
28 char *buf;
29 unsigned long size;
30 struct object_context obj_context;
31 - struct object_info oi = {NULL};
31 + struct object_info oi = OBJECT_INFO_INIT;
32 struct strbuf sb = STRBUF_INIT;
33 unsigned flags = LOOKUP_REPLACE_OBJECT;
34
@@ -378,8 +378,7 @@ static int batch_objects(struct batch_options *opt)
378 data.mark_query = 0;
379
380 if (opt->all_objects) {
381 - struct object_info empty;
382 - memset(&empty, 0, sizeof(empty));
381 + struct object_info empty = OBJECT_INFO_INIT;
382 if (!memcmp(&data.info, &empty, sizeof(empty)))
383 data.skip_object_info = 1;
384 }
cache.h
+7
@@ -1542,6 +1542,13 @@ struct object_info {
1542 } packed;
1543 } u;
1544 };
1545 +
1546 +/*
1547 + * Initializer for a "struct object_info" that wants no items. You may
1548 + * also memset() the memory to all-zeroes.
1549 + */
1550 +#define OBJECT_INFO_INIT {NULL}
1551 +
1552 extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
1553
1554 /* Dumb servers support */
sha1_file.c
+2 -4
@@ -1730,11 +1730,9 @@ static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1730
1731 int parse_sha1_header(const char *hdr, unsigned long *sizep)
1732 {
1733 - struct object_info oi;
1733 + struct object_info oi = OBJECT_INFO_INIT;
1734
1735 oi.sizep = sizep;
1736 - oi.typename = NULL;
1737 - oi.typep = NULL;
1736 return parse_sha1_header_extended(hdr, &oi, LOOKUP_REPLACE_OBJECT);
1737 }
1738
@@ -2738,7 +2736,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
2736 int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
2737 {
2738 enum object_type type;
2741 - struct object_info oi = {NULL};
2739 + struct object_info oi = OBJECT_INFO_INIT;
2740
2741 oi.typep = &type;
2742 oi.sizep = sizep;
streaming.c
+1 -1
@@ -135,7 +135,7 @@ struct git_istream *open_istream(const unsigned char *sha1,
135 struct stream_filter *filter)
136 {
137 struct git_istream *st;
138 - struct object_info oi = {NULL};
138 + struct object_info oi = OBJECT_INFO_INIT;
139 const unsigned char *real = lookup_replace_object(sha1);
140 enum input_source src = istream_source(real, type, &oi);
141