@matheusbsbs / matheus-bagliano / commits / ac900fddb7
Fork 0

progress: don't dereference before checking for NULL

In `stop_progress()`, we're careful to check that `p_progress` is non-NULL before we dereference it, but by then we have already dereferenced it when calling `finish_if_sparse(*p_progress)`. And, for what it's worth, we'll go on to blindly dereference it again inside `stop_progress_msg()`. We could return early if we get a NULL-pointer, but let's go one step further and BUG instead. The progress API handles NULL just fine, but that's the NULL-ness of `*p_progress`, e.g., when running with `--no-progress`. If `p_progress` is NULL, chances are that's a mistake. For symmetry, let's do the same check in `stop_progress_msg()`, too. Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Martin Ågren committed Aug 10, 2020 at 21:47 UTC ac900fddb7fd3cc760ea5c1b79a50ad4564e4a0d
1 file changed +10 -2
progress.c
+10 -2
@@ -319,9 +319,12 @@ static void finish_if_sparse(struct progress *progress)
319
320 void stop_progress(struct progress **p_progress)
321 {
322 + if (!p_progress)
323 + BUG("don't provide NULL to stop_progress");
324 +
325 finish_if_sparse(*p_progress);
326
324 - if (p_progress && *p_progress) {
327 + if (*p_progress) {
328 trace2_data_intmax("progress", the_repository, "total_objects",
329 (*p_progress)->total);
330
@@ -342,7 +345,12 @@ void stop_progress(struct progress **p_progress)
345
346 void stop_progress_msg(struct progress **p_progress, const char *msg)
347 {
345 - struct progress *progress = *p_progress;
348 + struct progress *progress;
349 +
350 + if (!p_progress)
351 + BUG("don't provide NULL to stop_progress_msg");
352 +
353 + progress = *p_progress;
354 if (!progress)
355 return;
356 *p_progress = NULL;