[v2,3/4] pack-objects: fix maybe-uninitialized warning for index_pos
diff mbox series

Message ID 20190905224833.24913-3-s-beyer@gmx.net
State New
Headers show
  • [v2,1/4] am: fail if no author line is given in --rebasing mode
Related show

Commit Message

Stephan Beyer Sept. 5, 2019, 10:48 p.m. UTC
gcc 9.2.1 with -flto shows a maybe-uninitialized warning for index_pos
in builtin/pack-objects.c's add_object_entry().  Tracking it down,
the variable should be initialized in pack_objects.c's packlist_find().

The return value of locate_object_entry_hash(), which becomes index_pos,
is either (in case of found = 1) the position where the (already included)
OID is, or (in case of found = 0), index_pos is the position where the
(not yet included) OID will be after insertion (which takes place in
packlist_alloc() if the hash table is still large enough).

However, packlist_find() does not invoke locate_object_entry_hash() if
the index size is zero (which might be the case on the first run).
This is the only case where index_pos is undefined; and it is irrelevant
since the first run will increase the size of the hash table to 1024 and
then the undefined value index_pos is ignored.

This patch sets index_pos to zero on the first run to silence the warning.

Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
 pack-objects.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)


diff mbox series

diff --git a/pack-objects.c b/pack-objects.c
index 52560293b6..726147a75d 100644
--- a/pack-objects.c
+++ b/pack-objects.c
@@ -74,8 +74,11 @@  struct object_entry *packlist_find(struct packing_data *pdata,
 	uint32_t i;
 	int found;

-	if (!pdata->index_size)
+	if (!pdata->index_size) {
+		if (index_pos)
+			*index_pos = 0; /* silence uninitialized warning */
 		return NULL;
+	}

 	i = locate_object_entry_hash(pdata, oid, &found);