Commit a227451d by Russell Belfer

Merge pull request #602 from arrbee/more-error-handling

More error handling conversions
parents 0d0fa7c3 a4c291ef
...@@ -123,12 +123,14 @@ typedef struct { ...@@ -123,12 +123,14 @@ typedef struct {
typedef enum { typedef enum {
GITERR_NOMEMORY, GITERR_NOMEMORY,
GITERR_OS, GITERR_OS,
GITERR_INVALID,
GITERR_REFERENCE, GITERR_REFERENCE,
GITERR_ZLIB, GITERR_ZLIB,
GITERR_REPOSITORY, GITERR_REPOSITORY,
GITERR_CONFIG, GITERR_CONFIG,
GITERR_REGEX, GITERR_REGEX,
GITERR_ODB GITERR_ODB,
GITERR_INDEX
} git_error_class; } git_error_class;
/** /**
......
...@@ -108,10 +108,10 @@ mode_t git_futils_canonical_mode(mode_t raw_mode) ...@@ -108,10 +108,10 @@ mode_t git_futils_canonical_mode(mode_t raw_mode)
return S_IFREG | GIT_CANONICAL_PERMS(raw_mode); return S_IFREG | GIT_CANONICAL_PERMS(raw_mode);
else if (S_ISLNK(raw_mode)) else if (S_ISLNK(raw_mode))
return S_IFLNK; return S_IFLNK;
else if (S_ISDIR(raw_mode))
return S_IFDIR;
else if (S_ISGITLINK(raw_mode)) else if (S_ISGITLINK(raw_mode))
return S_IFGITLINK; return S_IFGITLINK;
else if (S_ISDIR(raw_mode))
return S_IFDIR;
else else
return 0; return 0;
} }
......
...@@ -31,4 +31,6 @@ struct git_index { ...@@ -31,4 +31,6 @@ struct git_index {
git_vector unmerged; git_vector unmerged;
}; };
extern void git_index__init_entry_from_stat(struct stat *st, git_index_entry *entry);
#endif #endif
...@@ -49,17 +49,22 @@ static int parse_header(git_indexer *idx) ...@@ -49,17 +49,22 @@ static int parse_header(git_indexer *idx)
int error; int error;
/* Verify we recognize this pack file format. */ /* Verify we recognize this pack file format. */
if ((error = p_read(idx->pack->mwf.fd, &idx->hdr, sizeof(idx->hdr))) < GIT_SUCCESS) if ((error = p_read(idx->pack->mwf.fd, &idx->hdr, sizeof(idx->hdr))) < 0) {
return git__rethrow(error, "Failed to read in pack header"); giterr_set(GITERR_OS, "Failed to read in pack header");
return error;
if (idx->hdr.hdr_signature != ntohl(PACK_SIGNATURE)) }
return git__throw(GIT_EOBJCORRUPTED, "Wrong pack signature");
if (!pack_version_ok(idx->hdr.hdr_version)) if (idx->hdr.hdr_signature != ntohl(PACK_SIGNATURE)) {
return git__throw(GIT_EOBJCORRUPTED, "Wrong pack version"); giterr_set(GITERR_INVALID, "Wrong pack signature");
return -1;
}
if (!pack_version_ok(idx->hdr.hdr_version)) {
giterr_set(GITERR_INVALID, "Wrong pack version");
return -1;
}
return GIT_SUCCESS; return 0;
} }
static int objects_cmp(const void *a, const void *b) static int objects_cmp(const void *a, const void *b)
...@@ -87,49 +92,43 @@ int git_indexer_new(git_indexer **out, const char *packname) ...@@ -87,49 +92,43 @@ int git_indexer_new(git_indexer **out, const char *packname)
assert(out && packname); assert(out && packname);
if (git_path_root(packname) < 0) if (git_path_root(packname) < 0) {
return git__throw(GIT_EINVALIDPATH, "Path is not absolute"); giterr_set(GITERR_INVALID, "Path is not absolute");
return -1;
idx = git__malloc(sizeof(git_indexer)); }
if (idx == NULL)
return GIT_ENOMEM;
memset(idx, 0x0, sizeof(*idx)); idx = git__calloc(1, sizeof(git_indexer));
GITERR_CHECK_ALLOC(idx);
namelen = strlen(packname); namelen = strlen(packname);
idx->pack = git__malloc(sizeof(struct git_pack_file) + namelen + 1); idx->pack = git__calloc(1, sizeof(struct git_pack_file) + namelen + 1);
if (idx->pack == NULL) { GITERR_CHECK_ALLOC(idx->pack);
error = GIT_ENOMEM;
goto cleanup;
}
memset(idx->pack, 0x0, sizeof(struct git_pack_file));
memcpy(idx->pack->pack_name, packname, namelen + 1); memcpy(idx->pack->pack_name, packname, namelen + 1);
ret = p_stat(packname, &idx->st); if ((ret = p_stat(packname, &idx->st)) < 0) {
if (ret < 0) { if (errno == ENOENT) {
if (errno == ENOENT) giterr_set(GITERR_OS, "Failed to stat packfile. File not found");
error = git__throw(GIT_ENOTFOUND, "Failed to stat packfile. File not found"); error = GIT_ENOTFOUND;
else } else {
error = git__throw(GIT_EOSERR, "Failed to stat packfile."); giterr_set(GITERR_OS, "Failed to stat packfile.");
error = -1;
}
goto cleanup; goto cleanup;
} }
ret = p_open(idx->pack->pack_name, O_RDONLY); if ((ret = p_open(idx->pack->pack_name, O_RDONLY)) < 0) {
if (ret < 0) { giterr_set(GITERR_OS, "Failed to open packfile.");
error = git__throw(GIT_EOSERR, "Failed to open packfile"); error = -1;
goto cleanup; goto cleanup;
} }
idx->pack->mwf.fd = ret; idx->pack->mwf.fd = ret;
idx->pack->mwf.size = (git_off_t)idx->st.st_size; idx->pack->mwf.size = (git_off_t)idx->st.st_size;
error = parse_header(idx); if ((error = parse_header(idx)) < 0)
if (error < GIT_SUCCESS) {
error = git__rethrow(error, "Failed to parse packfile header");
goto cleanup; goto cleanup;
}
idx->nr_objects = ntohl(idx->hdr.hdr_entries); idx->nr_objects = ntohl(idx->hdr.hdr_entries);
...@@ -137,17 +136,17 @@ int git_indexer_new(git_indexer **out, const char *packname) ...@@ -137,17 +136,17 @@ int git_indexer_new(git_indexer **out, const char *packname)
assert(idx->nr_objects == (size_t)((unsigned int)idx->nr_objects)); assert(idx->nr_objects == (size_t)((unsigned int)idx->nr_objects));
error = git_vector_init(&idx->pack->cache, (unsigned int)idx->nr_objects, cache_cmp); error = git_vector_init(&idx->pack->cache, (unsigned int)idx->nr_objects, cache_cmp);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
idx->pack->has_cache = 1; idx->pack->has_cache = 1;
error = git_vector_init(&idx->objects, (unsigned int)idx->nr_objects, objects_cmp); error = git_vector_init(&idx->objects, (unsigned int)idx->nr_objects, objects_cmp);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
*out = idx; *out = idx;
return GIT_SUCCESS; return 0;
cleanup: cleanup:
git_indexer_free(idx); git_indexer_free(idx);
...@@ -165,8 +164,8 @@ static int index_path(git_buf *path, git_indexer *idx) ...@@ -165,8 +164,8 @@ static int index_path(git_buf *path, git_indexer *idx)
slash--; slash--;
if (git_buf_grow(path, slash + 1 + strlen(prefix) + if (git_buf_grow(path, slash + 1 + strlen(prefix) +
GIT_OID_HEXSZ + strlen(suffix) + 1) < GIT_SUCCESS) GIT_OID_HEXSZ + strlen(suffix) + 1) < 0)
return GIT_ENOMEM; return -1;
git_buf_truncate(path, slash); git_buf_truncate(path, slash);
git_buf_puts(path, prefix); git_buf_puts(path, prefix);
...@@ -174,10 +173,7 @@ static int index_path(git_buf *path, git_indexer *idx) ...@@ -174,10 +173,7 @@ static int index_path(git_buf *path, git_indexer *idx)
path->size += GIT_OID_HEXSZ; path->size += GIT_OID_HEXSZ;
git_buf_puts(path, suffix); git_buf_puts(path, suffix);
if (git_buf_oom(path)) return git_buf_oom(path) ? -1 : 0;
return GIT_ENOMEM;
return 0;
} }
int git_indexer_write(git_indexer *idx) int git_indexer_write(git_indexer *idx)
...@@ -197,26 +193,25 @@ int git_indexer_write(git_indexer *idx) ...@@ -197,26 +193,25 @@ int git_indexer_write(git_indexer *idx)
git_buf_sets(&filename, idx->pack->pack_name); git_buf_sets(&filename, idx->pack->pack_name);
git_buf_truncate(&filename, filename.size - strlen("pack")); git_buf_truncate(&filename, filename.size - strlen("pack"));
git_buf_puts(&filename, "idx"); git_buf_puts(&filename, "idx");
if (git_buf_oom(&filename)) if (git_buf_oom(&filename))
return GIT_ENOMEM; return -1;
error = git_filebuf_open(&idx->file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS); error = git_filebuf_open(&idx->file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
/* Write out the header */ /* Write out the header */
hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
hdr.idx_version = htonl(2); hdr.idx_version = htonl(2);
error = git_filebuf_write(&idx->file, &hdr, sizeof(hdr)); error = git_filebuf_write(&idx->file, &hdr, sizeof(hdr));
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
/* Write out the fanout table */ /* Write out the fanout table */
for (i = 0; i < 256; ++i) { for (i = 0; i < 256; ++i) {
uint32_t n = htonl(idx->fanout[i]); uint32_t n = htonl(idx->fanout[i]);
error = git_filebuf_write(&idx->file, &n, sizeof(n)); error = git_filebuf_write(&idx->file, &n, sizeof(n));
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
} }
...@@ -225,7 +220,7 @@ int git_indexer_write(git_indexer *idx) ...@@ -225,7 +220,7 @@ int git_indexer_write(git_indexer *idx)
git_vector_foreach(&idx->objects, i, entry) { git_vector_foreach(&idx->objects, i, entry) {
error = git_filebuf_write(&idx->file, &entry->oid, sizeof(git_oid)); error = git_filebuf_write(&idx->file, &entry->oid, sizeof(git_oid));
SHA1_Update(&ctx, &entry->oid, GIT_OID_RAWSZ); SHA1_Update(&ctx, &entry->oid, GIT_OID_RAWSZ);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
} }
SHA1_Final(idx->hash.id, &ctx); SHA1_Final(idx->hash.id, &ctx);
...@@ -233,7 +228,7 @@ int git_indexer_write(git_indexer *idx) ...@@ -233,7 +228,7 @@ int git_indexer_write(git_indexer *idx)
/* Write out the CRC32 values */ /* Write out the CRC32 values */
git_vector_foreach(&idx->objects, i, entry) { git_vector_foreach(&idx->objects, i, entry) {
error = git_filebuf_write(&idx->file, &entry->crc, sizeof(uint32_t)); error = git_filebuf_write(&idx->file, &entry->crc, sizeof(uint32_t));
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
} }
...@@ -247,7 +242,7 @@ int git_indexer_write(git_indexer *idx) ...@@ -247,7 +242,7 @@ int git_indexer_write(git_indexer *idx)
n = htonl(entry->offset); n = htonl(entry->offset);
error = git_filebuf_write(&idx->file, &n, sizeof(uint32_t)); error = git_filebuf_write(&idx->file, &n, sizeof(uint32_t));
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
} }
...@@ -262,7 +257,7 @@ int git_indexer_write(git_indexer *idx) ...@@ -262,7 +257,7 @@ int git_indexer_write(git_indexer *idx)
split[1] = htonl(entry->offset_long & 0xffffffff); split[1] = htonl(entry->offset_long & 0xffffffff);
error = git_filebuf_write(&idx->file, &split, sizeof(uint32_t) * 2); error = git_filebuf_write(&idx->file, &split, sizeof(uint32_t) * 2);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
} }
...@@ -271,7 +266,7 @@ int git_indexer_write(git_indexer *idx) ...@@ -271,7 +266,7 @@ int git_indexer_write(git_indexer *idx)
packfile_hash = git_mwindow_open(&idx->pack->mwf, &w, idx->st.st_size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left); packfile_hash = git_mwindow_open(&idx->pack->mwf, &w, idx->st.st_size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left);
git_mwindow_close(&w); git_mwindow_close(&w);
if (packfile_hash == NULL) { if (packfile_hash == NULL) {
error = git__rethrow(GIT_ENOMEM, "Failed to open window to packfile hash"); error = -1;
goto cleanup; goto cleanup;
} }
...@@ -280,19 +275,21 @@ int git_indexer_write(git_indexer *idx) ...@@ -280,19 +275,21 @@ int git_indexer_write(git_indexer *idx)
git_mwindow_close(&w); git_mwindow_close(&w);
error = git_filebuf_write(&idx->file, &file_hash, sizeof(git_oid)); error = git_filebuf_write(&idx->file, &file_hash, sizeof(git_oid));
if (error < 0)
goto cleanup;
/* Write out the index sha */ /* Write out the index sha */
error = git_filebuf_hash(&file_hash, &idx->file); error = git_filebuf_hash(&file_hash, &idx->file);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
error = git_filebuf_write(&idx->file, &file_hash, sizeof(git_oid)); error = git_filebuf_write(&idx->file, &file_hash, sizeof(git_oid));
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
/* Figure out what the final name should be */ /* Figure out what the final name should be */
error = index_path(&filename, idx); error = index_path(&filename, idx);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
/* Commit file */ /* Commit file */
...@@ -300,7 +297,7 @@ int git_indexer_write(git_indexer *idx) ...@@ -300,7 +297,7 @@ int git_indexer_write(git_indexer *idx)
cleanup: cleanup:
git_mwindow_free_all(&idx->pack->mwf); git_mwindow_free_all(&idx->pack->mwf);
if (error < GIT_SUCCESS) if (error < 0)
git_filebuf_cleanup(&idx->file); git_filebuf_cleanup(&idx->file);
git_buf_free(&filename); git_buf_free(&filename);
...@@ -319,8 +316,8 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats) ...@@ -319,8 +316,8 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats)
mwf = &idx->pack->mwf; mwf = &idx->pack->mwf;
error = git_mwindow_file_register(mwf); error = git_mwindow_file_register(mwf);
if (error < GIT_SUCCESS) if (error < 0)
return git__rethrow(error, "Failed to register mwindow file"); return error;
stats->total = (unsigned int)idx->nr_objects; stats->total = (unsigned int)idx->nr_objects;
stats->processed = processed = 0; stats->processed = processed = 0;
...@@ -346,27 +343,26 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats) ...@@ -346,27 +343,26 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats)
} }
error = git_packfile_unpack(&obj, idx->pack, &off); error = git_packfile_unpack(&obj, idx->pack, &off);
if (error < GIT_SUCCESS) { if (error < 0)
error = git__rethrow(error, "Failed to unpack object");
goto cleanup; goto cleanup;
}
/* FIXME: Parse the object instead of hashing it */ /* FIXME: Parse the object instead of hashing it */
error = git_odb__hashobj(&oid, &obj); error = git_odb__hashobj(&oid, &obj);
if (error < GIT_SUCCESS) { if (error < 0) {
error = git__rethrow(error, "Failed to hash object"); giterr_set(GITERR_INVALID, "Failed to hash object");
goto cleanup; goto cleanup;
} }
pentry = git__malloc(sizeof(struct git_pack_entry)); pentry = git__malloc(sizeof(struct git_pack_entry));
if (pentry == NULL) { if (pentry == NULL) {
error = GIT_ENOMEM; error = -1;
goto cleanup; goto cleanup;
} }
git_oid_cpy(&pentry->sha1, &oid); git_oid_cpy(&pentry->sha1, &oid);
pentry->offset = entry_start; pentry->offset = entry_start;
error = git_vector_insert(&idx->pack->cache, pentry); error = git_vector_insert(&idx->pack->cache, pentry);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
git_oid_cpy(&entry->oid, &oid); git_oid_cpy(&entry->oid, &oid);
...@@ -375,7 +371,7 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats) ...@@ -375,7 +371,7 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats)
entry_size = (size_t)(off - entry_start); entry_size = (size_t)(off - entry_start);
packed = git_mwindow_open(mwf, &w, entry_start, entry_size, &left); packed = git_mwindow_open(mwf, &w, entry_start, entry_size, &left);
if (packed == NULL) { if (packed == NULL) {
error = git__rethrow(error, "Failed to open window to read packed data"); error = -1;
goto cleanup; goto cleanup;
} }
entry->crc = htonl(crc32(entry->crc, packed, (uInt)entry_size)); entry->crc = htonl(crc32(entry->crc, packed, (uInt)entry_size));
...@@ -383,10 +379,8 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats) ...@@ -383,10 +379,8 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats)
/* Add the object to the list */ /* Add the object to the list */
error = git_vector_insert(&idx->objects, entry); error = git_vector_insert(&idx->objects, entry);
if (error < GIT_SUCCESS) { if (error < 0)
error = git__rethrow(error, "Failed to add entry to list");
goto cleanup; goto cleanup;
}
for (i = oid.id[0]; i < 256; ++i) { for (i = oid.id[0]; i < 256; ++i) {
idx->fanout[i]++; idx->fanout[i]++;
......
...@@ -395,7 +395,6 @@ static void workdir_iterator__free(git_iterator *self) ...@@ -395,7 +395,6 @@ static void workdir_iterator__free(git_iterator *self)
static int workdir_iterator__update_entry(workdir_iterator *wi) static int workdir_iterator__update_entry(workdir_iterator *wi)
{ {
int error;
git_path_with_stat *ps = git_vector_get(&wi->stack->entries, wi->stack->index); git_path_with_stat *ps = git_vector_get(&wi->stack->entries, wi->stack->index);
git_buf_truncate(&wi->path, wi->root_len); git_buf_truncate(&wi->path, wi->root_len);
...@@ -412,24 +411,18 @@ static int workdir_iterator__update_entry(workdir_iterator *wi) ...@@ -412,24 +411,18 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
/* if there is an error processing the entry, treat as ignored */ /* if there is an error processing the entry, treat as ignored */
wi->is_ignored = 1; wi->is_ignored = 1;
/* TODO: remove shared code for struct stat conversion with index.c */ git_index__init_entry_from_stat(&ps->st, &wi->entry);
wi->entry.ctime.seconds = (git_time_t)ps->st.st_ctime;
wi->entry.mtime.seconds = (git_time_t)ps->st.st_mtime; /* need different mode here to keep directories during iteration */
wi->entry.dev = ps->st.st_rdev;
wi->entry.ino = ps->st.st_ino;
wi->entry.mode = git_futils_canonical_mode(ps->st.st_mode); wi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
wi->entry.uid = ps->st.st_uid;
wi->entry.gid = ps->st.st_gid;
wi->entry.file_size = ps->st.st_size;
/* if this is a file type we don't handle, treat as ignored */ /* if this is a file type we don't handle, treat as ignored */
if (wi->entry.mode == 0) if (wi->entry.mode == 0)
return 0; return 0;
/* okay, we are far enough along to look up real ignore rule */ /* okay, we are far enough along to look up real ignore rule */
error = git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored); if (git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored) < 0)
if (error < 0) return 0; /* if error, ignore it and ignore file */
return 0;
/* detect submodules */ /* detect submodules */
if (S_ISDIR(wi->entry.mode) && if (S_ISDIR(wi->entry.mode) &&
......
...@@ -541,6 +541,10 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id) ...@@ -541,6 +541,10 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
error = b->read(&raw.data, &raw.len, &raw.type, b, id); error = b->read(&raw.data, &raw.len, &raw.type, b, id);
} }
/* TODO: If no backends are configured, this returns GIT_ENOTFOUND but
* will never have called giterr_set().
*/
if (error && error != GIT_EPASSTHROUGH) if (error && error != GIT_EPASSTHROUGH)
return error; return error;
......
...@@ -13,13 +13,19 @@ ...@@ -13,13 +13,19 @@
static char to_hex[] = "0123456789abcdef"; static char to_hex[] = "0123456789abcdef";
static int oid_error_invalid(const char *msg)
{
giterr_set(GITERR_INVALID, "Unable to parse OID - %s", msg);
return -1;
}
int git_oid_fromstrn(git_oid *out, const char *str, size_t length) int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
{ {
size_t p; size_t p;
int v; int v;
if (length < 4) if (length < 4)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is too short"); return oid_error_invalid("input too short");
if (length > GIT_OID_HEXSZ) if (length > GIT_OID_HEXSZ)
length = GIT_OID_HEXSZ; length = GIT_OID_HEXSZ;
...@@ -29,7 +35,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) ...@@ -29,7 +35,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
| git__fromhex(str[p + 1]); | git__fromhex(str[p + 1]);
if (v < 0) if (v < 0)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash"); return oid_error_invalid("contains invalid characters");
out->id[p / 2] = (unsigned char)v; out->id[p / 2] = (unsigned char)v;
} }
...@@ -37,7 +43,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) ...@@ -37,7 +43,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
if (length % 2) { if (length % 2) {
v = (git__fromhex(str[p + 0]) << 4); v = (git__fromhex(str[p + 0]) << 4);
if (v < 0) if (v < 0)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash"); return oid_error_invalid("contains invalid characters");
out->id[p / 2] = (unsigned char)v; out->id[p / 2] = (unsigned char)v;
p += 2; p += 2;
...@@ -45,7 +51,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) ...@@ -45,7 +51,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2); memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
return GIT_SUCCESS; return 0;
} }
int git_oid_fromstr(git_oid *out, const char *str) int git_oid_fromstr(git_oid *out, const char *str)
...@@ -109,7 +115,8 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid) ...@@ -109,7 +115,8 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
return out; return out;
} }
int git_oid__parse(git_oid *oid, const char **buffer_out, int git_oid__parse(
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;
...@@ -118,20 +125,20 @@ int git_oid__parse(git_oid *oid, const char **buffer_out, ...@@ -118,20 +125,20 @@ int git_oid__parse(git_oid *oid, const char **buffer_out,
const 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__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer too small"); return oid_error_invalid("input is too short");
if (memcmp(buffer, header, header_len) != 0) if (memcmp(buffer, header, header_len) != 0)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer and header do not match"); return oid_error_invalid("did not match expected header");
if (buffer[header_len + sha_len] != '\n') if (buffer[header_len + sha_len] != '\n')
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer not terminated correctly"); return oid_error_invalid("not terminated correctly");
if (git_oid_fromstr(oid, buffer + header_len) < GIT_SUCCESS) if (git_oid_fromstr(oid, buffer + header_len) < 0)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Failed to generate sha1"); return -1;
*buffer_out = buffer + (header_len + sha_len + 1); *buffer_out = buffer + (header_len + sha_len + 1);
return GIT_SUCCESS; return 0;
} }
void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid) void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
...@@ -182,12 +189,11 @@ int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, unsigned int len) ...@@ -182,12 +189,11 @@ int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, unsigned int len)
int git_oid_streq(const git_oid *a, const char *str) int git_oid_streq(const git_oid *a, const char *str)
{ {
git_oid id; git_oid id;
int error;
if ((error = git_oid_fromstr(&id, str)) < GIT_SUCCESS) if (git_oid_fromstr(&id, str) < 0)
return git__rethrow(error, "Failed to convert '%s' to oid.", str); return -1;
return git_oid_cmp(a, &id) == 0 ? GIT_SUCCESS : GIT_ERROR; return git_oid_cmp(a, &id) == 0 ? 0 : -1;
} }
int git_oid_iszero(const git_oid *oid_a) int git_oid_iszero(const git_oid *oid_a)
...@@ -216,15 +222,14 @@ struct git_oid_shorten { ...@@ -216,15 +222,14 @@ struct git_oid_shorten {
static int resize_trie(git_oid_shorten *self, size_t new_size) static int resize_trie(git_oid_shorten *self, size_t new_size)
{ {
self->nodes = git__realloc(self->nodes, new_size * sizeof(trie_node)); self->nodes = git__realloc(self->nodes, new_size * sizeof(trie_node));
if (self->nodes == NULL) GITERR_CHECK_ALLOC(self->nodes);
return GIT_ENOMEM;
if (new_size > self->size) { if (new_size > self->size) {
memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node)); memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
} }
self->size = new_size; self->size = new_size;
return GIT_SUCCESS; return 0;
} }
static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid) static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
...@@ -233,7 +238,7 @@ static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, co ...@@ -233,7 +238,7 @@ static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, co
node_index idx_leaf; node_index idx_leaf;
if (os->node_count >= os->size) { if (os->node_count >= os->size) {
if (resize_trie(os, os->size * 2) < GIT_SUCCESS) if (resize_trie(os, os->size * 2) < 0)
return NULL; return NULL;
} }
...@@ -255,13 +260,11 @@ git_oid_shorten *git_oid_shorten_new(size_t min_length) ...@@ -255,13 +260,11 @@ git_oid_shorten *git_oid_shorten_new(size_t min_length)
{ {
git_oid_shorten *os; git_oid_shorten *os;
os = git__malloc(sizeof(git_oid_shorten)); os = git__calloc(1, sizeof(git_oid_shorten));
if (os == NULL) if (os == NULL)
return NULL; return NULL;
memset(os, 0x0, sizeof(git_oid_shorten)); if (resize_trie(os, 16) < 0) {
if (resize_trie(os, 16) < GIT_SUCCESS) {
git__free(os); git__free(os);
return NULL; return NULL;
} }
...@@ -329,7 +332,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) ...@@ -329,7 +332,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
node_index idx; node_index idx;
if (os->full) if (os->full)
return GIT_ENOMEM; return -1;
if (text_oid == NULL) if (text_oid == NULL)
return os->min_length; return os->min_length;
...@@ -341,8 +344,10 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) ...@@ -341,8 +344,10 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
int c = git__fromhex(text_oid[i]); int c = git__fromhex(text_oid[i]);
trie_node *node; trie_node *node;
if (c == -1) if (c == -1) {
return git__throw(GIT_ENOTOID, "Failed to shorten OID. Invalid hex value"); giterr_set(GITERR_INVALID, "Unable to shorten OID - invalid hex value");
return -1;
}
node = &os->nodes[idx]; node = &os->nodes[idx];
...@@ -353,13 +358,12 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) ...@@ -353,13 +358,12 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
node->tail = NULL; node->tail = NULL;
node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]); node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]);
if (node == NULL) GITERR_CHECK_ALLOC(node);
return GIT_ENOMEM;
} }
if (node->children[c] == 0) { if (node->children[c] == 0) {
if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL) if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL)
return GIT_ENOMEM; return -1;
break; break;
} }
......
...@@ -16,23 +16,21 @@ static int reflog_init(git_reflog **reflog, git_reference *ref) ...@@ -16,23 +16,21 @@ static int reflog_init(git_reflog **reflog, git_reference *ref)
*reflog = NULL; *reflog = NULL;
log = git__malloc(sizeof(git_reflog)); log = git__calloc(1, sizeof(git_reflog));
if (log == NULL) GITERR_CHECK_ALLOC(log);
return GIT_ENOMEM;
memset(log, 0x0, sizeof(git_reflog));
log->ref_name = git__strdup(ref->name); log->ref_name = git__strdup(ref->name);
GITERR_CHECK_ALLOC(log->ref_name);
if (git_vector_init(&log->entries, 0, NULL) < 0) { if (git_vector_init(&log->entries, 0, NULL) < 0) {
git__free(log->ref_name); git__free(log->ref_name);
git__free(log); git__free(log);
return GIT_ENOMEM; return -1;
} }
*reflog = log; *reflog = log;
return GIT_SUCCESS; return 0;
} }
static int reflog_write(const char *log_path, const char *oid_old, static int reflog_write(const char *log_path, const char *oid_old,
...@@ -42,9 +40,22 @@ static int reflog_write(const char *log_path, const char *oid_old, ...@@ -42,9 +40,22 @@ static int reflog_write(const char *log_path, const char *oid_old,
int error; int error;
git_buf log = GIT_BUF_INIT; git_buf log = GIT_BUF_INIT;
git_filebuf fbuf = GIT_FILEBUF_INIT; git_filebuf fbuf = GIT_FILEBUF_INIT;
bool trailing_newline = false;
assert(log_path && oid_old && oid_new && committer); assert(log_path && oid_old && oid_new && committer);
if (msg) {
const char *newline = strchr(msg, '\n');
if (newline) {
if (*(newline + 1) == '\0')
trailing_newline = true;
else {
giterr_set(GITERR_INVALID, "Reflog message cannot contain newline");
return -1;
}
}
}
git_buf_puts(&log, oid_old); git_buf_puts(&log, oid_old);
git_buf_putc(&log, ' '); git_buf_putc(&log, ' ');
...@@ -54,68 +65,58 @@ static int reflog_write(const char *log_path, const char *oid_old, ...@@ -54,68 +65,58 @@ static int reflog_write(const char *log_path, const char *oid_old,
git_buf_truncate(&log, log.size - 1); /* drop LF */ git_buf_truncate(&log, log.size - 1); /* drop LF */
if (msg) { if (msg) {
if (strchr(msg, '\n')) {
git_buf_free(&log);
return git__throw(GIT_ERROR, "Reflog message cannot contain newline");
}
git_buf_putc(&log, '\t'); git_buf_putc(&log, '\t');
git_buf_puts(&log, msg); git_buf_puts(&log, msg);
} }
if (!trailing_newline)
git_buf_putc(&log, '\n'); git_buf_putc(&log, '\n');
if (git_buf_oom(&log)) { if (git_buf_oom(&log)) {
git_buf_free(&log); git_buf_free(&log);
return git__throw(GIT_ENOMEM, "Failed to write reflog. Memory allocation failure"); return -1;
}
if ((error = git_filebuf_open(&fbuf, log_path, GIT_FILEBUF_APPEND)) < GIT_SUCCESS) {
git_buf_free(&log);
return git__rethrow(error, "Failed to write reflog. Cannot open reflog `%s`", log_path);
} }
git_filebuf_write(&fbuf, log.ptr, log.size); error = git_filebuf_open(&fbuf, log_path, GIT_FILEBUF_APPEND);
if (!error) {
if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
git_filebuf_cleanup(&fbuf);
else
error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE); error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE);
}
git_buf_free(&log); git_buf_free(&log);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write reflog"); return error;
} }
static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
{ {
int error = GIT_SUCCESS;
const char *ptr; const char *ptr;
git_reflog_entry *entry; git_reflog_entry *entry;
#define seek_forward(_increase) { \ #define seek_forward(_increase) do { \
if (_increase >= buf_size) { \ if (_increase >= buf_size) { \
if (entry->committer) \ giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
git__free(entry->committer); \ goto fail; \
git__free(entry); \
return git__throw(GIT_ERROR, "Failed to seek forward. Buffer size exceeded"); \
} \ } \
buf += _increase; \ buf += _increase; \
buf_size -= _increase; \ buf_size -= _increase; \
} } while (0)
while (buf_size > GIT_REFLOG_SIZE_MIN) { while (buf_size > GIT_REFLOG_SIZE_MIN) {
entry = git__malloc(sizeof(git_reflog_entry)); entry = git__malloc(sizeof(git_reflog_entry));
if (entry == NULL) GITERR_CHECK_ALLOC(entry);
return GIT_ENOMEM;
entry->committer = NULL;
if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) { entry->committer = git__malloc(sizeof(git_signature));
git__free(entry); GITERR_CHECK_ALLOC(entry->committer);
return GIT_ERROR;
} if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0)
goto fail;
seek_forward(GIT_OID_HEXSZ + 1); seek_forward(GIT_OID_HEXSZ + 1);
if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) { if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0)
git__free(entry); goto fail;
return GIT_ERROR;
}
seek_forward(GIT_OID_HEXSZ + 1); seek_forward(GIT_OID_HEXSZ + 1);
ptr = buf; ptr = buf;
...@@ -124,17 +125,8 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) ...@@ -124,17 +125,8 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
while (*buf && *buf != '\t' && *buf != '\n') while (*buf && *buf != '\t' && *buf != '\n')
seek_forward(1); seek_forward(1);
entry->committer = git__malloc(sizeof(git_signature)); if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0)
if (entry->committer == NULL) { goto fail;
git__free(entry);
return GIT_ENOMEM;
}
if ((error = git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf)) < GIT_SUCCESS) {
git__free(entry->committer);
git__free(entry);
return git__rethrow(error, "Failed to parse reflog. Could not parse signature");
}
if (*buf == '\t') { if (*buf == '\t') {
/* We got a message. Read everything till we reach LF. */ /* We got a message. Read everything till we reach LF. */
...@@ -145,19 +137,27 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) ...@@ -145,19 +137,27 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
seek_forward(1); seek_forward(1);
entry->msg = git__strndup(ptr, buf - ptr); entry->msg = git__strndup(ptr, buf - ptr);
GITERR_CHECK_ALLOC(entry->msg);
} else } else
entry->msg = NULL; entry->msg = NULL;
while (*buf && *buf == '\n' && buf_size > 1) while (*buf && *buf == '\n' && buf_size > 1)
seek_forward(1); seek_forward(1);
if ((error = git_vector_insert(&log->entries, entry)) < GIT_SUCCESS) if (git_vector_insert(&log->entries, entry) < 0)
return git__rethrow(error, "Failed to parse reflog. Could not add new entry"); goto fail;
} }
return 0;
#undef seek_forward #undef seek_forward
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse reflog"); fail:
if (entry) {
git__free(entry->committer);
git__free(entry);
}
return -1;
} }
void git_reflog_free(git_reflog *reflog) void git_reflog_free(git_reflog *reflog)
...@@ -188,27 +188,23 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref) ...@@ -188,27 +188,23 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref)
*reflog = NULL; *reflog = NULL;
if ((error = reflog_init(&log, ref)) < GIT_SUCCESS) if (reflog_init(&log, ref) < 0)
return git__rethrow(error, "Failed to read reflog. Cannot init reflog"); return -1;
error = git_buf_join_n(&log_path, '/', 3, error = git_buf_join_n(&log_path, '/', 3,
ref->owner->path_repository, GIT_REFLOG_DIR, ref->name); ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
if (error < GIT_SUCCESS)
goto cleanup;
if ((error = git_futils_readbuffer(&log_file, log_path.ptr)) < GIT_SUCCESS) { if (!error)
git__rethrow(error, "Failed to read reflog. Cannot read file `%s`", log_path.ptr); error = git_futils_readbuffer(&log_file, log_path.ptr);
goto cleanup;
}
if ((error = reflog_parse(log, log_file.ptr, log_file.size)) < GIT_SUCCESS) if (!error)
git__rethrow(error, "Failed to read reflog"); error = reflog_parse(log, log_file.ptr, log_file.size);
else
*reflog = log;
cleanup: if (!error)
if (error != GIT_SUCCESS && log != NULL) *reflog = log;
else
git_reflog_free(log); git_reflog_free(log);
git_buf_free(&log_file); git_buf_free(&log_file);
git_buf_free(&log_path); git_buf_free(&log_path);
...@@ -225,16 +221,15 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old, ...@@ -225,16 +221,15 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
git_reference *r; git_reference *r;
const git_oid *oid; const git_oid *oid;
if ((error = git_reference_resolve(&r, ref)) < GIT_SUCCESS) if ((error = git_reference_resolve(&r, ref)) < 0)
return git__rethrow(error, return error;
"Failed to write reflog. Cannot resolve reference `%s`", ref->name);
oid = git_reference_oid(r); oid = git_reference_oid(r);
if (oid == NULL) { if (oid == NULL) {
error = git__throw(GIT_ERROR, giterr_set(GITERR_REFERENCE,
"Failed to write reflog. Cannot resolve reference `%s`", r->name); "Failed to write reflog. Cannot resolve reference `%s`", r->name);
git_reference_free(r); git_reference_free(r);
return error; return -1;
} }
git_oid_to_string(new, GIT_OID_HEXSZ+1, oid); git_oid_to_string(new, GIT_OID_HEXSZ+1, oid);
...@@ -243,23 +238,21 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old, ...@@ -243,23 +238,21 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
error = git_buf_join_n(&log_path, '/', 3, error = git_buf_join_n(&log_path, '/', 3,
ref->owner->path_repository, GIT_REFLOG_DIR, ref->name); ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
if (error < GIT_SUCCESS) if (error < 0)
goto cleanup; goto cleanup;
if (git_path_exists(log_path.ptr) == false) { if (git_path_exists(log_path.ptr) == false) {
error = git_futils_mkpath2file(log_path.ptr, GIT_REFLOG_DIR_MODE); error = git_futils_mkpath2file(log_path.ptr, GIT_REFLOG_DIR_MODE);
if (error < GIT_SUCCESS)
git__rethrow(error,
"Failed to write reflog. Cannot create reflog directory");
} else if (git_path_isfile(log_path.ptr) == false) { } else if (git_path_isfile(log_path.ptr) == false) {
error = git__throw(GIT_ERROR, giterr_set(GITERR_REFERENCE,
"Failed to write reflog. `%s` is directory", log_path.ptr); "Failed to write reflog. `%s` is directory", log_path.ptr);
error = -1;
} else if (oid_old == NULL) { } else if (oid_old == NULL) {
error = git__throw(GIT_ERROR, giterr_set(GITERR_REFERENCE,
"Failed to write reflog. Old OID cannot be NULL for existing reference"); "Failed to write reflog. Old OID cannot be NULL for existing reference");
error = -1;
} }
if (error < 0)
if (error < GIT_SUCCESS)
goto cleanup; goto cleanup;
if (oid_old) if (oid_old)
...@@ -280,13 +273,13 @@ int git_reflog_rename(git_reference *ref, const char *new_name) ...@@ -280,13 +273,13 @@ int git_reflog_rename(git_reference *ref, const char *new_name)
git_buf old_path = GIT_BUF_INIT; git_buf old_path = GIT_BUF_INIT;
git_buf new_path = GIT_BUF_INIT; git_buf new_path = GIT_BUF_INIT;
if (git_buf_join_n(&old_path, '/', 3, ref->owner->path_repository, if (!git_buf_join_n(&old_path, '/', 3, ref->owner->path_repository,
GIT_REFLOG_DIR, ref->name) && GIT_REFLOG_DIR, ref->name) &&
git_buf_join_n(&new_path, '/', 3, ref->owner->path_repository, !git_buf_join_n(&new_path, '/', 3, ref->owner->path_repository,
GIT_REFLOG_DIR, new_name)) GIT_REFLOG_DIR, new_name))
error = p_rename(git_buf_cstr(&old_path), git_buf_cstr(&new_path)); error = p_rename(git_buf_cstr(&old_path), git_buf_cstr(&new_path));
else else
error = GIT_ENOMEM; error = -1;
git_buf_free(&old_path); git_buf_free(&old_path);
git_buf_free(&new_path); git_buf_free(&new_path);
...@@ -296,13 +289,13 @@ int git_reflog_rename(git_reference *ref, const char *new_name) ...@@ -296,13 +289,13 @@ int git_reflog_rename(git_reference *ref, const char *new_name)
int git_reflog_delete(git_reference *ref) int git_reflog_delete(git_reference *ref)
{ {
int error = GIT_SUCCESS; int error;
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
error = git_buf_join_n(&path, '/', 3, error = git_buf_join_n(
ref->owner->path_repository, GIT_REFLOG_DIR, ref->name); &path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
if (error == GIT_SUCCESS && git_path_exists(path.ptr) == true) if (!error && git_path_exists(path.ptr))
error = p_unlink(path.ptr); error = p_unlink(path.ptr);
git_buf_free(&path); git_buf_free(&path);
......
...@@ -158,7 +158,8 @@ int sha1_entry_pos(const void *table, ...@@ -158,7 +158,8 @@ int sha1_entry_pos(const void *table,
#endif #endif
if (!(lo <= mi && mi < hi)) { if (!(lo <= mi && mi < hi)) {
return git__throw(GIT_ERROR, "Assertion failure. Binary search invariant is false"); giterr_set(GITERR_INVALID, "Assertion failure. Binary search invariant is false");
return -1;
} }
mi_key = base + elem_size * mi + key_offset; mi_key = base + elem_size * mi + key_offset;
......
...@@ -38,31 +38,38 @@ static const char *skip_trailing_spaces(const char *buffer_start, const char *bu ...@@ -38,31 +38,38 @@ static const char *skip_trailing_spaces(const char *buffer_start, const char *bu
return buffer_end; return buffer_end;
} }
static int signature_error(const char *msg)
{
giterr_set(GITERR_INVALID, "Failed to parse signature - %s", msg);
return -1;
}
static int process_trimming(const char *input, char **storage, const char *input_end, int fail_when_empty) static int process_trimming(const char *input, char **storage, const char *input_end, int fail_when_empty)
{ {
const char *left, *right; const char *left, *right;
int trimmed_input_length; int trimmed_input_length;
assert(storage);
left = skip_leading_spaces(input, input_end); left = skip_leading_spaces(input, input_end);
right = skip_trailing_spaces(input, input_end - 1); right = skip_trailing_spaces(input, input_end - 1);
if (right < left) { if (right < left) {
if (fail_when_empty) if (fail_when_empty)
return git__throw(GIT_EINVALIDARGS, "Failed to trim. Input is either empty or only contains spaces"); return signature_error("input is either empty of contains only spaces");
else
right = left - 1; right = left - 1;
} }
trimmed_input_length = right - left + 1; trimmed_input_length = right - left + 1;
*storage = git__malloc(trimmed_input_length + 1); *storage = git__malloc(trimmed_input_length + 1);
if (*storage == NULL) GITERR_CHECK_ALLOC(*storage);
return GIT_ENOMEM;
memcpy(*storage, left, trimmed_input_length); memcpy(*storage, left, trimmed_input_length);
(*storage)[trimmed_input_length] = 0; (*storage)[trimmed_input_length] = 0;
return GIT_SUCCESS; return 0;
} }
int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset) int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset)
...@@ -74,23 +81,14 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema ...@@ -74,23 +81,14 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema
*sig_out = NULL; *sig_out = NULL;
if ((p = git__malloc(sizeof(git_signature))) == NULL) { p = git__calloc(1, sizeof(git_signature));
error = GIT_ENOMEM; GITERR_CHECK_ALLOC(p);
goto cleanup;
}
memset(p, 0x0, sizeof(git_signature));
error = process_trimming(name, &p->name, name + strlen(name), 1); if ((error = process_trimming(name, &p->name, name + strlen(name), 1)) < 0 ||
if (error < GIT_SUCCESS) { (error = process_trimming(email, &p->email, email + strlen(email), 1)) < 0)
git__rethrow(GIT_EINVALIDARGS, "Failed to create signature. 'name' argument is invalid"); {
goto cleanup; git_signature_free(p);
} return error;
error = process_trimming(email, &p->email, email + strlen(email), 1);
if (error < GIT_SUCCESS) {
git__rethrow(GIT_EINVALIDARGS, "Failed to create signature. 'email' argument is invalid");
goto cleanup;
} }
p->when.time = time; p->when.time = time;
...@@ -98,24 +96,19 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema ...@@ -98,24 +96,19 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema
*sig_out = p; *sig_out = p;
return error; return 0;
cleanup:
git_signature_free(p);
return error;
} }
git_signature *git_signature_dup(const git_signature *sig) git_signature *git_signature_dup(const git_signature *sig)
{ {
git_signature *new; git_signature *new;
if (git_signature_new(&new, sig->name, sig->email, sig->when.time, sig->when.offset) < GIT_SUCCESS) if (git_signature_new(&new, sig->name, sig->email, sig->when.time, sig->when.offset) < 0)
return NULL; return NULL;
return new; return new;
} }
int git_signature_now(git_signature **sig_out, const char *name, const char *email) int git_signature_now(git_signature **sig_out, const char *name, const char *email)
{ {
int error;
time_t now; time_t now;
time_t offset; time_t offset;
struct tm *utc_tm, *local_tm; struct tm *utc_tm, *local_tm;
...@@ -148,12 +141,18 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema ...@@ -148,12 +141,18 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema
if (local_tm->tm_isdst) if (local_tm->tm_isdst)
offset += 60; offset += 60;
if ((error = git_signature_new(&sig, name, email, now, (int)offset)) < GIT_SUCCESS) if (git_signature_new(&sig, name, email, now, (int)offset) < 0)
return error; return -1;
*sig_out = sig; *sig_out = sig;
return error; return 0;
}
static int timezone_error(const char *msg)
{
giterr_set(GITERR_INVALID, "Failed to parse TZ offset - %s", msg);
return -1;
} }
static int parse_timezone_offset(const char *buffer, int *offset_out) static int parse_timezone_offset(const char *buffer, int *offset_out)
...@@ -172,28 +171,28 @@ static int parse_timezone_offset(const char *buffer, int *offset_out) ...@@ -172,28 +171,28 @@ static int parse_timezone_offset(const char *buffer, int *offset_out)
} }
if (offset_start[0] != '-' && offset_start[0] != '+') if (offset_start[0] != '-' && offset_start[0] != '+')
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. It doesn't start with '+' or '-'"); return timezone_error("does not start with '+' or '-'");
if (offset_start[1] < '0' || offset_start[1] > '9') if (offset_start[1] < '0' || offset_start[1] > '9')
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset."); return timezone_error("expected initial digit");
if (git__strtol32(&dec_offset, offset_start + 1, &offset_end, 10) < GIT_SUCCESS) if (git__strtol32(&dec_offset, offset_start + 1, &offset_end, 10) < GIT_SUCCESS)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. It isn't a number"); return timezone_error("not a valid number");
if (offset_end - offset_start != 5) if (offset_end - offset_start != 5)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Invalid length"); return timezone_error("invalid length");
if (dec_offset > 1400) if (dec_offset > 1400)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Value too large"); return timezone_error("value too large");
hours = dec_offset / 100; hours = dec_offset / 100;
mins = dec_offset % 100; mins = dec_offset % 100;
if (hours > 14) // see http://www.worldtimezone.com/faq.html if (hours > 14) // see http://www.worldtimezone.com/faq.html
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Hour value too large"); return timezone_error("hour value too large");
if (mins > 59) if (mins > 59)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Minute value too large"); return timezone_error("minutes value too large");
offset = (hours * 60) + mins; offset = (hours * 60) + mins;
...@@ -202,22 +201,22 @@ static int parse_timezone_offset(const char *buffer, int *offset_out) ...@@ -202,22 +201,22 @@ static int parse_timezone_offset(const char *buffer, int *offset_out)
*offset_out = offset; *offset_out = offset;
return GIT_SUCCESS; return 0;
} }
static int process_next_token(const char **buffer_out, char **storage, static int process_next_token(const char **buffer_out, char **storage,
const char *token_end, const char *right_boundary) const char *token_end, const char *right_boundary)
{ {
int error = process_trimming(*buffer_out, storage, token_end, 0); int error = process_trimming(*buffer_out, storage, token_end, 0);
if (error < GIT_SUCCESS) if (error < 0)
return error; return error;
*buffer_out = token_end + 1; *buffer_out = token_end + 1;
if (*buffer_out > right_boundary) if (*buffer_out > right_boundary)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Signature too short"); return signature_error("signature is too short");
return GIT_SUCCESS; return 0;
} }
static const char *scan_for_previous_token(const char *buffer, const char *left_boundary) static const char *scan_for_previous_token(const char *buffer, const char *left_boundary)
...@@ -241,17 +240,17 @@ static int parse_time(git_time_t *time_out, const char *buffer) ...@@ -241,17 +240,17 @@ static int parse_time(git_time_t *time_out, const char *buffer)
int time; int time;
int error; int error;
if (*buffer == '+' || *buffer == '-') if (*buffer == '+' || *buffer == '-') {
return git__throw(GIT_ERROR, "Failed while parsing time. '%s' rather look like a timezone offset.", buffer); giterr_set(GITERR_INVALID, "Failed while parsing time. '%s' actually looks like a timezone offset.", buffer);
return -1;
}
error = git__strtol32(&time, buffer, &buffer, 10); error = git__strtol32(&time, buffer, &buffer, 10);
if (error < GIT_SUCCESS) if (!error)
return error;
*time_out = (git_time_t)time; *time_out = (git_time_t)time;
return GIT_SUCCESS; return error;
} }
int git_signature__parse(git_signature *sig, const char **buffer_out, int git_signature__parse(git_signature *sig, const char **buffer_out,
...@@ -259,40 +258,40 @@ int git_signature__parse(git_signature *sig, const char **buffer_out, ...@@ -259,40 +258,40 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
{ {
const char *buffer = *buffer_out; const char *buffer = *buffer_out;
const char *line_end, *name_end, *email_end, *tz_start, *time_start; const char *line_end, *name_end, *email_end, *tz_start, *time_start;
int error = GIT_SUCCESS; int error = 0;
memset(sig, 0x0, sizeof(git_signature)); memset(sig, 0x0, sizeof(git_signature));
if ((line_end = memchr(buffer, ender, buffer_end - buffer)) == NULL) if ((line_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. No newline given"); return signature_error("no newline given");
if (header) { if (header) {
const size_t header_len = strlen(header); const size_t header_len = strlen(header);
if (memcmp(buffer, header, header_len) != 0) if (memcmp(buffer, header, header_len) != 0)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Expected prefix '%s' doesn't match actual", header); return signature_error("expected prefix doesn't match actual");
buffer += header_len; buffer += header_len;
} }
if (buffer > line_end) if (buffer > line_end)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Signature too short"); return signature_error("signature too short");
if ((name_end = strchr(buffer, '<')) == NULL) if ((name_end = strchr(buffer, '<')) == NULL)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Cannot find '<' in signature"); return signature_error("character '<' not allowed in signature");
if ((email_end = strchr(name_end, '>')) == NULL) if ((email_end = strchr(name_end, '>')) == NULL)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Cannot find '>' in signature"); return signature_error("character '>' not allowed in signature");
if (email_end < name_end) if (email_end < name_end)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Malformed e-mail"); return signature_error("malformed e-mail");
error = process_next_token(&buffer, &sig->name, name_end, line_end); error = process_next_token(&buffer, &sig->name, name_end, line_end);
if (error < GIT_SUCCESS) if (error < 0)
return error; return error;
error = process_next_token(&buffer, &sig->email, email_end, line_end); error = process_next_token(&buffer, &sig->email, email_end, line_end);
if (error < GIT_SUCCESS) if (error < 0)
return error; return error;
tz_start = scan_for_previous_token(line_end - 1, buffer); tz_start = scan_for_previous_token(line_end - 1, buffer);
...@@ -301,19 +300,19 @@ int git_signature__parse(git_signature *sig, const char **buffer_out, ...@@ -301,19 +300,19 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
goto clean_exit; /* No timezone nor date */ goto clean_exit; /* No timezone nor date */
time_start = scan_for_previous_token(tz_start - 1, buffer); time_start = scan_for_previous_token(tz_start - 1, buffer);
if (time_start == NULL || parse_time(&sig->when.time, time_start) < GIT_SUCCESS) { if (time_start == NULL || parse_time(&sig->when.time, time_start) < 0) {
/* The tz_start might point at the time */ /* The tz_start might point at the time */
parse_time(&sig->when.time, tz_start); parse_time(&sig->when.time, tz_start);
goto clean_exit; goto clean_exit;
} }
if (parse_timezone_offset(tz_start, &sig->when.offset) < GIT_SUCCESS) { if (parse_timezone_offset(tz_start, &sig->when.offset) < 0) {
sig->when.time = 0; /* Bogus timezone, we reset the time */ sig->when.time = 0; /* Bogus timezone, we reset the time */
} }
clean_exit: clean_exit:
*buffer_out = line_end + 1; *buffer_out = line_end + 1;
return GIT_SUCCESS; return 0;
} }
void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig) void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig)
......
...@@ -112,31 +112,37 @@ int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int ba ...@@ -112,31 +112,37 @@ int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int ba
} }
Return: Return:
if (ndig == 0) if (ndig == 0) {
return git__throw(GIT_ENOTNUM, "Failed to convert string to long. Not a number"); giterr_set(GITERR_INVALID, "Failed to convert string to long. Not a number");
return -1;
}
if (endptr) if (endptr)
*endptr = p; *endptr = p;
if (ovfl) if (ovfl) {
return git__throw(GIT_EOVERFLOW, "Failed to convert string to long. Overflow error"); giterr_set(GITERR_INVALID, "Failed to convert string to long. Overflow error");
return -1;
}
*result = neg ? -n : n; *result = neg ? -n : n;
return GIT_SUCCESS; return 0;
} }
int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base) int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base)
{ {
int error = GIT_SUCCESS; int error;
int32_t tmp_int; int32_t tmp_int;
int64_t tmp_long; int64_t tmp_long;
if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < GIT_SUCCESS) if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < 0)
return error; return error;
tmp_int = tmp_long & 0xFFFFFFFF; tmp_int = tmp_long & 0xFFFFFFFF;
if (tmp_int != tmp_long) if (tmp_int != tmp_long) {
return git__throw(GIT_EOVERFLOW, "Failed to convert. '%s' is too large", nptr); giterr_set(GITERR_INVALID, "Failed to convert. '%s' is too large", nptr);
return -1;
}
*result = tmp_int; *result = tmp_int;
......
...@@ -544,3 +544,20 @@ void test_core_buffer__9(void) ...@@ -544,3 +544,20 @@ void test_core_buffer__9(void)
git_buf_free(&buf); git_buf_free(&buf);
} }
void test_core_buffer__10(void)
{
git_buf a = GIT_BUF_INIT;
cl_git_pass(git_buf_join_n(&a, '/', 1, "test"));
cl_assert_strequal(a.ptr, "test");
cl_git_pass(git_buf_join_n(&a, '/', 1, "string"));
cl_assert_strequal(a.ptr, "test/string");
git_buf_clear(&a);
cl_git_pass(git_buf_join_n(&a, '/', 3, "test", "string", "join"));
cl_assert_strequal(a.ptr, "test/string/join");
cl_git_pass(git_buf_join_n(&a, '/', 2, a.ptr, "more"));
cl_assert_strequal(a.ptr, "test/string/join/test/string/join/more");
git_buf_free(&a);
}
...@@ -10,9 +10,9 @@ void test_core_oid__initialize(void) ...@@ -10,9 +10,9 @@ void test_core_oid__initialize(void)
void test_core_oid__streq(void) void test_core_oid__streq(void)
{ {
cl_assert(git_oid_streq(&id, str_oid) == GIT_SUCCESS); cl_assert(git_oid_streq(&id, str_oid) == 0);
cl_assert(git_oid_streq(&id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") == GIT_ERROR); cl_assert(git_oid_streq(&id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") == -1);
cl_assert(git_oid_streq(&id, "deadbeef") == GIT_ENOTOID); cl_assert(git_oid_streq(&id, "deadbeef") == -1);
cl_assert(git_oid_streq(&id, "I'm not an oid.... :)") == GIT_ENOTOID); cl_assert(git_oid_streq(&id, "I'm not an oid.... :)") == -1);
} }
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