]> git.imager.perl.org - imager.git/blobdiff - error.c
check for the uninitialized gif89 bug in 4.2.0 and probe for the 5.0.1 api
[imager.git] / error.c
diff --git a/error.c b/error.c
index c234147ca9012b1e9f5ea4150925aea51b8f3c0f..84785a2a2fd59d0a188b979198b84e55ec8cb991 100644 (file)
--- a/error.c
+++ b/error.c
@@ -61,15 +61,25 @@ C).  The Perl level won't use all of this.
 =cut
 */
 
-#include "imageri.h"
+#include "imager.h"
 #include <stdio.h>
 #include <stdlib.h>
 
-#if 0
+/* we never actually use the last item - it's the NULL terminator */
+#define ERRSTK 20
+static i_errmsg error_stack[ERRSTK];
+static int error_sp = ERRSTK - 1;
+/* we track the amount of space used each string, so we don't reallocate 
+   space unless we need to.
+   This also means that a memory tracking library may see the memory 
+   allocated for this as a leak. */
+static int error_space[ERRSTK];
+
 static i_error_cb error_cb;
 static i_failed_cb failed_cb;
 static int failures_fatal;
 static char *argv0;
+
 /*
 =item i_set_argv0(char const *program)
 
@@ -147,8 +157,6 @@ i_failed_cb i_set_failed_cb(i_failed_cb cb) {
   return old;
 }
 
-#endif
-
 /*
 =item i_errors()
 
@@ -157,12 +165,8 @@ terminated by a NULL pointer.  The highest level message is first.
 
 =cut
 */
-i_errmsg *im_errors(im_context_t ctx) {
-  return ctx->error_stack + ctx->error_sp;
-}
-
-i_errmsg *i_errors(void) {
-  return im_errors(im_get_context());
+i_errmsg *i_errors() {
+  return error_stack + error_sp;
 }
 
 /*
@@ -188,26 +192,19 @@ Called by any Imager function before doing any other processing.
 
 =cut
 */
-
-void
-im_clear_error(im_context_t ctx) {
+void i_clear_error() {
 #ifdef IMAGER_DEBUG_MALLOC
   int i;
 
-  for (i = 0; i < IM_ERROR_COUNT; ++i) {
-    if (ctx->error_space[i]) {
-      myfree(ctx->error_stack[i].msg);
-      ctx->error_stack[i].msg = NULL;
-      ctx->error_space[i] = 0;
+  for (i = 0; i < ERRSTK; ++i) {
+    if (error_space[i]) {
+      myfree(error_stack[i].msg);
+      error_stack[i].msg = NULL;
+      error_space[i] = 0;
     }
   }
 #endif
-  ctx->error_sp = IM_ERROR_COUNT-1;
-}
-
-void
-i_clear_error(void) {
-  im_clear_error(im_get_context());
+  error_sp = ERRSTK-1;
 }
 
 /*
@@ -224,32 +221,29 @@ error handling is calling function that does.).
 
 =cut
 */
-void
-im_push_error(im_context_t ctx, int code, char const *msg) {
+void i_push_error(int code, char const *msg) {
   size_t size = strlen(msg)+1;
 
-  if (ctx->error_sp <= 0)
+  if (error_sp <= 0)
     /* bad, bad programmer */
     return;
 
-  --ctx->error_sp;
-  if (ctx->error_alloc[ctx->error_sp] < size) {
-    if (ctx->error_stack[ctx->error_sp].msg)
-      myfree(ctx->error_stack[ctx->error_sp].msg);
+  --error_sp;
+  if (error_space[error_sp] < size) {
+    if (error_stack[error_sp].msg)
+      myfree(error_stack[error_sp].msg);
     /* memory allocated on the following line is only ever released when 
        we need a bigger string */
     /* size is size (len+1) of an existing string, overflow would mean
        the system is broken anyway */
-    ctx->error_stack[ctx->error_sp].msg = mymalloc(size); /* checked 17jul05 tonyc */
-    ctx->error_alloc[ctx->error_sp] = size;
+    error_stack[error_sp].msg = mymalloc(size); /* checked 17jul05 tonyc */
+    error_space[error_sp] = size;
   }
-  strcpy(ctx->error_stack[ctx->error_sp].msg, msg);
-  ctx->error_stack[ctx->error_sp].code = code;
-}
+  strcpy(error_stack[error_sp].msg, msg);
+  error_stack[error_sp].code = code;
 
-void
-i_push_error(int code, char const *msg) {
-  im_push_error(im_get_context(), code, msg);
+  if (error_cb)
+    error_cb(code, msg);
 }
 
 /*
@@ -264,8 +258,7 @@ Does not support perl specific format codes.
 
 =cut
 */
-void 
-im_push_errorvf(im_context_t ctx, int code, char const *fmt, va_list ap) {
+void i_push_errorvf(int code, char const *fmt, va_list ap) {
   char buf[1024];
 #if defined(IMAGER_VSNPRINTF)
   vsnprintf(buf, sizeof(buf), fmt, ap);
@@ -278,12 +271,7 @@ im_push_errorvf(im_context_t ctx, int code, char const *fmt, va_list ap) {
    */
   vsprintf(buf, fmt, ap);
 #endif
-  im_push_error(ctx, code, buf);
-}
-
-void
-i_push_errorvf(int code, char const *fmt, va_list ap) {
-  im_push_errorvf(im_get_context(), code, fmt, ap);
+  i_push_error(code, buf);
 }
 
 /*
@@ -297,22 +285,13 @@ Does not support perl specific format codes.
 
 =cut
 */
-void
-i_push_errorf(int code, char const *fmt, ...) {
+void i_push_errorf(int code, char const *fmt, ...) {
   va_list ap;
   va_start(ap, fmt);
   i_push_errorvf(code, fmt, ap);
   va_end(ap);
 }
 
-void
-im_push_errorf(im_context_t ctx, int code, char const *fmt, ...) {
-  va_list ap;
-  va_start(ap, fmt);
-  im_push_errorvf(ctx, code, fmt, ap);
-  va_end(ap);
-}
-
 #ifdef IMAGER_I_FAILED
 #error "This isn't used and is untested"
 
@@ -390,7 +369,7 @@ void
 im_assert_fail(char const *file, int line, char const *message) {
   fprintf(stderr, "Assertion failed line %d file %s: %s\n", 
          line, file, message);
-  abort();
+  exit(EXIT_FAILURE);
 }
 
 /*