[PATCH 02/21] Return error messages when parsing fails.

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

This patch brings the already similar tag.c:parse_tag_buffer() and
mktag.c:verify_tag() a little bit closer to eachother.

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

diff --git a/tag.c b/tag.c
index bbacd59..954ed8a 100644
--- a/tag.c
+++ b/tag.c
@@ -35,6 +35,12 @@ struct tag *lookup_tag(const unsigned char *sha1)
 
 int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
 {
+#ifdef NO_C99_FORMAT
+#define PD_FMT "%d"
+#else
+#define PD_FMT "%td"
+#endif
+
 	int typelen, taglen;
 	unsigned char sha1[20];
 	const char *type_line, *tag_line, *sig_line;
@@ -45,28 +51,41 @@ int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
         item->object.parsed = 1;
 
 	if (size < 64)
-		return -1;
-	if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1))
-		return -1;
+		return error("failed preliminary size check");
 
+	/* Verify object line */
+	if (memcmp(data, "object ", 7))
+		return error("char%d: does not start with \"object \"", 0);
+
+	if (get_sha1_hex((char *) data + 7, sha1))
+		return error("char%d: could not get SHA1 hash", 7);
+
+	/* Verify type line */
 	type_line = (char *) data + 48;
-	if (memcmp("\ntype ", type_line-1, 6))
-		return -1;
+	if (memcmp(type_line - 1, "\ntype ", 6))
+		return error("char%d: could not find \"\\ntype \"", 47);
 
+	/* Verify tag-line */
 	tag_line = strchr(type_line, '\n');
-	if (!tag_line || memcmp("tag ", ++tag_line, 4))
-		return -1;
+	if (!tag_line)
+		return error("char" PD_FMT ": could not find next \"\\n\"", type_line - (char *) data);
+	tag_line++;
+	if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
+		return error("char" PD_FMT ": no \"tag \" found", tag_line - (char *) data);
 
 	sig_line = strchr(tag_line, '\n');
 	if (!sig_line)
 		return -1;
 	sig_line++;
 
+	/* Get the actual type */
 	typelen = tag_line - type_line - strlen("type \n");
-	if (typelen >= 20)
-		return -1;
+	if (typelen >= sizeof(type))
+		return error("char" PD_FMT ": type too long", type_line+5 - (char *) data);
+
 	memcpy(type, type_line + 5, typelen);
 	type[typelen] = '\0';
+
 	taglen = sig_line - tag_line - strlen("tag \n");
 	item->tag = xmalloc(taglen + 1);
 	memcpy(item->tag, tag_line + 4, taglen);
@@ -92,6 +111,8 @@ int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
 	}
 
 	return 0;
+
+#undef PD_FMT
 }
 
 int parse_tag(struct tag *item)
-- 
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 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)