git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCHv3] tag: add --points-at list option

From
Tom Grennan <tmgrennan@gmail.com>
Date
Feb 8, 2012, 18:43 UTC
Message-ID
<20120208184332.GF6264@tgrennan-laptop>
In-Reply-To
<20120208154442.GB8773@sigill.intra.peff.net>
On Wed, Feb 08, 2012 at 10:44:42AM -0500, Jeff King wrote:
Show 15 quoted lines
>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.
Show 9 quoted lines
>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;
}
Show 9 quoted lines
>> +	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
Previous: Jeff KingNext: Jeff King
Message 43 of 53 in “tag: add --points-at list option”
  1. tag: add --points-at list optionTom Grennan, Feb 5, 2012
  2. Junio C HamanoFeb 5, 2012
  3. Tom GrennanFeb 6, 2012
  4. Junio C HamanoFeb 6, 2012
  5. Tom GrennanFeb 6, 2012
  6. Jeff KingFeb 6, 2012
  7. Tom GrennanFeb 6, 2012
  8. Jeff KingFeb 6, 2012
  9. Jeff KingFeb 6, 2012
  10. Jeff KingFeb 6, 2012
  11. Jeff KingFeb 6, 2012
  12. 1/3 tag: fix output of "tag -n" when errors occurJeff King, Feb 6, 2012
  13. 2/3 tag: die when listing missing or corrupt objectsJeff King, Feb 6, 2012
  14. Junio C HamanoFeb 6, 2012
  15. Jeff KingFeb 6, 2012
  16. Junio C HamanoFeb 6, 2012
  17. Jeff KingFeb 6, 2012
  18. Junio C HamanoFeb 6, 2012
  19. Junio C HamanoFeb 6, 2012
  20. Jeff KingFeb 6, 2012
  21. Junio C HamanoFeb 6, 2012
  22. Jeff KingFeb 8, 2012
  23. Junio C HamanoFeb 9, 2012
  24. 3/3 tag: don't show non-tag contents with "-n"Jeff King, Feb 6, 2012
  25. [PATCHv2] tag: add --points-at list optionTom Grennan, Feb 7, 2012
  26. [PATCHv2] tag: add --points-at list optionTom Grennan, Feb 7, 2012
  27. Junio C HamanoFeb 7, 2012
  28. Tom GrennanFeb 7, 2012
  29. Jeff KingFeb 7, 2012
  30. Tom GrennanFeb 7, 2012
  31. Jeff KingFeb 7, 2012
  32. Tom GrennanFeb 7, 2012
  33. Jeff KingFeb 7, 2012
  34. Junio C HamanoFeb 7, 2012
  35. Jeff KingFeb 7, 2012
  36. Tom GrennanFeb 7, 2012
  37. Jeff KingFeb 8, 2012
  38. Tom GrennanFeb 8, 2012
  39. Jeff KingFeb 8, 2012
  40. [PATCHv3] tag: add --points-at list optionTom Grennan, Feb 8, 2012
  41. [PATCHv3] tag: add --points-at list optionTom Grennan, Feb 8, 2012
  42. Jeff KingFeb 8, 2012
  43. Tom GrennanFeb 8, 2012
  44. Jeff KingFeb 8, 2012
  45. [PATCHv4] tag: add --points-at list optionTom Grennan, Feb 8, 2012
  46. [PATCHv4] tag: add --points-at list optionTom Grennan, Feb 8, 2012
  47. Jeff KingFeb 8, 2012
  48. Tom GrennanFeb 8, 2012
  49. tag: add --points-at list optionTom Grennan, Feb 8, 2012
  50. tag: add --points-at list optionTom Grennan, Feb 8, 2012
  51. Jeff KingFeb 9, 2012
  52. Junio C HamanoFeb 9, 2012
  53. Tom GrennanFeb 8, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.