Skip to content

Commit 00a4b03

Browse files
ramsay-jonesgitster
authored andcommitted
read-cache: fix an -Wmaybe-uninitialized warning
The function ce_write_entry() uses a 'self-initialised' variable construct, for the symbol 'saved_namelen', to suppress a gcc '-Wmaybe-uninitialized' warning, given that the warning is a false positive. For the purposes of this discussion, the ce_write_entry() function has three code blocks of interest, that look like so: /* block #1 */ if (ce->ce_flags & CE_STRIP_NAME) { saved_namelen = ce_namelen(ce); ce->ce_namelen = 0; } /* block #2 */ /* * several code blocks that contain, among others, calls * to copy_cache_entry_to_ondisk(ondisk, ce); */ /* block #3 */ if (ce->ce_flags & CE_STRIP_NAME) { ce->ce_namelen = saved_namelen; ce->ce_flags &= ~CE_STRIP_NAME; } The warning implies that gcc thinks it is possible that the first block is not entered, the calls to copy_cache_entry_to_ondisk() could toggle the CE_STRIP_NAME flag on, thereby entering block #3 with saved_namelen unset. However, the copy_cache_entry_to_ondisk() function does not write to ce->ce_flags (it only reads). gcc could easily determine this, since that function is local to this file, but it obviously doesn't. In order to suppress this warning, we make it clear to the reader (human and compiler), that block #3 will only be entered when the first block has been entered, by introducing a new 'stripped_name' boolean variable. We also take the opportunity to change the type of 'saved_namelen' to 'unsigned int' to match ce->ce_namelen. Signed-off-by: Ramsay Jones <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 156e178 commit 00a4b03

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

read-cache.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,13 +2104,15 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
21042104
struct strbuf *previous_name, struct ondisk_cache_entry *ondisk)
21052105
{
21062106
int size;
2107-
int saved_namelen = saved_namelen; /* compiler workaround */
21082107
int result;
2108+
unsigned int saved_namelen;
2109+
int stripped_name = 0;
21092110
static unsigned char padding[8] = { 0x00 };
21102111

21112112
if (ce->ce_flags & CE_STRIP_NAME) {
21122113
saved_namelen = ce_namelen(ce);
21132114
ce->ce_namelen = 0;
2115+
stripped_name = 1;
21142116
}
21152117

21162118
if (ce->ce_flags & CE_EXTENDED)
@@ -2150,7 +2152,7 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
21502152
strbuf_splice(previous_name, common, to_remove,
21512153
ce->name + common, ce_namelen(ce) - common);
21522154
}
2153-
if (ce->ce_flags & CE_STRIP_NAME) {
2155+
if (stripped_name) {
21542156
ce->ce_namelen = saved_namelen;
21552157
ce->ce_flags &= ~CE_STRIP_NAME;
21562158
}

0 commit comments

Comments
 (0)