# [PATCH] notes_merge_commit(): do not pass temporary buffer to other function

5 messages from 2011-09-27 to 2011-09-28. Participants: Michael Haggerty, Johan Herland, Junio C Hamano.
Thread: https://gitlist.dev/t/28509

## Michael Haggerty, 2011-09-27 04:46

Subject: [PATCH] notes_merge_commit(): do not pass temporary buffer to other function
Message-ID: <1317098813-30839-1-git-send-email-mhagger@alum.mit.edu>
URL: https://gitlist.dev/e/1317098813-30839-1-git-send-email-mhagger%40alum.mit.edu

```
It is unsafe to pass a temporary buffer as an argument to
read_directory().

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---

I discovered this problem when an innocent modification to unrelated
code triggered test failures.

 notes-merge.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git notes-merge.c notes-merge.c
index e1aaf43..baaf31f 100644
--- notes-merge.c
+++ notes-merge.c
@@ -680,7 +680,7 @@ int notes_merge_commit(struct notes_merge_options *o,
 	 * Finally store the new commit object SHA1 into 'result_sha1'.
 	 */
 	struct dir_struct dir;
-	const char *path = git_path(NOTES_MERGE_WORKTREE "/");
+	char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
 	int path_len = strlen(path), i;
 	const char *msg = strstr(partial_commit->buffer, "\n\n");
 
@@ -720,6 +720,7 @@ int notes_merge_commit(struct notes_merge_options *o,
 			    result_sha1);
 	OUTPUT(o, 4, "Finalized notes merge commit: %s",
 	       sha1_to_hex(result_sha1));
+	free(path);
 	return 0;
 }
 
-- 
1.7.7.rc2

```

## Johan Herland, 2011-09-27 07:12

Subject: Re: [PATCH] notes_merge_commit(): do not pass temporary buffer to other function
Message-ID: <CALKQrgcO20am0nU7W=hmbMYVjjGi0-REB8=Xko5NtXjjG4UZag@mail.gmail.com>
URL: https://gitlist.dev/e/CALKQrgcO20am0nU7W%3DhmbMYVjjGi0-REB8%3DXko5NtXjjG4UZag%40mail.gmail.com
In-Reply-To: <1317098813-30839-1-git-send-email-mhagger@alum.mit.edu>

```
On Tue, Sep 27, 2011 at 06:46, Michael Haggerty <mhagger@alum.mit.edu> wrote:
> It is unsafe to pass a temporary buffer as an argument to
> read_directory().
>
> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>

ACK.

...Johan

-- 
Johan Herland, <johan@herland.net>
www.herland.net

```

## Junio C Hamano, 2011-09-27 16:59

Subject: Re: [PATCH] notes_merge_commit(): do not pass temporary buffer to other function
Message-ID: <7vd3elyl8t.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vd3elyl8t.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1317098813-30839-1-git-send-email-mhagger@alum.mit.edu>

```
Michael Haggerty <mhagger@alum.mit.edu> writes:

> I discovered this problem when an innocent modification to unrelated
> code triggered test failures.
>
>  notes-merge.c |    3 ++-
>  1 files changed, 2 insertions(+), 1 deletions(-)
>
> diff --git notes-merge.c notes-merge.c
> index e1aaf43..baaf31f 100644
> --- notes-merge.c
> +++ notes-merge.c

It is Ok to play with -p0 yourself but please don't do that in the public.

> @@ -680,7 +680,7 @@ int notes_merge_commit(struct notes_merge_options *o,
>  	 * Finally store the new commit object SHA1 into 'result_sha1'.
>  	 */
>  	struct dir_struct dir;
> -	const char *path = git_path(NOTES_MERGE_WORKTREE "/");
> +	char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
>  	int path_len = strlen(path), i;
>  	const char *msg = strstr(partial_commit->buffer, "\n\n");
>  
> @@ -720,6 +720,7 @@ int notes_merge_commit(struct notes_merge_options *o,
>  			    result_sha1);
>  	OUTPUT(o, 4, "Finalized notes merge commit: %s",
>  	       sha1_to_hex(result_sha1));
> +	free(path);
>  	return 0;
>  }

```

## Michael Haggerty, 2011-09-28 02:50

Subject: Re: [PATCH] notes_merge_commit(): do not pass temporary buffer to other function
Message-ID: <4E828B6C.1010707@alum.mit.edu>
URL: https://gitlist.dev/e/4E828B6C.1010707%40alum.mit.edu
In-Reply-To: <7vd3elyl8t.fsf@alter.siamese.dyndns.org>

```
On 09/27/2011 06:59 PM, Junio C Hamano wrote:
> Michael Haggerty <mhagger@alum.mit.edu> writes:
> 
>> I discovered this problem when an innocent modification to unrelated
>> code triggered test failures.
>>
>>  notes-merge.c |    3 ++-
>>  1 files changed, 2 insertions(+), 1 deletions(-)
>>
>> diff --git notes-merge.c notes-merge.c
>> index e1aaf43..baaf31f 100644
>> --- notes-merge.c
>> +++ notes-merge.c
> 
> It is Ok to play with -p0 yourself but please don't do that in the public.

Sorry; I had set diff.noprefix=true, not realizing that it would affect
things like "git format-patch".  It also confused emacs' magit mode, and
probably some other tools.  It's now set permanently back to false.

The reason I was experimenting with this option is that it is a quick
double-click to select a filename like "foo/bar" in the diff output,
whereas selecting the filename out of "a/foo/bar" requires a slower
click and drag.  Once I considered whether git could be taught to
explicitly ignore the "[ab]/" prefix when parsing filenames, but that is
too evil even for me :-)

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

```

## Junio C Hamano, 2011-09-28 03:10

Subject: Re: [PATCH] notes_merge_commit(): do not pass temporary buffer to other function
Message-ID: <7vty7xs6ou.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vty7xs6ou.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4E828B6C.1010707@alum.mit.edu>

```
Michael Haggerty <mhagger@alum.mit.edu> writes:

>>> diff --git notes-merge.c notes-merge.c
>>> index e1aaf43..baaf31f 100644
>>> --- notes-merge.c
>>> +++ notes-merge.c
>> 
>> It is Ok to play with -p0 yourself but please don't do that in the public.
>
> Sorry; I had set diff.noprefix=true, not realizing that it would affect
> things like "git format-patch".  It also confused emacs' magit mode, and
> probably some other tools.  It's now set permanently back to false.

Thanks.

It is nothing catastrophic (I applied it with "am -p0 -s3c" just fine),
but everybody here sends -p1 format, so...

```
