summaryrefslogtreecommitdiff
path: root/src/odb.c
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2008-12-30 23:21:36 -0800
committerShawn O. Pearce <spearce@spearce.org>2008-12-30 23:28:30 -0800
commit64a47c01426a36cdb7e598d17018d5791e54bb97 (patch)
treec96d985df9957e7c042290d868d44feed8a4fc08 /src/odb.c
parentffb55c532ccc1cb3c47eeeaac6c64240ab88fe29 (diff)
downloadlibgit2-64a47c01426a36cdb7e598d17018d5791e54bb97.tar.gz
Wrap malloc and friends and report out of memory as GIT_ENOMEM
We now forbid direct use of malloc, strdup or calloc within the library and instead use wrapper functions git__malloc, etc. to invoke the underlying library malloc and set git_errno to a no memory error code if the allocation fails. In the future once we have pack objects in memory we are likely to enhance these routines with garbage collection logic to purge cached pack data when allocations fail. Because the size of the function will grow somewhat large, we don't want to mark them for inline as gcc tends to aggressively inline, creating larger than expected executables. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/odb.c')
-rw-r--r--src/odb.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/odb.c b/src/odb.c
index e96aa1e25..e074873b8 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -279,7 +279,7 @@ static void *inflate_tail(z_stream *s, void *hb, size_t used, obj_hdr *hdr)
* initial sequence of inflated data from the tail of the
* head buffer, if any.
*/
- if ((buf = malloc(hdr->size + 1)) == NULL)
+ if ((buf = git__malloc(hdr->size + 1)) == NULL)
return NULL;
tail = s->total_out - used;
if (used > 0 && tail > 0) {
@@ -352,7 +352,10 @@ static int inflate_packlike_loose_disk_obj(git_obj *out, gitfo_buf *obj)
/*
* allocate a buffer and inflate the data into it
*/
- buf = malloc(hdr.size+1);
+ buf = git__malloc(hdr.size + 1);
+ if (!buf)
+ return GIT_ERROR;
+
in = ((unsigned char *)obj->data) + used;
len = obj->len - used;
if (inflate_buffer(in, len, buf, hdr.size)) {
@@ -414,7 +417,7 @@ static int open_alternates(git_odb *db)
{
unsigned n = 0;
- db->alternates = malloc(sizeof(*db->alternates) * (n + 1));
+ db->alternates = git__malloc(sizeof(*db->alternates) * (n + 1));
if (!db->alternates)
return GIT_ERROR;
@@ -424,11 +427,11 @@ static int open_alternates(git_odb *db)
int git_odb_open(git_odb **out, const char *objects_dir)
{
- git_odb *db = malloc(sizeof(*db));
+ git_odb *db = git__malloc(sizeof(*db));
if (!db)
return GIT_ERROR;
- db->objects_dir = strdup(objects_dir);
+ db->objects_dir = git__strdup(objects_dir);
if (!db->objects_dir) {
free(db);
return GIT_ERROR;