[PATCH 13/21] Collect skipping of header field names and calculation of line lengths in one place

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Johan Herland
Date: Friday, June 8, 2007 - 5:18 pm

For each of the parsed lines we at some point skip past its initial
identifier ("type ", "tag ", etc.). We also at some point calculate the
length of the remaining line. This patch moves these calculations into
one place. This provides _one_ place for all header lines where their
respective pointers start pointing at the header value (instead of the
start of the line), and their lengths are calculated.

Signed-off-by: Johan Herland <johan@herland.net>
---
 tag.c |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/tag.c b/tag.c
index ac76ec0..9a6924f 100644
--- a/tag.c
+++ b/tag.c
@@ -118,12 +118,20 @@ int parse_and_verify_tag_buffer(struct tag *item,
 				tagger_line - data);
 	}
 
+	/*
+	 * Advance header field pointers past their initial identifier.
+	 * Calculate lengths of header fields.
+	 */
+	type_line += strlen("type ");
+	type_len   = tag_line - type_line - 1;
+	tag_line  += strlen("tag ");
+	tag_len    = tagger_line - tag_line - 1;
+
 	/* Get the actual type */
-	type_len = tag_line - type_line - strlen("type \n");
 	if (type_len >= sizeof(type))
 		return error("Tag object (@ char " PD_FMT "): "
-			"Type too long", type_line + 5 - data);
-	memcpy(type, type_line + 5, type_len);
+			"Type too long", type_line - data);
+	memcpy(type, type_line, type_len);
 	type[type_len] = '\0';
 
 	if (thorough_verify) {
@@ -136,7 +144,7 @@ int parse_and_verify_tag_buffer(struct tag *item,
 				sha1_to_hex(sha1));
 
 		/* Verify tag name: disallow control characters or spaces */
-		for (i = 4;;) {
+		for (i = 0;;) {
 			unsigned char c = tag_line[i++];
 			if (c == '\n')
 				break;
@@ -154,9 +162,8 @@ int parse_and_verify_tag_buffer(struct tag *item,
 	}
 
 	if (item) {
-		tag_len = tagger_line - tag_line - strlen("tag \n");
 		item->tag = xmalloc(tag_len + 1);
-		memcpy(item->tag, tag_line + 4, tag_len);
+		memcpy(item->tag, tag_line, tag_len);
 		item->tag[tag_len] = '\0';
 
 		if (!strcmp(type, blob_type)) {
@@ -169,7 +176,7 @@ int parse_and_verify_tag_buffer(struct tag *item,
 			item->tagged = &lookup_tag(sha1)->object;
 		} else {
 			error("Tag object (@ char " PD_FMT "): "
-				"Unknown type '%s'", type_line + 5 - data, type);
+				"Unknown type '%s'", type_line - data, type);
 			item->tagged = NULL;
 		}
 
-- 
1.5.2


-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 0/21] Refactor the tag object (take 2), Johan Herland, (Fri Jun 8, 5:10 pm)
[PATCH 02/21] Return error messages when parsing fails., Johan Herland, (Fri Jun 8, 5:13 pm)
[PATCH 09/21] Remove unneeded code from mktag.c, Johan Herland, (Fri Jun 8, 5:16 pm)
[PATCH 10/21] Free mktag's buffer before dying, Johan Herland, (Fri Jun 8, 5:16 pm)
[PATCH 13/21] Collect skipping of header field names and c ..., Johan Herland, (Fri Jun 8, 5:18 pm)
[PATCH 17/21] Update comments on tag objects in mktag.c, Johan Herland, (Fri Jun 8, 5:20 pm)
Re: [PATCH 03/21] Refactoring to make verify_tag() and par ..., Johannes Schindelin, (Fri Jun 8, 7:54 pm)
Re: [PATCH 10/21] Free mktag's buffer before dying, Alex Riesen, (Sat Jun 9, 2:37 pm)
Re: [PATCH 09/21] Remove unneeded code from mktag.c, Alex Riesen, (Sat Jun 9, 2:39 pm)
Re: [PATCH 09/21] Remove unneeded code from mktag.c, Johan Herland, (Sat Jun 9, 2:42 pm)
Re: [PATCH 10/21] Free mktag's buffer before dying, Johan Herland, (Sat Jun 9, 2:46 pm)
Re: [PATCH 10/21] Free mktag's buffer before dying, Alex Riesen, (Sat Jun 9, 3:00 pm)
Re: [PATCH 10/21] Free mktag's buffer before dying, Johan Herland, (Sat Jun 9, 3:05 pm)
Re: [PATCH 05/21] Make parse_tag_buffer_internal() handle ..., Johannes Schindelin, (Sun Jun 10, 1:06 am)
Re: [PATCH 06/21] Refactor tag name verification loop to u ..., Johannes Schindelin, (Sun Jun 10, 1:14 am)
Re: [PATCH 07/21] Copy the remaining differences from veri ..., Johannes Schindelin, (Sun Jun 10, 1:22 am)
Re: [PATCH 10/21] Free mktag's buffer before dying, Johannes Schindelin, (Sun Jun 10, 1:38 am)
Re: [PATCH 11/21] Rewrite error messages; fix up line lengths, Johannes Schindelin, (Sun Jun 10, 1:38 am)
Re: [PATCH 12/21] Use prefixcmp() instead of memcmp() for ..., Johannes Schindelin, (Sun Jun 10, 1:41 am)
Re: [PATCH 13/21] Collect skipping of header field names a ..., Johannes Schindelin, (Sun Jun 10, 1:45 am)
Re: [PATCH 06/21] Refactor tag name verification loop to u ..., Johannes Schindelin, (Sun Jun 10, 2:01 am)