summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2014-01-15 12:57:22 +0100
committerVicent Marti <tanoku@gmail.com>2014-01-15 15:02:23 +0100
commit0f7d72624876f8d01be22196b65f1385cd5a2ca4 (patch)
tree35854a5ff25840a6df4fa83c146387ed01a356ee
parentdec1ac755d57ecd5e2f91ee18d998d2ec0803a1e (diff)
downloadlibgit2-0f7d72624876f8d01be22196b65f1385cd5a2ca4.tar.gz
Replace `giterr_set_oom` with `giterr_panic`
-rw-r--r--include/git2/common.h3
-rw-r--r--include/git2/errors.h10
-rw-r--r--src/common.h5
-rw-r--r--src/errors.c16
-rw-r--r--src/filebuf.c2
-rw-r--r--src/pack-objects.c4
-rw-r--r--src/settings.c4
-rw-r--r--src/util.h11
-rw-r--r--src/zstream.c2
-rw-r--r--tests/core/errors.c18
10 files changed, 24 insertions, 51 deletions
diff --git a/include/git2/common.h b/include/git2/common.h
index dca0c9c21..d535d9006 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -138,7 +138,8 @@ typedef enum {
GIT_OPT_ENABLE_CACHING,
GIT_OPT_GET_CACHED_MEMORY,
GIT_OPT_GET_TEMPLATE_PATH,
- GIT_OPT_SET_TEMPLATE_PATH
+ GIT_OPT_SET_TEMPLATE_PATH,
+ GIT_OPT_SET_PANIC_HANDLER
} git_libgit2_opt_t;
/**
diff --git a/include/git2/errors.h b/include/git2/errors.h
index 973d56003..1769d7be2 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -136,15 +136,9 @@ GIT_EXTERN(int) giterr_detach(git_error *cpy);
GIT_EXTERN(void) giterr_set_str(int error_class, const char *string);
/**
- * Set the error message to a special value for memory allocation failure.
- *
- * The normal `giterr_set_str()` function attempts to `strdup()` the string
- * that is passed in. This is not a good idea when the error in question
- * is a memory allocation failure. That circumstance has a special setter
- * function that sets the error string to a known and statically allocated
- * internal value.
+ * Panic
*/
-GIT_EXTERN(void) giterr_set_oom(void);
+GIT_EXTERN(void) giterr_panic(const char *error);
/** @} */
GIT_END_DECL
diff --git a/src/common.h b/src/common.h
index e315b5979..7f49c1ae8 100644
--- a/src/common.h
+++ b/src/common.h
@@ -53,10 +53,7 @@
#include <regex.h>
-/**
- * Check a pointer allocation result, returning -1 if it failed.
- */
-#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
+extern void (*git__on_panic)(const char *);
/**
* Check a return value and propogate result if non-zero.
diff --git a/src/errors.c b/src/errors.c
index a0b085923..e3057ffa7 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -14,11 +14,6 @@
* New error handling
********************************************/
-static git_error g_git_oom_error = {
- "Out of memory",
- GITERR_NOMEMORY
-};
-
static void set_error(int error_class, char *string)
{
git_error *error = &GIT_GLOBAL->error_t;
@@ -32,9 +27,16 @@ static void set_error(int error_class, char *string)
GIT_GLOBAL->last_error = error;
}
-void giterr_set_oom(void)
+void (*git__on_panic)(const char *) = NULL;
+
+void giterr_panic(const char *message)
{
- GIT_GLOBAL->last_error = &g_git_oom_error;
+ if (git__on_panic != NULL) {
+ git__on_panic(message);
+ }
+
+ fprintf(stderr, "libgit2 panic: %s\n", message);
+ _Exit(255);
}
void giterr_set(int error_class, const char *string, ...)
diff --git a/src/filebuf.c b/src/filebuf.c
index 9c3dae811..fb98c9e8a 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -29,7 +29,7 @@ static int verify_last_error(git_filebuf *file)
return -1;
case BUFERR_MEM:
- giterr_set_oom();
+ giterr_panic("buffer out of memory");
return -1;
case BUFERR_ZLIB:
diff --git a/src/pack-objects.c b/src/pack-objects.c
index c4ed4dce3..b6a08ddb0 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -217,10 +217,6 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
po->hash = name_hash(name);
pos = kh_put(oid, pb->object_ix, &po->id, &ret);
- if (ret < 0) {
- giterr_set_oom();
- return ret;
- }
assert(ret != 0);
kh_value(pb->object_ix, pos) = po;
diff --git a/src/settings.c b/src/settings.c
index 748f76560..6ddf71243 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -125,6 +125,10 @@ int git_libgit2_opts(int key, ...)
case GIT_OPT_SET_TEMPLATE_PATH:
error = git_futils_dirs_set(GIT_FUTILS_DIR_TEMPLATE, va_arg(ap, const char *));
break;
+
+ case GIT_OPT_SET_PANIC_HANDLER:
+ git__on_panic = va_arg(ap, void *);
+ break;
}
va_end(ap);
diff --git a/src/util.h b/src/util.h
index f9de909e9..b283d1954 100644
--- a/src/util.h
+++ b/src/util.h
@@ -27,21 +27,21 @@
GIT_INLINE(void *) git__malloc(size_t len)
{
void *ptr = malloc(len);
- if (!ptr) giterr_set_oom();
+ if (!ptr) giterr_panic("malloc() failed");
return ptr;
}
GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
{
void *ptr = calloc(nelem, elsize);
- if (!ptr) giterr_set_oom();
+ if (!ptr) giterr_panic("calloc() failed");
return ptr;
}
GIT_INLINE(char *) git__strdup(const char *str)
{
char *ptr = strdup(str);
- if (!ptr) giterr_set_oom();
+ if (!ptr) giterr_panic("strdup() failed");
return ptr;
}
@@ -55,9 +55,6 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
ptr = (char*)git__malloc(length + 1);
- if (!ptr)
- return NULL;
-
if (length)
memcpy(ptr, str, length);
@@ -78,7 +75,7 @@ GIT_INLINE(char *) git__substrdup(const char *start, size_t n)
GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
{
void *new_ptr = realloc(ptr, size);
- if (!new_ptr) giterr_set_oom();
+ if (!new_ptr) giterr_panic("realloc() failed");
return new_ptr;
}
diff --git a/src/zstream.c b/src/zstream.c
index 7def0440b..db342336c 100644
--- a/src/zstream.c
+++ b/src/zstream.c
@@ -15,7 +15,7 @@
static int zstream_seterr(int zerr, git_zstream *zstream)
{
if (zerr == Z_MEM_ERROR)
- giterr_set_oom();
+ giterr_panic("zstream out of memory");
else if (zstream->msg)
giterr_set(GITERR_ZLIB, zstream->msg);
else
diff --git a/tests/core/errors.c b/tests/core/errors.c
index 512a4134d..237edda7c 100644
--- a/tests/core/errors.c
+++ b/tests/core/errors.c
@@ -7,15 +7,6 @@ void test_core_errors__public_api(void)
giterr_clear();
cl_assert(giterr_last() == NULL);
- giterr_set_oom();
-
- cl_assert(giterr_last() != NULL);
- cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
- str_in_error = strstr(giterr_last()->message, "memory");
- cl_assert(str_in_error != NULL);
-
- giterr_clear();
-
giterr_set_str(GITERR_REPOSITORY, "This is a test");
cl_assert(giterr_last() != NULL);
@@ -37,15 +28,6 @@ void test_core_errors__new_school(void)
giterr_clear();
cl_assert(giterr_last() == NULL);
- giterr_set_oom(); /* internal fn */
-
- cl_assert(giterr_last() != NULL);
- cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
- str_in_error = strstr(giterr_last()->message, "memory");
- cl_assert(str_in_error != NULL);
-
- giterr_clear();
-
giterr_set(GITERR_REPOSITORY, "This is a test"); /* internal fn */
cl_assert(giterr_last() != NULL);