Commit 7cd0bf65671a1a486242403aaa0237ed5dec9d2b

Edward Thomson 2020-04-05T18:26:52

pack: use GIT_ASSERT

diff --git a/src/indexer.c b/src/indexer.c
index b6a2ee4..7b3db31 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -900,8 +900,8 @@ static int inject_object(git_indexer *idx, git_oid *id)
 	entry->crc = crc32(0L, Z_NULL, 0);
 
 	/* Write out the object header */
-	hdr_len = git_packfile__object_header(hdr, len, git_odb_object_type(obj));
-	if ((error = append_to_pack(idx, hdr, hdr_len)) < 0)
+	if ((error = git_packfile__object_header(&hdr_len, hdr, len, git_odb_object_type(obj))) < 0 ||
+	    (error = append_to_pack(idx, hdr, hdr_len)) < 0)
 		goto cleanup;
 
 	idx->pack->mwf.size += hdr_len;
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 9679e8e..abc636b 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -347,10 +347,9 @@ static int write_object(
 	}
 
 	/* Write header */
-	hdr_len = git_packfile__object_header(hdr, data_len, type);
-
-	if ((error = write_cb(hdr, hdr_len, cb_data)) < 0 ||
-		(error = git_hash_update(&pb->ctx, hdr, hdr_len)) < 0)
+	if ((error = git_packfile__object_header(&hdr_len, hdr, data_len, type)) < 0 ||
+	    (error = write_cb(hdr, hdr_len, cb_data)) < 0 ||
+	    (error = git_hash_update(&pb->ctx, hdr, hdr_len)) < 0)
 		goto done;
 
 	if (type == GIT_OBJECT_REF_DELTA) {
diff --git a/src/pack.c b/src/pack.c
index 1b5cf67..982ad17 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -67,7 +67,6 @@ static void free_cache_object(void *o)
 	git_pack_cache_entry *e = (git_pack_cache_entry *)o;
 
 	if (e != NULL) {
-		assert(e->refcount.val == 0);
 		git__free(e->raw.data);
 		git__free(e);
 	}
@@ -311,8 +310,9 @@ static int pack_index_open(struct git_pack_file *p)
 	if (p->index_version > -1)
 		return 0;
 
+	/* checked by git_pack_file alloc */
 	name_len = strlen(p->pack_name);
-	assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
+	GIT_ASSERT(name_len > strlen(".pack"));
 
 	if (git_buf_init(&idx_name, name_len) < 0)
 		return -1;
@@ -372,12 +372,12 @@ static unsigned char *pack_window_open(
  *  - each byte afterwards: low seven bits are size continuation,
  *    with the high bit being "size continues"
  */
-size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_object_t type)
+int git_packfile__object_header(size_t *out, unsigned char *hdr, size_t size, git_object_t type)
 {
 	unsigned char *hdr_base;
 	unsigned char c;
 
-	assert(type >= GIT_OBJECT_COMMIT && type <= GIT_OBJECT_REF_DELTA);
+	GIT_ASSERT_ARG(type >= GIT_OBJECT_COMMIT && type <= GIT_OBJECT_REF_DELTA);
 
 	/* TODO: add support for chunked objects; see git.git 6c0d19b1 */
 
@@ -392,7 +392,8 @@ size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_object_t
 	}
 	*hdr++ = c;
 
-	return (hdr - hdr_base);
+	*out = (hdr - hdr_base);
+	return 0;
 }
 
 
@@ -899,7 +900,7 @@ int get_delta_base(
 	off64_t base_offset;
 	git_oid unused;
 
-	assert(delta_base_out);
+	GIT_ASSERT_ARG(delta_base_out);
 
 	base_info = pack_window_open(p, w_curs, *curpos, &left);
 	/* Assumption: the only reason this would fail is because the file is too small */
@@ -1211,8 +1212,7 @@ int git_pack_foreach_entry(
 		if ((error = pack_index_open(p)) < 0)
 			return error;
 
-		assert(p->index_map.data);
-
+		GIT_ASSERT(p->index_map.data);
 		index = p->index_map.data;
 	}
 
@@ -1299,7 +1299,8 @@ static int pack_entry_find_offset(
 
 		if ((error = pack_index_open(p)) < 0)
 			return error;
-		assert(p->index_map.data);
+
+		GIT_ASSERT(p->index_map.data);
 	}
 
 	index = p->index_map.data;
@@ -1388,7 +1389,7 @@ int git_pack_entry_find(
 	git_oid found_oid;
 	int error;
 
-	assert(p);
+	GIT_ASSERT_ARG(p);
 
 	if (len == GIT_OID_HEXSZ && p->num_bad_objects) {
 		unsigned i;
diff --git a/src/pack.h b/src/pack.h
index cebfcd1..544a5d2 100644
--- a/src/pack.h
+++ b/src/pack.h
@@ -133,7 +133,7 @@ typedef struct git_packfile_stream {
 	git_mwindow *mw;
 } git_packfile_stream;
 
-size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_object_t type);
+int git_packfile__object_header(size_t *out, unsigned char *hdr, size_t size, git_object_t type);
 
 int git_packfile__name(char **out, const char *path);