Commit abf24a30fd4a78373f62ecafa36bd9d1d89489d0

Patrick Steinhardt 2019-06-27T15:25:17

examples: avoid conversion warnings when calculating progress When computing the progress, we perform some arithmetics that are implicitly converting from `size_t` to `int`. In one case we're calclulating a percentage, so we know that it should always be in the range of [0,100] and thus we're fine. In the other case we convert from bytes to kilobytes -- this should be stored in a `size_t` to avoid loss of precision, even though it probably won't matter due to limited download rates.

diff --git a/examples/clone.c b/examples/clone.c
index ee403e1..26ad12b 100644
--- a/examples/clone.c
+++ b/examples/clone.c
@@ -17,9 +17,9 @@ static void print_progress(const progress_data *pd)
 		0;
 
 	int checkout_percent = pd->total_steps > 0
-		? (100 * pd->completed_steps) / pd->total_steps
+		? (int)((100 * pd->completed_steps) / pd->total_steps)
 		: 0;
-	int kbytes = pd->fetch_progress.received_bytes / 1024;
+	size_t kbytes = pd->fetch_progress.received_bytes / 1024;
 
 	if (pd->fetch_progress.total_objects &&
 		pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) {
@@ -27,7 +27,7 @@ static void print_progress(const progress_data *pd)
 		       pd->fetch_progress.indexed_deltas,
 		       pd->fetch_progress.total_deltas);
 	} else {
-		printf("net %3d%% (%4d kb, %5d/%5d)  /  idx %3d%% (%5d/%5d)  /  chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
+		printf("net %3d%% (%4"PRIuZ" kb, %5d/%5d)  /  idx %3d%% (%5d/%5d)  /  chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
 		   network_percent, kbytes,
 		   pd->fetch_progress.received_objects, pd->fetch_progress.total_objects,
 		   index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects,