git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] progress: don't dereference before checking for NULL
@ 2020-08-10 19:47 Martin Ågren
  2020-08-10 21:26 ` Eric Sunshine
  2020-08-10 21:57 ` Junio C Hamano
  0 siblings, 2 replies; 5+ messages in thread
From: Martin Ågren @ 2020-08-10 19:47 UTC (permalink / raw)
  To: git; +Cc: Emily Shaffer

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>
---
 progress.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/progress.c b/progress.c
index 3eda914518..31014e6fca 100644
--- 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);
 
-	if (p_progress && *p_progress) {
+	if (*p_progress) {
 		trace2_data_intmax("progress", the_repository, "total_objects",
 				   (*p_progress)->total);
 
@@ -338,7 +341,12 @@ void stop_progress(struct progress **p_progress)
 
 void stop_progress_msg(struct progress **p_progress, const char *msg)
 {
-	struct progress *progress = *p_progress;
+	struct progress *progress;
+
+	if (!p_progress)
+		BUG("don't provide NULL to stop_progress_msg");
+
+	progress = *p_progress;
 	if (!progress)
 		return;
 	*p_progress = NULL;
-- 
2.28.0.220.ged08abb693


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] progress: don't dereference before checking for NULL
  2020-08-10 19:47 [PATCH] progress: don't dereference before checking for NULL Martin Ågren
@ 2020-08-10 21:26 ` Eric Sunshine
  2020-08-11  4:28   ` Martin Ågren
  2020-08-10 21:57 ` Junio C Hamano
  1 sibling, 1 reply; 5+ messages in thread
From: Eric Sunshine @ 2020-08-10 21:26 UTC (permalink / raw)
  To: Martin Ågren; +Cc: Git List, Emily Shaffer

On Mon, Aug 10, 2020 at 3:48 PM Martin Ågren <martin.agren@gmail.com> wrote:
> 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).

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] progress: don't dereference before checking for NULL
  2020-08-10 19:47 [PATCH] progress: don't dereference before checking for NULL Martin Ågren
  2020-08-10 21:26 ` Eric Sunshine
@ 2020-08-10 21:57 ` Junio C Hamano
  1 sibling, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2020-08-10 21:57 UTC (permalink / raw)
  To: Martin Ågren; +Cc: git, Emily Shaffer

Martin Ågren <martin.agren@gmail.com> writes:

> 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.

True, true.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] progress: don't dereference before checking for NULL
  2020-08-10 21:26 ` Eric Sunshine
@ 2020-08-11  4:28   ` Martin Ågren
  2020-08-11 15:41     ` Taylor Blau
  0 siblings, 1 reply; 5+ messages in thread
From: Martin Ågren @ 2020-08-11  4:28 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Emily Shaffer

On Mon, 10 Aug 2020 at 23:27, Eric Sunshine <sunshine@sunshineco.com> wrote:
>
> On Mon, Aug 10, 2020 at 3:48 PM Martin Ågren <martin.agren@gmail.com> wrote:
> >  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.

I'm not a big fan of undefined behavior. In general, I don't buy the
"but in practice it will do what we want" argumentation.

Before 98a1364740 ("trace2: log progress time and throughput",
2020-05-12), we didn't check for NULL in this function. Then that commit
tried to do so. It would feel wrong for me to say "that commit didn't
get it quite right -- rip out the check". Then, to be honest, I'd much
rather just leave it in place. At least that way, someone else might
spot it a year from now.

I could add an early return (instead of an early BUG). That would
gracefully handle NULL. Grepping around suggests there are other `if (!p)
BUG();`. Even Documentation/CodingGuidelines BUGs on a NULL-pointer,
although in the context of checking for NULL (as opposed to "how to
BUG").

> > -       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).

I started with this, but then I felt terrible for just sweeping the
whole thing under the rug.

Martin

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] progress: don't dereference before checking for NULL
  2020-08-11  4:28   ` Martin Ågren
@ 2020-08-11 15:41     ` Taylor Blau
  0 siblings, 0 replies; 5+ messages in thread
From: Taylor Blau @ 2020-08-11 15:41 UTC (permalink / raw)
  To: Martin Ågren; +Cc: Eric Sunshine, Git List, Emily Shaffer

On Tue, Aug 11, 2020 at 06:28:30AM +0200, Martin Ågren wrote:
> On Mon, 10 Aug 2020 at 23:27, Eric Sunshine <sunshine@sunshineco.com> wrote:
> >
> > On Mon, Aug 10, 2020 at 3:48 PM Martin Ågren <martin.agren@gmail.com> wrote:
> > >  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.
>
> I'm not a big fan of undefined behavior. In general, I don't buy the
> "but in practice it will do what we want" argumentation.

I think that this is good reasoning; having the guard around
'p_progress' being non-NULL makes the implementation have no undefined
behavior, which is worth a lot.

> Before 98a1364740 ("trace2: log progress time and throughput",
> 2020-05-12), we didn't check for NULL in this function. Then that commit
> tried to do so. It would feel wrong for me to say "that commit didn't
> get it quite right -- rip out the check". Then, to be honest, I'd much
> rather just leave it in place. At least that way, someone else might
> spot it a year from now.
>
> I could add an early return (instead of an early BUG). That would
> gracefully handle NULL. Grepping around suggests there are other `if (!p)
> BUG();`. Even Documentation/CodingGuidelines BUGs on a NULL-pointer,
> although in the context of checking for NULL (as opposed to "how to
> BUG").
>
> > > -       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).
>
> I started with this, but then I felt terrible for just sweeping the
> whole thing under the rug.

Yep, I'm a fan of the direction you ended up taking. Thanks.

  Reviewed-by: Taylor Blau <me@ttaylorr.com>

> Martin

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-08-11 15:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-10 19:47 [PATCH] progress: don't dereference before checking for NULL Martin Ågren
2020-08-10 21:26 ` Eric Sunshine
2020-08-11  4:28   ` Martin Ågren
2020-08-11 15:41     ` Taylor Blau
2020-08-10 21:57 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).