Commit 720d5472 by Vicent Marti

Change `parse` methods to const buffer

Signed-off-by: Vicent Marti <tanoku@gmail.com>
parent d8ad64d3
...@@ -235,9 +235,9 @@ int git_commit_create( ...@@ -235,9 +235,9 @@ int git_commit_create(
return error; return error;
} }
int commit_parse_buffer(git_commit *commit, void *data, size_t len) int commit_parse_buffer(git_commit *commit, const void *data, size_t len)
{ {
char *buffer = (char *)data; const char *buffer = (char *)data;
const char *buffer_end = (char *)data + len; const char *buffer_end = (char *)data + len;
git_oid parent_oid; git_oid parent_oid;
......
...@@ -118,13 +118,13 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid) ...@@ -118,13 +118,13 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
return out; return out;
} }
int git__parse_oid(git_oid *oid, char **buffer_out, int git__parse_oid(git_oid *oid, const char **buffer_out,
const char *buffer_end, const char *header) const char *buffer_end, const char *header)
{ {
const size_t sha_len = GIT_OID_HEXSZ; const size_t sha_len = GIT_OID_HEXSZ;
const size_t header_len = strlen(header); const size_t header_len = strlen(header);
char *buffer = *buffer_out; const char *buffer = *buffer_out;
if (buffer + (header_len + sha_len + 1) > buffer_end) if (buffer + (header_len + sha_len + 1) > buffer_end)
return GIT_EOBJCORRUPTED; return GIT_EOBJCORRUPTED;
......
...@@ -43,7 +43,7 @@ struct git_repository { ...@@ -43,7 +43,7 @@ struct git_repository {
* export */ * export */
void git_object__free(void *object); void git_object__free(void *object);
int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header); int git__parse_oid(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
int git__write_oid(git_odb_stream *src, const char *header, const git_oid *oid); int git__write_oid(git_odb_stream *src, const char *header, const git_oid *oid);
#endif #endif
...@@ -109,14 +109,14 @@ static int parse_timezone_offset(const char *buffer, int *offset_out) ...@@ -109,14 +109,14 @@ static int parse_timezone_offset(const char *buffer, int *offset_out)
} }
int git_signature__parse(git_signature *sig, char **buffer_out, int git_signature__parse(git_signature *sig, const char **buffer_out,
const char *buffer_end, const char *header) const char *buffer_end, const char *header)
{ {
const size_t header_len = strlen(header); const size_t header_len = strlen(header);
int name_length, email_length; int name_length, email_length;
char *buffer = *buffer_out; const char *buffer = *buffer_out;
char *line_end, *name_end, *email_end; const char *line_end, *name_end, *email_end;
int offset = 0; int offset = 0;
memset(sig, 0x0, sizeof(git_signature)); memset(sig, 0x0, sizeof(git_signature));
...@@ -159,7 +159,7 @@ int git_signature__parse(git_signature *sig, char **buffer_out, ...@@ -159,7 +159,7 @@ int git_signature__parse(git_signature *sig, char **buffer_out,
if (buffer >= line_end) if (buffer >= line_end)
return GIT_EOBJCORRUPTED; return GIT_EOBJCORRUPTED;
sig->when.time = strtol(buffer, &buffer, 10); sig->when.time = strtol(buffer, (char **)&buffer, 10);
if (sig->when.time == 0) if (sig->when.time == 0)
return GIT_EOBJCORRUPTED; return GIT_EOBJCORRUPTED;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "repository.h" #include "repository.h"
#include <time.h> #include <time.h>
int git_signature__parse(git_signature *sig, char **buffer_out, const char *buffer_end, const char *header); int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header);
int git_signature__write(char **signature, const char *header, const git_signature *sig); int git_signature__write(char **signature, const char *header, const git_signature *sig);
#endif #endif
...@@ -79,7 +79,7 @@ const char *git_tag_message(git_tag *t) ...@@ -79,7 +79,7 @@ const char *git_tag_message(git_tag *t)
return t->message; return t->message;
} }
static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) static int parse_tag_buffer(git_tag *tag, const char *buffer, const char *buffer_end)
{ {
static const char *tag_types[] = { static const char *tag_types[] = {
NULL, "commit\n", "tree\n", "blob\n", "tag\n" NULL, "commit\n", "tree\n", "blob\n", "tag\n"
...@@ -130,9 +130,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) ...@@ -130,9 +130,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
text_len = search - buffer; text_len = search - buffer;
if (tag->tag_name != NULL)
free(tag->tag_name);
tag->tag_name = git__malloc(text_len + 1); tag->tag_name = git__malloc(text_len + 1);
memcpy(tag->tag_name, buffer, text_len); memcpy(tag->tag_name, buffer, text_len);
tag->tag_name[text_len] = '\0'; tag->tag_name[text_len] = '\0';
...@@ -141,8 +138,11 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) ...@@ -141,8 +138,11 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
tag->tagger = git__malloc(sizeof(git_signature)); tag->tagger = git__malloc(sizeof(git_signature));
if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0) if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0) {
goto cleanup; free(tag->tag_name);
git_signature_free(tag->tagger);
return error;
}
text_len = buffer_end - ++buffer; text_len = buffer_end - ++buffer;
...@@ -151,14 +151,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) ...@@ -151,14 +151,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
tag->message[text_len] = '\0'; tag->message[text_len] = '\0';
return GIT_SUCCESS; return GIT_SUCCESS;
cleanup:
if(tag->tag_name)
free(tag->tag_name);
if(tag->tagger)
git_signature_free(tag->tagger);
return error;
} }
int git_tag_create_o( int git_tag_create_o(
...@@ -194,7 +186,6 @@ int git_tag_create( ...@@ -194,7 +186,6 @@ int git_tag_create(
int type_str_len, tag_name_len, tagger_str_len, message_len; int type_str_len, tag_name_len, tagger_str_len, message_len;
int error; int error;
type_str = git_object_type2string(target_type); type_str = git_object_type2string(target_type);
tagger_str_len = git_signature__write(&tagger_str, "tagger", tagger); tagger_str_len = git_signature__write(&tagger_str, "tagger", tagger);
...@@ -245,40 +236,31 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu ...@@ -245,40 +236,31 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
{ {
git_tag tag; git_tag tag;
int error; int error;
char *buf;
git_object *obj; git_object *obj;
assert(oid && buffer); assert(oid && buffer);
memset(&tag, 0, sizeof(tag)); memset(&tag, 0, sizeof(tag));
buf = strdup(buffer); if ((error = parse_tag_buffer(&tag, buffer, buffer + strlen(buffer))) < GIT_SUCCESS)
if(buf == NULL) return error;
return GIT_ENOMEM;
if((error = parse_tag_buffer(&tag, buf, buf + strlen(buf))) < 0)
goto exit_freebuf;
error = git_object_lookup(&obj, repo, &tag.target, tag.type); error = git_object_lookup(&obj, repo, &tag.target, tag.type);
if(error < 0) if (error < GIT_SUCCESS)
goto exit_freetag; goto cleanup;
error = git_tag_create_o(oid, repo, tag.tag_name, obj, error = git_tag_create_o(oid, repo, tag.tag_name, obj, tag.tagger, tag.message);
tag.tagger, tag.message);
git_object_close(obj); git_object_close(obj);
exit_freetag: cleanup:
git_signature_free(tag.tagger); git_signature_free(tag.tagger);
free(tag.tag_name); free(tag.tag_name);
free(tag.message); free(tag.message);
exit_freebuf:
free(buf);
return error; return error;
} }
int git_tag__parse(git_tag *tag, git_odb_object *obj) int git_tag__parse(git_tag *tag, git_odb_object *obj)
{ {
assert(tag); assert(tag);
......
...@@ -127,7 +127,7 @@ size_t git_tree_entrycount(git_tree *tree) ...@@ -127,7 +127,7 @@ size_t git_tree_entrycount(git_tree *tree)
return tree->entries.length; return tree->entries.length;
} }
static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end) static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end)
{ {
int error = GIT_SUCCESS; int error = GIT_SUCCESS;
...@@ -146,7 +146,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end) ...@@ -146,7 +146,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS) if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS)
return GIT_ENOMEM; return GIT_ENOMEM;
entry->attr = strtol(buffer, &buffer, 8); entry->attr = strtol(buffer, (char **)&buffer, 8);
if (*buffer++ != ' ') { if (*buffer++ != ' ') {
error = GIT_EOBJCORRUPTED; error = GIT_EOBJCORRUPTED;
......
...@@ -114,15 +114,15 @@ BEGIN_TEST(parse0, "parse the OID line in a commit") ...@@ -114,15 +114,15 @@ BEGIN_TEST(parse0, "parse the OID line in a commit")
git_oid oid; git_oid oid;
#define TEST_OID_PASS(string, header) { \ #define TEST_OID_PASS(string, header) { \
char *ptr = string;\ const char *ptr = string;\
char *ptr_original = ptr;\ const char *ptr_original = ptr;\
size_t len = strlen(ptr);\ size_t len = strlen(ptr);\
must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\ must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\
must_be_true(ptr == ptr_original + len);\ must_be_true(ptr == ptr_original + len);\
} }
#define TEST_OID_FAIL(string, header) { \ #define TEST_OID_FAIL(string, header) { \
char *ptr = string;\ const char *ptr = string;\
size_t len = strlen(ptr);\ size_t len = strlen(ptr);\
must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\ must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\
} }
...@@ -154,7 +154,7 @@ END_TEST ...@@ -154,7 +154,7 @@ END_TEST
BEGIN_TEST(parse1, "parse the signature line in a commit") BEGIN_TEST(parse1, "parse the signature line in a commit")
#define TEST_SIGNATURE_PASS(_string, _header, _name, _email, _time, _offset) { \ #define TEST_SIGNATURE_PASS(_string, _header, _name, _email, _time, _offset) { \
char *ptr = _string; \ const char *ptr = _string; \
size_t len = strlen(_string);\ size_t len = strlen(_string);\
git_signature person = {NULL, NULL, {0, 0}}; \ git_signature person = {NULL, NULL, {0, 0}}; \
must_pass(git_signature__parse(&person, &ptr, ptr + len, _header));\ must_pass(git_signature__parse(&person, &ptr, ptr + len, _header));\
...@@ -166,7 +166,7 @@ BEGIN_TEST(parse1, "parse the signature line in a commit") ...@@ -166,7 +166,7 @@ BEGIN_TEST(parse1, "parse the signature line in a commit")
} }
#define TEST_SIGNATURE_FAIL(_string, _header) { \ #define TEST_SIGNATURE_FAIL(_string, _header) { \
char *ptr = _string; \ const char *ptr = _string; \
size_t len = strlen(_string);\ size_t len = strlen(_string);\
git_signature person = {NULL, NULL, {0, 0}}; \ git_signature person = {NULL, NULL, {0, 0}}; \
must_fail(git_signature__parse(&person, &ptr, ptr + len, _header));\ must_fail(git_signature__parse(&person, &ptr, ptr + len, _header));\
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment