]> git.imager.perl.org - imager.git/blobdiff - error.c
API documentation (mostly)
[imager.git] / error.c
diff --git a/error.c b/error.c
index 4e6c0622a89af44af2da126f5d1e435730fd58a1..a3f172b03d4a2d5f0cf1f8225e9a8007d0a80cd6 100644 (file)
--- a/error.c
+++ b/error.c
@@ -61,19 +61,19 @@ C).  The Perl level won't use all of this.
 =cut
 */
 
-#include "image.h"
+#include "imager.h"
 #include <stdio.h>
 #include <stdlib.h>
 
 /* we never actually use the last item - it's the NULL terminator */
 #define ERRSTK 20
-i_errmsg error_stack[ERRSTK];
-int error_sp = ERRSTK - 1;
+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. */
-int error_space[ERRSTK];
+static int error_space[ERRSTK];
 
 static i_error_cb error_cb;
 static i_failed_cb failed_cb;
@@ -95,7 +95,9 @@ void i_set_argv0(char const *name) {
   char *dupl;
   if (!name)
     return;
-  dupl = mymalloc(strlen(name)+1);
+  /* if the user has an existing string of MAXINT length then
+     the system is broken anyway */
+  dupl = mymalloc(strlen(name)+1); /* check 17jul05 tonyc */
   strcpy(dupl, name);
   if (argv0)
     myfree(argv0);
@@ -181,18 +183,37 @@ the mark.
 =over
 
 =item i_clear_error()
+=synopsis i_clear_error();
+=category Error handling
+
+Clears the error stack.
 
 Called by any imager function before doing any other processing.
 
-=cut */
+=cut
+*/
 void i_clear_error() {
+#ifdef IMAGER_DEBUG_MALLOC
+  int i;
+
+  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
   error_sp = ERRSTK-1;
 }
 
 /*
 =item i_push_error(int code, char const *msg)
+=synopsis i_push_error(0, "Yep, it's broken");
+=synopsis i_push_error(errno, "Error writing");
+=category Error handling
 
-Called by an imager function to push an error message onto the stack.
+Called by an Imager function to push an error message onto the stack.
 
 No message is pushed if the stack is full (since this means someone
 forgot to call i_clear_error(), or that a function that doesn't do
@@ -211,9 +232,11 @@ void i_push_error(int code, char const *msg) {
   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 release when 
+    /* memory allocated on the following line is only ever released when 
        we need a bigger string */
-    error_stack[error_sp].msg = mymalloc(size);
+    /* size is size (len+1) of an existing string, overflow would mean
+       the system is broken anyway */
+    error_stack[error_sp].msg = mymalloc(size); /* checked 17jul05 tonyc */
     error_space[error_sp] = size;
   }
   strcpy(error_stack[error_sp].msg, msg);
@@ -226,9 +249,13 @@ void i_push_error(int code, char const *msg) {
 /*
 =item i_push_errorvf(int code, char const *fmt, va_list ap)
 
+=category Error handling
+
 Intended for use by higher level functions, takes a varargs pointer
 and a format to produce the finally pushed error message.
 
+Does not support perl specific format codes.
+
 =cut
 */
 void i_push_errorvf(int code, char const *fmt, va_list ap) {
@@ -247,9 +274,13 @@ void i_push_errorvf(int code, char const *fmt, va_list ap) {
 
 /*
 =item i_push_errorf(int code, char const *fmt, ...)
+=synopsis i_push_errorf(errno, "Cannot open file %s: %d", filename, errno);
+=category Error handling
 
 A version of i_push_error() that does printf() like formating.
 
+Does not support perl specific format codes.
+
 =cut
 */
 void i_push_errorf(int code, char const *fmt, ...) {
@@ -259,6 +290,9 @@ void i_push_errorf(int code, char const *fmt, ...) {
   va_end(ap);
 }
 
+#ifdef IMAGER_I_FAILED
+#error "This isn't used and is untested"
+
 /*
 =item i_failed(char const *msg)
 
@@ -292,12 +326,12 @@ int i_failed(int code, char const *msg) {
       ++sp;
     }
     /* we want to log the error too, build an error message to hand to
-       m_fatal() */
+       i_fatal() */
     total = 1; /* remember the NUL */
     for (sp = error_sp; error_stack[sp].msg; ++sp) {
       total += strlen(error_stack[sp].msg) + 2;
     }
-    full = malloc(total);
+    full = mymalloc(total);
     if (!full) {
       /* just quit, at least it's on stderr */
       exit(EXIT_FAILURE);
@@ -309,12 +343,14 @@ int i_failed(int code, char const *msg) {
     }
     /* lose the extra ": " */
     full[strlen(full)-2] = '\0';
-    m_fatal(EXIT_FAILURE, "%s", full);
+    i_fatal(EXIT_FAILURE, "%s", full);
   }
 
   return 0;
 }
 
+#endif
+
 /*
 =back