Skip to content

Commit 27b5c1a

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

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

builtin/cat-file.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
2828
char *buf;
2929
unsigned long size;
3030
struct object_context obj_context;
31-
struct object_info oi = {NULL};
31+
struct object_info oi = OBJECT_INFO_INIT;
3232
struct strbuf sb = STRBUF_INIT;
3333
unsigned flags = LOOKUP_REPLACE_OBJECT;
3434

@@ -378,8 +378,7 @@ static int batch_objects(struct batch_options *opt)
378378
data.mark_query = 0;
379379

380380
if (opt->all_objects) {
381-
struct object_info empty;
382-
memset(&empty, 0, sizeof(empty));
381+
struct object_info empty = OBJECT_INFO_INIT;
383382
if (!memcmp(&data.info, &empty, sizeof(empty)))
384383
data.skip_object_info = 1;
385384
}

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,13 @@ struct object_info {
15421542
} packed;
15431543
} u;
15441544
};
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+
15451552
extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
15461553

15471554
/* Dumb servers support */

sha1_file.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,11 +1730,9 @@ static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
17301730

17311731
int parse_sha1_header(const char *hdr, unsigned long *sizep)
17321732
{
1733-
struct object_info oi;
1733+
struct object_info oi = OBJECT_INFO_INIT;
17341734

17351735
oi.sizep = sizep;
1736-
oi.typename = NULL;
1737-
oi.typep = NULL;
17381736
return parse_sha1_header_extended(hdr, &oi, LOOKUP_REPLACE_OBJECT);
17391737
}
17401738

@@ -2738,7 +2736,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
27382736
int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
27392737
{
27402738
enum object_type type;
2741-
struct object_info oi = {NULL};
2739+
struct object_info oi = OBJECT_INFO_INIT;
27422740

27432741
oi.typep = &type;
27442742
oi.sizep = sizep;

streaming.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ struct git_istream *open_istream(const unsigned char *sha1,
135135
struct stream_filter *filter)
136136
{
137137
struct git_istream *st;
138-
struct object_info oi = {NULL};
138+
struct object_info oi = OBJECT_INFO_INIT;
139139
const unsigned char *real = lookup_replace_object(sha1);
140140
enum input_source src = istream_source(real, type, &oi);
141141

0 commit comments

Comments
 (0)