git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] progress: don't dereference before checking for NULL

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 10, 2020, 21:26 UTC
Message-ID
<CAPig+cTsxAW=s1iXcK_-Kn+xiSNCm_u_o_Q2xm-0+a_v4qc5_w@mail.gmail.com>
In-Reply-To
<20200810194748.1483784-1-martin.agren@gmail.com>
On Mon, Aug 10, 2020 at 3:48 PM Martin Ågren <martin.agren@gmail.com> wrote:
Show 22 quoted lines
> 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>
> ---
> diff --git a/progress.c b/progress.c
> @@ -319,9 +319,12 @@ static void finish_if_sparse(struct progress *progress)
>  void stop_progress(struct progress **p_progress)
>  {
> +       if (!p_progress)
> +               BUG("don't provide NULL to stop_progress");
> +
>         finish_if_sparse(*p_progress);

I'm wondering what this really buys us over simply crashing due to the NULL dereference (aside from the slightly more informative diagnostic message). Either way, it's going to crash, as it should because passing NULL is indeed a programmer error for these two functions. I'm pretty sure that it is more common in this project simply to allow a programmer error like this simply to crash on its own rather than adding code to make it crash explicitly.

> -       if (p_progress && *p_progress) {
> +       if (*p_progress) {

In other words, I think the entire patch can be reduced to just this change here (and a simplified commit message).

Previous: Martin ÅgrenNext: Martin Ågren
Message 2 of 5 in “progress: don't dereference before checking for NULL”
  1. progress: don't dereference before checking for NULLMartin Ågren, Aug 10, 2020
  2. Eric SunshineAug 10, 2020
  3. Martin ÅgrenAug 11, 2020
  4. Taylor BlauAug 11, 2020
  5. Junio C HamanoAug 10, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.