ucx/utils.c

changeset 174
bbfe511cfddb
parent 173
31a8682fffb7
child 177
11ad03783baf
     1.1 --- a/ucx/utils.c	Tue Jun 10 15:43:13 2014 +0200
     1.2 +++ b/ucx/utils.c	Wed Jun 11 09:27:02 2014 +0200
     1.3 @@ -213,29 +213,35 @@
     1.4      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
     1.5      if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
     1.6          s.ptr = (char*)almalloc(a, ret + 1);
     1.7 -        s.length = (size_t)ret;
     1.8 -        memcpy(s.ptr, buf, ret);
     1.9 -        s.ptr[s.length] = '\0';
    1.10 +        if (s.ptr) {
    1.11 +            s.length = (size_t)ret;
    1.12 +            memcpy(s.ptr, buf, ret);
    1.13 +            s.ptr[s.length] = '\0';
    1.14 +        }
    1.15      } else if (ret == INT_MAX) {
    1.16          errno = ENOMEM;
    1.17      } else  {
    1.18          int len = ret + 1;
    1.19          s.ptr = (char*)almalloc(a, len);
    1.20 -        ret = vsnprintf(s.ptr, len, fmt, ap2);
    1.21 -        if (ret < 0) {
    1.22 -            free(s.ptr);
    1.23 -            s.ptr = NULL;
    1.24 -        } else {
    1.25 -            s.length = (size_t)ret;
    1.26 +        if (s.ptr) {
    1.27 +            ret = vsnprintf(s.ptr, len, fmt, ap2);
    1.28 +            if (ret < 0) {
    1.29 +                free(s.ptr);
    1.30 +                s.ptr = NULL;
    1.31 +            } else {
    1.32 +                s.length = (size_t)ret;
    1.33 +            }
    1.34          }
    1.35      }
    1.36  #else
    1.37      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
    1.38      if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
    1.39          s.ptr = (char*)almalloc(a, ret + 1);
    1.40 -        s.length = (size_t)ret;
    1.41 -        memcpy(s.ptr, buf, ret);
    1.42 -        s.ptr[s.length] = '\0';
    1.43 +        if (s.ptr) {
    1.44 +            s.length = (size_t)ret;
    1.45 +            memcpy(s.ptr, buf, ret);
    1.46 +            s.ptr[s.length] = '\0';
    1.47 +        }
    1.48      } else {
    1.49          errno = ENOMEM;
    1.50      }

mercurial