fixed missing malloc return value validation in ucx_vasprintf

Wed, 11 Jun 2014 09:27:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 11 Jun 2014 09:27:02 +0200
changeset 174
bbfe511cfddb
parent 173
31a8682fffb7
child 175
f5aa799abd86

fixed missing malloc return value validation in ucx_vasprintf

ucx/allocator.h file | annotate | diff | comparison | revisions
ucx/utils.c file | annotate | diff | comparison | revisions
--- a/ucx/allocator.h	Tue Jun 10 15:43:13 2014 +0200
+++ b/ucx/allocator.h	Wed Jun 11 09:27:02 2014 +0200
@@ -162,7 +162,7 @@
  * @param n size of space to allocate
  * @return a pointer to the allocated memory area
  */
-#define almalloc(allocator, n) ((allocator)->malloc(allocator->pool, n))
+#define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n))
 
 /**
  * Shorthand for calling an allocators calloc function.
@@ -172,7 +172,7 @@
  * @return a pointer to the allocated memory area
  */
 #define alcalloc(allocator, n, size) \
-        ((allocator)->calloc(allocator->pool, n, size))
+        ((allocator)->calloc((allocator)->pool, n, size))
 
 /**
  * Shorthand for calling an allocators realloc function.
@@ -182,14 +182,14 @@
  * @return a pointer to the reallocated memory area
  */
 #define alrealloc(allocator, ptr, n) \
-        ((allocator)->realloc(allocator->pool, ptr, n))
+        ((allocator)->realloc((allocator)->pool, ptr, n))
 
 /**
  * Shorthand for calling an allocators free function.
  * @param allocator the allocator to use
  * @param ptr the pointer to the memory area that shall be freed
  */
-#define alfree(allocator, ptr) ((allocator)->free(allocator->pool, ptr))
+#define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr))
 
 /**
  * Convenient macro for a default allocator <code>struct</code> definition.
--- a/ucx/utils.c	Tue Jun 10 15:43:13 2014 +0200
+++ b/ucx/utils.c	Wed Jun 11 09:27:02 2014 +0200
@@ -213,29 +213,35 @@
     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
     if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
         s.ptr = (char*)almalloc(a, ret + 1);
-        s.length = (size_t)ret;
-        memcpy(s.ptr, buf, ret);
-        s.ptr[s.length] = '\0';
+        if (s.ptr) {
+            s.length = (size_t)ret;
+            memcpy(s.ptr, buf, ret);
+            s.ptr[s.length] = '\0';
+        }
     } else if (ret == INT_MAX) {
         errno = ENOMEM;
     } else  {
         int len = ret + 1;
         s.ptr = (char*)almalloc(a, len);
-        ret = vsnprintf(s.ptr, len, fmt, ap2);
-        if (ret < 0) {
-            free(s.ptr);
-            s.ptr = NULL;
-        } else {
-            s.length = (size_t)ret;
+        if (s.ptr) {
+            ret = vsnprintf(s.ptr, len, fmt, ap2);
+            if (ret < 0) {
+                free(s.ptr);
+                s.ptr = NULL;
+            } else {
+                s.length = (size_t)ret;
+            }
         }
     }
 #else
     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
     if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
         s.ptr = (char*)almalloc(a, ret + 1);
-        s.length = (size_t)ret;
-        memcpy(s.ptr, buf, ret);
-        s.ptr[s.length] = '\0';
+        if (s.ptr) {
+            s.length = (size_t)ret;
+            memcpy(s.ptr, buf, ret);
+            s.ptr[s.length] = '\0';
+        }
     } else {
         errno = ENOMEM;
     }

mercurial