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
     1.1 --- a/ucx/allocator.h	Tue Jun 10 15:43:13 2014 +0200
     1.2 +++ b/ucx/allocator.h	Wed Jun 11 09:27:02 2014 +0200
     1.3 @@ -162,7 +162,7 @@
     1.4   * @param n size of space to allocate
     1.5   * @return a pointer to the allocated memory area
     1.6   */
     1.7 -#define almalloc(allocator, n) ((allocator)->malloc(allocator->pool, n))
     1.8 +#define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n))
     1.9  
    1.10  /**
    1.11   * Shorthand for calling an allocators calloc function.
    1.12 @@ -172,7 +172,7 @@
    1.13   * @return a pointer to the allocated memory area
    1.14   */
    1.15  #define alcalloc(allocator, n, size) \
    1.16 -        ((allocator)->calloc(allocator->pool, n, size))
    1.17 +        ((allocator)->calloc((allocator)->pool, n, size))
    1.18  
    1.19  /**
    1.20   * Shorthand for calling an allocators realloc function.
    1.21 @@ -182,14 +182,14 @@
    1.22   * @return a pointer to the reallocated memory area
    1.23   */
    1.24  #define alrealloc(allocator, ptr, n) \
    1.25 -        ((allocator)->realloc(allocator->pool, ptr, n))
    1.26 +        ((allocator)->realloc((allocator)->pool, ptr, n))
    1.27  
    1.28  /**
    1.29   * Shorthand for calling an allocators free function.
    1.30   * @param allocator the allocator to use
    1.31   * @param ptr the pointer to the memory area that shall be freed
    1.32   */
    1.33 -#define alfree(allocator, ptr) ((allocator)->free(allocator->pool, ptr))
    1.34 +#define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr))
    1.35  
    1.36  /**
    1.37   * Convenient macro for a default allocator <code>struct</code> definition.
     2.1 --- a/ucx/utils.c	Tue Jun 10 15:43:13 2014 +0200
     2.2 +++ b/ucx/utils.c	Wed Jun 11 09:27:02 2014 +0200
     2.3 @@ -213,29 +213,35 @@
     2.4      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
     2.5      if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
     2.6          s.ptr = (char*)almalloc(a, ret + 1);
     2.7 -        s.length = (size_t)ret;
     2.8 -        memcpy(s.ptr, buf, ret);
     2.9 -        s.ptr[s.length] = '\0';
    2.10 +        if (s.ptr) {
    2.11 +            s.length = (size_t)ret;
    2.12 +            memcpy(s.ptr, buf, ret);
    2.13 +            s.ptr[s.length] = '\0';
    2.14 +        }
    2.15      } else if (ret == INT_MAX) {
    2.16          errno = ENOMEM;
    2.17      } else  {
    2.18          int len = ret + 1;
    2.19          s.ptr = (char*)almalloc(a, len);
    2.20 -        ret = vsnprintf(s.ptr, len, fmt, ap2);
    2.21 -        if (ret < 0) {
    2.22 -            free(s.ptr);
    2.23 -            s.ptr = NULL;
    2.24 -        } else {
    2.25 -            s.length = (size_t)ret;
    2.26 +        if (s.ptr) {
    2.27 +            ret = vsnprintf(s.ptr, len, fmt, ap2);
    2.28 +            if (ret < 0) {
    2.29 +                free(s.ptr);
    2.30 +                s.ptr = NULL;
    2.31 +            } else {
    2.32 +                s.length = (size_t)ret;
    2.33 +            }
    2.34          }
    2.35      }
    2.36  #else
    2.37      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
    2.38      if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
    2.39          s.ptr = (char*)almalloc(a, ret + 1);
    2.40 -        s.length = (size_t)ret;
    2.41 -        memcpy(s.ptr, buf, ret);
    2.42 -        s.ptr[s.length] = '\0';
    2.43 +        if (s.ptr) {
    2.44 +            s.length = (size_t)ret;
    2.45 +            memcpy(s.ptr, buf, ret);
    2.46 +            s.ptr[s.length] = '\0';
    2.47 +        }
    2.48      } else {
    2.49          errno = ENOMEM;
    2.50      }

mercurial