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@xxxxxxxxxxx> --- 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@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html