# objectname:short bug?

4 messages from 2010-08-26 to 2010-08-27. Participants: Jay Soffian, Jeff King, Michael J Gruber.
Thread: https://gitlist.dev/t/24873

## Jay Soffian, 2010-08-26 21:16

Subject: objectname:short bug?
Message-ID: <AANLkTikhcAC6Nf=jWQN40ascodaCte6v6e1JPfv-=To-@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTikhcAC6Nf%3DjWQN40ascodaCte6v6e1JPfv-%3DTo-%40mail.gmail.com

```
$ ./git version
git version 1.7.1.88.g67687

$ ./git for-each-ref --format='%(objectname) %(refname:short)'
refs/tags | head -5
d5aef6e4d58cfe1549adef5b436f3ace984e8c86 gitgui-0.10.0
33682a5e98adfd8ba4ce0e21363c443bd273eb77 gitgui-0.10.1
ca9b793bda20c7d011c96895e9407fac2df9648b gitgui-0.10.2
8c178f72b54f387b84388d093a920ae45b8659dd gitgui-0.11.0
15c7170c8c1f6d4f8b8aa539987a92c45d06be9e gitgui-0.12.0

$ ./git for-each-ref --format='%(objectname:short) %(refname:short)'
refs/tags | head -5
8b1e557 gitgui-0.10.0
8b1e557 gitgui-0.10.1
8b1e557 gitgui-0.10.2
8b1e557 gitgui-0.11.0
8b1e557 gitgui-0.12.0

Just checked tip-of-master (b5442ca) and the issue still appears.

j.

```

## Jay Soffian, 2010-08-26 21:34

Subject: [PATCH] for-each-ref: fix objectname:short bug
Message-ID: <1282858469-22116-1-git-send-email-jaysoffian@gmail.com>
URL: https://gitlist.dev/e/1282858469-22116-1-git-send-email-jaysoffian%40gmail.com
In-Reply-To: <AANLkTikhcAC6Nf=jWQN40ascodaCte6v6e1JPfv-=To-@mail.gmail.com>

```
When objectname:short was introduced, it forgot to copy the result of
find_unique_abbrev. Because the result of find_unique_abbrev is a
pointer to static buffer, this resulted in the same value being
substituted in for each ref.

Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
---
 builtin/for-each-ref.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index a2b28c6..89e75c6 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -228,7 +228,8 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
 			v->s = s;
 		}
 		else if (!strcmp(name, "objectname:short")) {
-			v->s = find_unique_abbrev(obj->sha1, DEFAULT_ABBREV);
+			v->s = xstrdup(find_unique_abbrev(obj->sha1,
+							  DEFAULT_ABBREV));
 		}
 	}
 }
-- 
1.7.2.2.170.gb30b3d

```

## Jeff King, 2010-08-26 22:15

Subject: Re: [PATCH] for-each-ref: fix objectname:short bug
Message-ID: <20100826221536.GA10418@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100826221536.GA10418%40coredump.intra.peff.net
In-Reply-To: <1282858469-22116-1-git-send-email-jaysoffian@gmail.com>

```
On Thu, Aug 26, 2010 at 05:34:29PM -0400, Jay Soffian wrote:

> When objectname:short was introduced, it forgot to copy the result of
> find_unique_abbrev. Because the result of find_unique_abbrev is a
> pointer to static buffer, this resulted in the same value being
> substituted in for each ref.

Yuck. I can't believe neither Michael nor I noticed this on first
review.

> --- a/builtin/for-each-ref.c
> +++ b/builtin/for-each-ref.c
> @@ -228,7 +228,8 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
>  			v->s = s;
>  		}
>  		else if (!strcmp(name, "objectname:short")) {
> -			v->s = find_unique_abbrev(obj->sha1, DEFAULT_ABBREV);
> +			v->s = xstrdup(find_unique_abbrev(obj->sha1,
> +							  DEFAULT_ABBREV));

Your fix looks correct. It obviously leaks, but then so does 99% of the
rest of for-each-ref.

-Peff

```

## Michael J Gruber, 2010-08-27 07:08

Subject: Re: [PATCH] for-each-ref: fix objectname:short bug
Message-ID: <4C776452.8020200@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4C776452.8020200%40drmicha.warpmail.net
In-Reply-To: <1282858469-22116-1-git-send-email-jaysoffian@gmail.com>

```
Jay Soffian venit, vidit, dixit 26.08.2010 23:34:
> When objectname:short was introduced, it forgot to copy the result of
> find_unique_abbrev. Because the result of find_unique_abbrev is a
> pointer to static buffer, this resulted in the same value being
> substituted in for each ref.
> 
> Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
> ---
>  builtin/for-each-ref.c |    3 ++-
>  1 files changed, 2 insertions(+), 1 deletions(-)
> 
> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
> index a2b28c6..89e75c6 100644
> --- a/builtin/for-each-ref.c
> +++ b/builtin/for-each-ref.c
> @@ -228,7 +228,8 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
>  			v->s = s;
>  		}
>  		else if (!strcmp(name, "objectname:short")) {
> -			v->s = find_unique_abbrev(obj->sha1, DEFAULT_ABBREV);
> +			v->s = xstrdup(find_unique_abbrev(obj->sha1,
> +							  DEFAULT_ABBREV));
>  		}
>  	}
>  }

3 words:

embarrassing - sorry - thanks!

Michael

```
