about summary refs log tree commit diff
path: root/lib/libpm.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2009-05-03 01:05:05 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2009-05-03 01:05:05 +0000
commit11a80cd8ceb0787c3477d626b028f8f79e82f482 (patch)
tree4eca5db339025a78f56bb27fbade10e16f2a6b23 /lib/libpm.c
parentd183dec220bbedbcac8081dc6fa5a81dde9b23f3 (diff)
downloadnetpbm-mirror-11a80cd8ceb0787c3477d626b028f8f79e82f482.tar.gz
netpbm-mirror-11a80cd8ceb0787c3477d626b028f8f79e82f482.tar.xz
netpbm-mirror-11a80cd8ceb0787c3477d626b028f8f79e82f482.zip
handle zero allocations
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@909 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libpm.c')
-rw-r--r--lib/libpm.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/libpm.c b/lib/libpm.c
index 1adad8b6..1089da3c 100644
--- a/lib/libpm.c
+++ b/lib/libpm.c
@@ -190,17 +190,25 @@ pm_error(const char format[], ...) {
 
 
 
+static void *
+mallocz(size_t const size) {
+
+    return malloc(MAX(1, size));
+}
+
+
+
 void *
 pm_allocrow(unsigned int const cols,
             unsigned int const size) {
 
     unsigned char * itrow;
 
-    if (UINT_MAX / cols < size)
+    if (cols != 0 && UINT_MAX / cols < size)
         pm_error("Arithmetic overflow multiplying %u by %u to get the "
                  "size of a row to allocate.", cols, size);
 
-    itrow = malloc(cols * size);
+    itrow = mallocz(cols * size);
     if (itrow == NULL)
         pm_error("out of memory allocating a row");
 
@@ -223,7 +231,7 @@ allocarrayNoHeap(unsigned char ** const rowIndex,
                  unsigned int     const size,
                  const char **    const errorP) {
 
-    if (UINT_MAX / cols < size)
+    if (cols != 0 && UINT_MAX / cols < size)
         asprintfN(errorP,
                   "Arithmetic overflow multiplying %u by %u to get the "
                   "size of a row to allocate.", cols, size);
@@ -234,7 +242,7 @@ allocarrayNoHeap(unsigned char ** const rowIndex,
         *errorP = NULL;
 
         while (rowsDone < rows && !*errorP) {
-            unsigned char * const rowSpace = malloc(cols * size);
+            unsigned char * const rowSpace = mallocz(cols * size);
             if (rowSpace == NULL)
                 asprintfN(errorP,
                           "Unable to allocate a %u-column by %u byte row",
@@ -259,11 +267,11 @@ allocRowHeap(unsigned int const cols,
 
     unsigned char * retval;
 
-    if (UINT_MAX / cols / rows < size)
+    if (cols != 0 && rows != 0 && UINT_MAX / cols / rows < size)
         /* Too big even to request the memory ! */
         retval = NULL;
     else
-        retval = malloc(rows * cols * size);
+        retval = mallocz(rows * cols * size);
 
     return retval;
 }