public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox•com>
To: Jeff King <peff@peff•net>
Cc: git@vger•kernel.org,
	Johannes Schindelin <johannes.schindelin@gmx•de>,
	Karthik Nayak <karthik.188@gmail•com>
Subject: Re: [PATCH] type_from_string_gently: make sure length matches
Date: Fri, 17 Apr 2015 13:54:27 -0700	[thread overview]
Message-ID: <xmqqd232pjb0.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <20150417145247.GA2421@peff.net> (Jeff King's message of "Fri, 17 Apr 2015 10:52:48 -0400")

Jeff King <peff@peff•net> writes:

> When commit fe8e3b7 refactored type_from_string to allow
> input that was not NUL-terminated, it switched to using
> strncmp instead of strcmp. But this means we check only the
> first "len" bytes of the strings, and ignore any remaining
> bytes in the object_type_string. We should make sure that it
> is also "len" bytes, or else we would accept "comm" as
> "commit", and so forth.
>
> Signed-off-by: Jeff King <peff@peff•net>
> ---
> Since the strings we are matching are literals, we could also record
> their sizes in the object_type_strings array and check the length first
> before even calling strncmp. I doubt this is a performance hot-spot,
> though.
>
> You could also potentially just use strlen(object_type_strings[i]), but
> I'm not sure if compilers will optimize out the strlen in this case,
> since it is in a loop.

That thought crossed my mind while reading your patch.  It could
even make it go faster if we made object_type_strings into an array
of counted strings (i.e. "struct { const char *str; int len; }")
and then took advantage of the fact that we have lengths of both.


 object.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/object.c b/object.c
index aedac24..51584ea 100644
--- a/object.c
+++ b/object.c
@@ -18,19 +18,22 @@ struct object *get_indexed_object(unsigned int idx)
 	return obj_hash[idx];
 }
 
-static const char *object_type_strings[] = {
-	NULL,		/* OBJ_NONE = 0 */
-	"commit",	/* OBJ_COMMIT = 1 */
-	"tree",		/* OBJ_TREE = 2 */
-	"blob",		/* OBJ_BLOB = 3 */
-	"tag",		/* OBJ_TAG = 4 */
+static struct {
+	const char *str;
+	int len;
+} object_type_name[] = {
+	{ NULL, 0 },      /* OBJ_NONE = 0 */
+	{ "commit", 6 },  /* OBJ_COMMIT = 1 */
+	{ "tree", 4 },	  /* OBJ_TREE = 2 */
+	{ "blob", 4 },    /* OBJ_BLOB = 3 */
+	{ "tag", 3 },     /* OBJ_TAG = 4 */
 };
 
 const char *typename(unsigned int type)
 {
-	if (type >= ARRAY_SIZE(object_type_strings))
+	if (type >= ARRAY_SIZE(object_type_name))
 		return NULL;
-	return object_type_strings[type];
+	return object_type_name[type].str;
 }
 
 int type_from_string_gently(const char *str, ssize_t len, int gentle)
@@ -40,8 +43,9 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
 	if (len < 0)
 		len = strlen(str);
 
-	for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
-		if (!strncmp(str, object_type_strings[i], len))
+	for (i = 1; i < ARRAY_SIZE(object_type_name); i++)
+		if (object_type_name[i].len == len &&
+			    !strncmp(str, object_type_name[i].str, len))
 			return i;
 
 	if (gentle)

  reply	other threads:[~2015-04-17 20:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-17 14:52 [PATCH] type_from_string_gently: make sure length matches Jeff King
2015-04-17 20:54 ` Junio C Hamano [this message]
2015-04-17 21:07   ` Jeff King
2015-04-17 21:11     ` Junio C Hamano

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=xmqqd232pjb0.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=johannes.schindelin@gmx$(echo .)de \
    --cc=karthik.188@gmail$(echo .)com \
    --cc=peff@peff$(echo .)net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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