git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge
@ 2023-04-20  7:14 Elijah Newren via GitGitGadget
  2023-04-20 13:10 ` Derrick Stolee
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Elijah Newren via GitGitGadget @ 2023-04-20  7:14 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, Elijah Newren

From: Elijah Newren <newren@gmail.com>

If some code sets up the data structures for a merge, but then never
actually performs one before calling merge_finalize(), then
merge_finalize() wouldn't notice that result->priv was NULL and
return early, resulting in following that NULL pointer and getting
a segfault.  There is currently no code in the git codebase that does
this, but this issue was found during testing of some proposed patches
that had the following structure:

    struct merge_options merge_opt;
    struct merge_result result;

    init_merge_options(&merge_opt, the_repository);
    memset(&result, 0, sizeof(result));

    <do N merges, for some value of N>

    merge_finalize(&merge_opt, &result);

where some flags could cause the code to have N=0, i.e. doing no merges.
Add a check for result->priv being NULL and return early to avoid a
segfault in these kinds of cases.

While at it, ensure the FREE_AND_NULL() in the function does something
useful with the nulling aspect, namely sets result->priv to NULL rather
than a mere temporary.

Reported-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
---
    merge-ort: fix calling merge_finalize() with no intermediate merge
    
    See
    https://lore.kernel.org/git/CABPp-BHCdjOutYqdMO1NbYKNA0BgkXRgwUEKK=MX0kXM-5G_DQ@mail.gmail.com/

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1518%2Fnewren%2Ffix-merge-finalize-with-no-merge-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1518/newren/fix-merge-finalize-with-no-merge-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1518

 merge-ort.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/merge-ort.c b/merge-ort.c
index 5bf64354d16..cba3662e497 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -4718,14 +4718,14 @@ void merge_switch_to_result(struct merge_options *opt,
 void merge_finalize(struct merge_options *opt,
 		    struct merge_result *result)
 {
-	struct merge_options_internal *opti = result->priv;
-
 	if (opt->renormalize)
 		git_attr_set_direction(GIT_ATTR_CHECKIN);
 	assert(opt->priv == NULL);
 
-	clear_or_reinit_internal_opts(opti, 0);
-	FREE_AND_NULL(opti);
+	if (!result->priv)
+		return;
+	clear_or_reinit_internal_opts(result->priv, 0);
+	FREE_AND_NULL(result->priv);
 }
 
 /*** Function Grouping: helper functions for merge_incore_*() ***/

base-commit: 667fcf4e15379790f0b609d6a83d578e69f20301
-- 
gitgitgadget

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

* Re: [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge
  2023-04-20  7:14 [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge Elijah Newren via GitGitGadget
@ 2023-04-20 13:10 ` Derrick Stolee
  2023-04-20 17:10   ` Junio C Hamano
  2023-04-22  2:04   ` Elijah Newren
  2023-04-20 16:11 ` Junio C Hamano
  2023-04-22 20:22 ` [PATCH v2] " Elijah Newren via GitGitGadget
  2 siblings, 2 replies; 7+ messages in thread
From: Derrick Stolee @ 2023-04-20 13:10 UTC (permalink / raw)
  To: Elijah Newren via GitGitGadget, git; +Cc: Elijah Newren

On 4/20/2023 3:14 AM, Elijah Newren via GitGitGadget wrote:
> From: Elijah Newren <newren@gmail.com>

> While at it, ensure the FREE_AND_NULL() in the function does something
> useful with the nulling aspect, namely sets result->priv to NULL rather
> than a mere temporary.

Good call. It also makes the code look better.
 
>  void merge_finalize(struct merge_options *opt,
>  		    struct merge_result *result)
>  {
> -	struct merge_options_internal *opti = result->priv;
> -
>  	if (opt->renormalize)
>  		git_attr_set_direction(GIT_ATTR_CHECKIN);
>  	assert(opt->priv == NULL);
>  
> -	clear_or_reinit_internal_opts(opti, 0);
> -	FREE_AND_NULL(opti);
> +	if (!result->priv)
> +		return;
> +	clear_or_reinit_internal_opts(result->priv, 0);
> +	FREE_AND_NULL(result->priv);

Perhaps this would be better as

	if (result->priv) {
		clear_or_reinit_internal_opts(result->priv, 0);
		FREE_AND_NULL(result->priv);
	}

to avoid an accidental addition of code to the end of this
method that doesn't depend on result->priv?

Thanks,
-Stolee

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

* Re: [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge
  2023-04-20  7:14 [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge Elijah Newren via GitGitGadget
  2023-04-20 13:10 ` Derrick Stolee
@ 2023-04-20 16:11 ` Junio C Hamano
  2023-04-22 20:22 ` [PATCH v2] " Elijah Newren via GitGitGadget
  2 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2023-04-20 16:11 UTC (permalink / raw)
  To: Elijah Newren via GitGitGadget; +Cc: git, Elijah Newren

"Elijah Newren via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Elijah Newren <newren@gmail.com>
>
> If some code sets up the data structures for a merge, but then never
> actually performs one before calling merge_finalize(), then
> merge_finalize() wouldn't notice that result->priv was NULL and
> return early, resulting in following that NULL pointer and getting
> a segfault.  There is currently no code in the git codebase that does
> this, but this issue was found during testing of some proposed patches
> that had the following structure:
>
>     struct merge_options merge_opt;
>     struct merge_result result;
>
>     init_merge_options(&merge_opt, the_repository);
>     memset(&result, 0, sizeof(result));
>
>     <do N merges, for some value of N>
>
>     merge_finalize(&merge_opt, &result);
>
> where some flags could cause the code to have N=0, i.e. doing no merges.
> Add a check for result->priv being NULL and return early to avoid a
> segfault in these kinds of cases.
>
> While at it, ensure the FREE_AND_NULL() in the function does something
> useful with the nulling aspect, namely sets result->priv to NULL rather
> than a mere temporary.

Clearly written.  The FREE_AND_NULL() bit is a bit embarrassing
slip-up for everybody who reviewed and applied the initial patch
X-<.

And we need to undo the attribute direction change in merge_start()
even if we didn't perform any, so the early return cannot be done
before that.  So everything makes sense as a fix to the reported
problem.

> Reported-by: Derrick Stolee <derrickstolee@github.com>
> Signed-off-by: Elijah Newren <newren@gmail.com>
> ---
>     merge-ort: fix calling merge_finalize() with no intermediate merge
>     
>     See
>     https://lore.kernel.org/git/CABPp-BHCdjOutYqdMO1NbYKNA0BgkXRgwUEKK=MX0kXM-5G_DQ@mail.gmail.com/
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1518%2Fnewren%2Ffix-merge-finalize-with-no-merge-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1518/newren/fix-merge-finalize-with-no-merge-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/1518
>
>  merge-ort.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/merge-ort.c b/merge-ort.c
> index 5bf64354d16..cba3662e497 100644
> --- a/merge-ort.c
> +++ b/merge-ort.c
> @@ -4718,14 +4718,14 @@ void merge_switch_to_result(struct merge_options *opt,
>  void merge_finalize(struct merge_options *opt,
>  		    struct merge_result *result)
>  {
> -	struct merge_options_internal *opti = result->priv;
> -
>  	if (opt->renormalize)
>  		git_attr_set_direction(GIT_ATTR_CHECKIN);
>  	assert(opt->priv == NULL);
>  
> -	clear_or_reinit_internal_opts(opti, 0);
> -	FREE_AND_NULL(opti);
> +	if (!result->priv)
> +		return;
> +	clear_or_reinit_internal_opts(result->priv, 0);
> +	FREE_AND_NULL(result->priv);
>  }
>  
>  /*** Function Grouping: helper functions for merge_incore_*() ***/
>
> base-commit: 667fcf4e15379790f0b609d6a83d578e69f20301

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

* Re: [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge
  2023-04-20 13:10 ` Derrick Stolee
@ 2023-04-20 17:10   ` Junio C Hamano
  2023-04-22  2:04   ` Elijah Newren
  1 sibling, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2023-04-20 17:10 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: Elijah Newren via GitGitGadget, git, Elijah Newren

Derrick Stolee <derrickstolee@github.com> writes:

> On 4/20/2023 3:14 AM, Elijah Newren via GitGitGadget wrote:
>> From: Elijah Newren <newren@gmail.com>
>
>> While at it, ensure the FREE_AND_NULL() in the function does something
>> useful with the nulling aspect, namely sets result->priv to NULL rather
>> than a mere temporary.
>
> Good call. It also makes the code look better.

And more correct, if the NULLness of result->priv matters.

> Perhaps this would be better as
>
> 	if (result->priv) {
> 		clear_or_reinit_internal_opts(result->priv, 0);
> 		FREE_AND_NULL(result->priv);
> 	}
>
> to avoid an accidental addition of code to the end of this
> method that doesn't depend on result->priv?

It does sound like a good idea.

Thanks.

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

* Re: [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge
  2023-04-20 13:10 ` Derrick Stolee
  2023-04-20 17:10   ` Junio C Hamano
@ 2023-04-22  2:04   ` Elijah Newren
  1 sibling, 0 replies; 7+ messages in thread
From: Elijah Newren @ 2023-04-22  2:04 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: Elijah Newren via GitGitGadget, git

On Thu, Apr 20, 2023 at 6:10 AM Derrick Stolee <derrickstolee@github.com> wrote:
>
> On 4/20/2023 3:14 AM, Elijah Newren via GitGitGadget wrote:
> > From: Elijah Newren <newren@gmail.com>
>
> > While at it, ensure the FREE_AND_NULL() in the function does something
> > useful with the nulling aspect, namely sets result->priv to NULL rather
> > than a mere temporary.
>
> Good call. It also makes the code look better.
>
> >  void merge_finalize(struct merge_options *opt,
> >                   struct merge_result *result)
> >  {
> > -     struct merge_options_internal *opti = result->priv;
> > -
> >       if (opt->renormalize)
> >               git_attr_set_direction(GIT_ATTR_CHECKIN);
> >       assert(opt->priv == NULL);
> >
> > -     clear_or_reinit_internal_opts(opti, 0);
> > -     FREE_AND_NULL(opti);
> > +     if (!result->priv)
> > +             return;
> > +     clear_or_reinit_internal_opts(result->priv, 0);
> > +     FREE_AND_NULL(result->priv);
>
> Perhaps this would be better as
>
>         if (result->priv) {
>                 clear_or_reinit_internal_opts(result->priv, 0);
>                 FREE_AND_NULL(result->priv);
>         }
>
> to avoid an accidental addition of code to the end of this
> method that doesn't depend on result->priv?

Ooh, yes, that'd be a good improvement; thanks.

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

* [PATCH v2] merge-ort: fix calling merge_finalize() with no intermediate merge
  2023-04-20  7:14 [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge Elijah Newren via GitGitGadget
  2023-04-20 13:10 ` Derrick Stolee
  2023-04-20 16:11 ` Junio C Hamano
@ 2023-04-22 20:22 ` Elijah Newren via GitGitGadget
  2023-04-24 15:17   ` Derrick Stolee
  2 siblings, 1 reply; 7+ messages in thread
From: Elijah Newren via GitGitGadget @ 2023-04-22 20:22 UTC (permalink / raw)
  To: git; +Cc: Derrick Stolee, Elijah Newren, Elijah Newren, Elijah Newren

From: Elijah Newren <newren@gmail.com>

If some code sets up the data structures for a merge, but then never
actually performs one before calling merge_finalize(), then
merge_finalize() wouldn't notice that result->priv was NULL and
return early, resulting in following that NULL pointer and getting
a segfault.  There is currently no code in the git codebase that does
this, but this issue was found during testing of some proposed patches
that had the following structure:

    struct merge_options merge_opt;
    struct merge_result result;

    init_merge_options(&merge_opt, the_repository);
    memset(&result, 0, sizeof(result));

    <do N merges, for some value of N>

    merge_finalize(&merge_opt, &result);

where some flags could cause the code to have N=0, i.e. doing no merges.
Add a check for result->priv being NULL and return early to avoid a
segfault in these kinds of cases.

While at it, ensure the FREE_AND_NULL() in the function does something
useful with the nulling aspect, namely sets result->priv to NULL rather
than a mere temporary.

Reported-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
---
    merge-ort: fix calling merge_finalize() with no intermediate merge
    
    Changes since v1:
    
     * Moved code into an if-block instead of returning early, as suggested
       by Stolee.
    
    See
    https://lore.kernel.org/git/CABPp-BHCdjOutYqdMO1NbYKNA0BgkXRgwUEKK=MX0kXM-5G_DQ@mail.gmail.com/

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1518%2Fnewren%2Ffix-merge-finalize-with-no-merge-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1518/newren/fix-merge-finalize-with-no-merge-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1518

Range-diff vs v1:

 1:  e636027f60c ! 1:  2cb1c63f05b merge-ort: fix calling merge_finalize() with no intermediate merge
     @@ merge-ort.c: void merge_switch_to_result(struct merge_options *opt,
       
      -	clear_or_reinit_internal_opts(opti, 0);
      -	FREE_AND_NULL(opti);
     -+	if (!result->priv)
     -+		return;
     -+	clear_or_reinit_internal_opts(result->priv, 0);
     -+	FREE_AND_NULL(result->priv);
     ++	if (result->priv) {
     ++		clear_or_reinit_internal_opts(result->priv, 0);
     ++		FREE_AND_NULL(result->priv);
     ++	}
       }
       
       /*** Function Grouping: helper functions for merge_incore_*() ***/


 merge-ort.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/merge-ort.c b/merge-ort.c
index 5bf64354d16..29966fc082f 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -4718,14 +4718,14 @@ void merge_switch_to_result(struct merge_options *opt,
 void merge_finalize(struct merge_options *opt,
 		    struct merge_result *result)
 {
-	struct merge_options_internal *opti = result->priv;
-
 	if (opt->renormalize)
 		git_attr_set_direction(GIT_ATTR_CHECKIN);
 	assert(opt->priv == NULL);
 
-	clear_or_reinit_internal_opts(opti, 0);
-	FREE_AND_NULL(opti);
+	if (result->priv) {
+		clear_or_reinit_internal_opts(result->priv, 0);
+		FREE_AND_NULL(result->priv);
+	}
 }
 
 /*** Function Grouping: helper functions for merge_incore_*() ***/

base-commit: 667fcf4e15379790f0b609d6a83d578e69f20301
-- 
gitgitgadget

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

* Re: [PATCH v2] merge-ort: fix calling merge_finalize() with no intermediate merge
  2023-04-22 20:22 ` [PATCH v2] " Elijah Newren via GitGitGadget
@ 2023-04-24 15:17   ` Derrick Stolee
  0 siblings, 0 replies; 7+ messages in thread
From: Derrick Stolee @ 2023-04-24 15:17 UTC (permalink / raw)
  To: Elijah Newren via GitGitGadget, git; +Cc: Elijah Newren

On 4/22/2023 4:22 PM, Elijah Newren via GitGitGadget wrote:
>     merge-ort: fix calling merge_finalize() with no intermediate merge
>     
>     Changes since v1:
>     
>      * Moved code into an if-block instead of returning early, as suggested
>        by Stolee.

v2 LGTM. Thanks!

-Stolee

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

end of thread, other threads:[~2023-04-24 15:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-20  7:14 [PATCH] merge-ort: fix calling merge_finalize() with no intermediate merge Elijah Newren via GitGitGadget
2023-04-20 13:10 ` Derrick Stolee
2023-04-20 17:10   ` Junio C Hamano
2023-04-22  2:04   ` Elijah Newren
2023-04-20 16:11 ` Junio C Hamano
2023-04-22 20:22 ` [PATCH v2] " Elijah Newren via GitGitGadget
2023-04-24 15:17   ` Derrick Stolee

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 NNTP newsgroup(s).