From: Tom Grennan <tmgrennan@gmail.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org, gitster@pobox.com, jasampler@gmail.com
Subject: Re: [PATCHv3] tag: add --points-at list option
Date: Wed, 8 Feb 2012 10:43:32 -0800 [thread overview]
Message-ID: <20120208184332.GF6264@tgrennan-laptop> (raw)
In-Reply-To: <20120208154442.GB8773@sigill.intra.peff.net>
On Wed, Feb 08, 2012 at 10:44:42AM -0500, Jeff King wrote:
>On Tue, Feb 07, 2012 at 10:21:16PM -0800, Tom Grennan wrote:
>
>> +static const unsigned char *match_points_at(const unsigned char *sha1)
>> +{
>> + int i;
>> + const unsigned char *tagged_sha1 = (unsigned char*)"";
>> + struct object *obj = parse_object(sha1);
>> +
>> + if (obj && obj->type == OBJ_TAG)
>> + tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
>
>This is not safe. A sha1 is not NUL-terminated, but is rather _always_
>20 bytes. So when the object is not a tag, you do the hashcmp against
>your single-byte string literal above, and we end up comparing whatever
>garbage is in the data segment after the string literal.
Yikes! That was dumb.
>What you want instead is the all-zeros sha1, like:
>
> const unsigned char null_sha1[20] = { 0 };
>
>Though we provide a null_sha1 global already. So doing:
>
> const unsigned char *tagged_sha1 = null_sha1;
>
>would be sufficient.
Or just initialize at test tagged_sha1 with NULL.
static const unsigned char *match_points_at(const unsigned char *sha1)
{
int i;
const unsigned char *tagged_sha1 = NULL;
struct object *obj = parse_object(sha1);
if (obj && obj->type == OBJ_TAG)
tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
for (i = 0; i < points_at.nr; i++)
if (!hashcmp(points_at.sha1[i], sha1))
return sha1;
else if (tagged_sha1 &&
!hashcmp(points_at.sha1[i], tagged_sha1))
return tagged_sha1;
return NULL;
}
>That being said, I don't know why you want to do both lookups in the
>same loop of the points_at. If it's a lightweight tag and the tag
>matches, you can get away with not parsing the object at all (although
>to be fair, that is the minority case, so it is unlikely to matter).
Yes, I think your saying that the lightweight search could go before the
tag object search like this.
static const unsigned char *match_points_at(const unsigned char *sha1)
{
const unsigned char *tagged_sha1 = NULL;
struct object *obj = parse_object(sha1);
if (sha1_array_lookup(&points_at, sha1) >= 0)
return sha1;
if (obj && obj->type == OBJ_TAG)
tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
return tagged_sha1;
return NULL;
}
>Also, should we be producing an error if !obj? It would indicate a tag
>that points to a bogus object.
I think the test of (obj) is redundant as this should be caught
by get_sha1() in parse_opt_points_at()
int parse_opt_points_at(const struct option *opt __attribute__ ((unused)),
const char *arg, int unset)
{
unsigned char sha1[20];
if (unset) {
sha1_array_clear(&points_at);
return 0;
}
if (!arg)
return error(_("switch 'points-at' requires an object"));
if (get_sha1(arg, sha1))
return error(_("malformed object name '%s'"), arg);
sha1_array_append(&points_at, sha1);
return 0;
}
>> + for (i = 0; i < points_at.nr; i++)
>> + if (!hashcmp(points_at.sha1[i], sha1))
>> + return sha1;
>> + else if (!hashcmp(points_at.sha1[i], tagged_sha1))
>> + return tagged_sha1;
>> + return NULL;
>
>Why write your own linear search? sha1_array_lookup will do a binary
>search for you.
Well, it's only a linear search of the points_at command arguments.
But by that reasoning, might as well do two sha1_array_lookups like
above and save some code b/c "less code is always better"(TM).
>Other than that, the patch looks OK to me.
Thanks, I'll send what I hope to be the final version later today.
--
TomG
next prev parent reply other threads:[~2012-02-08 18:44 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-05 22:28 [RFC/PATCH] tag: add --points-at list option Tom Grennan
2012-02-05 23:31 ` Junio C Hamano
2012-02-06 5:48 ` Tom Grennan
2012-02-06 6:25 ` Junio C Hamano
2012-02-06 6:45 ` Tom Grennan
2012-02-06 0:04 ` Jeff King
2012-02-06 6:32 ` Tom Grennan
2012-02-06 7:04 ` Jeff King
2012-02-06 7:13 ` Jeff King
2012-02-06 7:45 ` Jeff King
2012-02-06 8:11 ` Jeff King
2012-02-06 8:13 ` [PATCH 1/3] tag: fix output of "tag -n" when errors occur Jeff King
2012-02-06 8:13 ` [PATCH 2/3] tag: die when listing missing or corrupt objects Jeff King
2012-02-06 8:32 ` Junio C Hamano
2012-02-06 8:34 ` Jeff King
2012-02-06 8:36 ` Junio C Hamano
2012-02-06 8:38 ` Jeff King
2012-02-06 18:04 ` Junio C Hamano
2012-02-06 18:13 ` Junio C Hamano
2012-02-06 20:12 ` Jeff King
2012-02-06 23:34 ` Junio C Hamano
2012-02-08 21:01 ` Jeff King
2012-02-09 4:33 ` Junio C Hamano
2012-02-06 8:14 ` [PATCH 3/3] tag: don't show non-tag contents with "-n" Jeff King
2012-02-07 7:01 ` [PATCHv2] tag: add --points-at list option Tom Grennan
2012-02-07 7:01 ` Tom Grennan
2012-02-07 8:35 ` Junio C Hamano
2012-02-07 18:05 ` Tom Grennan
2012-02-07 16:05 ` Jeff King
2012-02-07 19:02 ` Tom Grennan
2012-02-07 19:12 ` Jeff King
2012-02-07 19:22 ` Tom Grennan
2012-02-07 19:36 ` Jeff King
2012-02-07 20:20 ` Junio C Hamano
2012-02-07 21:30 ` Jeff King
2012-02-07 22:08 ` Tom Grennan
2012-02-08 0:25 ` Jeff King
2012-02-08 1:45 ` Tom Grennan
2012-02-08 15:31 ` Jeff King
2012-02-08 6:21 ` [PATCHv3] " Tom Grennan
2012-02-08 6:21 ` Tom Grennan
2012-02-08 15:44 ` Jeff King
2012-02-08 18:43 ` Tom Grennan [this message]
2012-02-08 18:57 ` Jeff King
2012-02-08 20:12 ` [PATCHv4] " Tom Grennan
2012-02-08 20:12 ` Tom Grennan
2012-02-08 20:58 ` Jeff King
2012-02-08 22:15 ` Tom Grennan
2012-02-08 23:03 ` [PATCH-master] " Tom Grennan
2012-02-08 23:03 ` Tom Grennan
2012-02-09 1:44 ` Jeff King
2012-02-09 4:29 ` Junio C Hamano
2012-02-08 18:58 ` [PATCHv3] " Tom Grennan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120208184332.GF6264@tgrennan-laptop \
--to=tmgrennan@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jasampler@gmail.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox