diff options
| author | Shawn O. Pearce <spearce@spearce.org> | 2008-12-30 23:21:36 -0800 |
|---|---|---|
| committer | Shawn O. Pearce <spearce@spearce.org> | 2008-12-30 23:28:30 -0800 |
| commit | 64a47c01426a36cdb7e598d17018d5791e54bb97 (patch) | |
| tree | c96d985df9957e7c042290d868d44feed8a4fc08 /src/fileops.c | |
| parent | ffb55c532ccc1cb3c47eeeaac6c64240ab88fe29 (diff) | |
| download | libgit2-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/fileops.c')
| -rw-r--r-- | src/fileops.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/fileops.c b/src/fileops.c index 1e4ac7e37..6522d02a7 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -58,17 +58,18 @@ int gitfo_read_file(gitfo_buf *obj, const char *path) assert(obj && path && *path); if ((fd = gitfo_open(path, O_RDONLY)) < 0) - return GIT_ERROR; /* TODO: error handling */ + return GIT_ERROR; - if (((len = gitfo_size(fd)) < 0) || ((buff = malloc(len+1)) == NULL)) { + if (((len = gitfo_size(fd)) < 0) + || ((buff = git__malloc(len + 1)) == NULL)) { gitfo_close(fd); - return GIT_ERROR; /* TODO: error handling */ + return GIT_ERROR; } if (gitfo_read(fd, buff, len) < 0) { gitfo_close(fd); free(buff); - return GIT_ERROR; /* TODO: error handling */ + return GIT_ERROR; } buff[len] = '\0'; @@ -98,13 +99,13 @@ gitfo_cache *gitfo_enable_caching(git_file fd, size_t cache_size) { gitfo_cache *ioc; - ioc = malloc(sizeof(*ioc)); + ioc = git__malloc(sizeof(*ioc)); if (!ioc) return NULL; ioc->pos = 0; ioc->cache_size = cache_size; - ioc->cache = malloc(cache_size); + ioc->cache = git__malloc(cache_size); if (!ioc->cache) { free(ioc); return NULL; |
