[BACK]Return to xmalloc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Diff for /src/usr.bin/tmux/xmalloc.c between version 1.8 and 1.9

version 1.8, 2015/11/17 18:25:03 version 1.9, 2015/11/18 13:06:54
Line 1 
Line 1 
 /* $OpenBSD$ */  /* $OpenBSD$ */
   
 /*  /*
  * Copyright (c) 2004 Nicholas Marriott <nicm@users.sourceforge.net>   * Author: Tatu Ylonen <ylo@cs.hut.fi>
    * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
    *                    All rights reserved
    * Versions of malloc and friends that check their results, and never return
    * failure (they call fatal if they encounter an error).
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * As far as I am concerned, the code I have written for this software
  * purpose with or without fee is hereby granted, provided that the above   * can be used freely for any purpose.  Any derived versions of this
  * copyright notice and this permission notice appear in all copies.   * software must be clearly marked as such, and if the derived work is
  *   * incompatible with the protocol description in the RFC file, it must be
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES   * called by a name other than "ssh" or "Secure Shell".
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  
  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER  
  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING  
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  
  */   */
   
 #include <sys/types.h>  #include <errno.h>
   #include <limits.h>
 #include <stdint.h>  #include <stdint.h>
   #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
 #include "tmux.h"  #include "tmux.h"
   
 char *  void *
 xstrdup(const char *str)  xmalloc(size_t size)
 {  {
         char    *cp;          void *ptr;
   
         if ((cp = strdup(str)) == NULL)          if (size == 0)
                 fatal("xstrdup");                  fatal("xmalloc: zero size");
         return (cp);          ptr = malloc(size);
           if (ptr == NULL)
                   fatal("xmalloc: allocating %zu bytes: %s",
                       size, strerror(errno));
           return ptr;
 }  }
   
 void *  void *
 xcalloc(size_t nmemb, size_t size)  xcalloc(size_t nmemb, size_t size)
 {  {
         void    *ptr;          void *ptr;
   
         if (size == 0 || nmemb == 0)          if (size == 0 || nmemb == 0)
                 fatalx("xcalloc: zero size");                  fatal("xcalloc: zero size");
         if ((ptr = calloc(nmemb, size)) == NULL)          ptr = calloc(nmemb, size);
                 log_fatal("xcalloc: allocating %zu bytes", size);          if (ptr == NULL)
                   fatal("xcalloc: allocating %zu * %zu bytes: %s",
         return (ptr);                      nmemb, size, strerror(errno));
           return ptr;
 }  }
   
 void *  void *
 xmalloc(size_t size)  xrealloc(void *ptr, size_t size)
 {  {
         void    *ptr;          return xreallocarray(ptr, 1, size);
   
         if (size == 0)  
                 fatalx("xmalloc: zero size");  
         if ((ptr = malloc(size)) == NULL)  
                 log_fatal("xmalloc: allocating %zu bytes", size);  
   
         return (ptr);  
 }  }
   
 void *  void *
 xrealloc(void *oldptr, size_t newsize)  xreallocarray(void *ptr, size_t nmemb, size_t size)
 {  {
         void    *newptr;          void *new_ptr;
   
         if (newsize == 0)          if (nmemb == 0 || size == 0)
                 fatalx("xrealloc: zero size");                  fatal("xreallocarray: zero size");
         if ((newptr = realloc(oldptr, newsize)) == NULL)          new_ptr = reallocarray(ptr, nmemb, size);
                 log_fatal("xrealloc: allocating %zu bytes", newsize);          if (new_ptr == NULL)
                   fatal("xreallocarray: allocating %zu * %zu bytes: %s",
         return (newptr);                      nmemb, size, strerror(errno));
           return new_ptr;
 }  }
   
 void *  char *
 xreallocarray(void *oldptr, size_t nmemb, size_t size)  xstrdup(const char *str)
 {  {
         void    *newptr;          char *cp;
   
         if (nmemb == 0 || size == 0)          if ((cp = strdup(str)) == NULL)
                 fatalx("xreallocarray: zero size");                  fatal("xstrdup: %s", strerror(errno));
         if ((newptr = reallocarray(oldptr, nmemb, size)) == NULL)          return cp;
                 log_fatal("xreallocarray: allocating %zu * %zu bytes",  
                     nmemb, size);  
   
         return (newptr);  
 }  }
   
 int  int
 xasprintf(char **ret, const char *fmt, ...)  xasprintf(char **ret, const char *fmt, ...)
 {  {
         va_list ap;          va_list ap;
         int     i;          int i;
   
         va_start(ap, fmt);          va_start(ap, fmt);
         i = xvasprintf(ret, fmt, ap);          i = xvasprintf(ret, fmt, ap);
         va_end(ap);          va_end(ap);
   
         return (i);          return i;
 }  }
   
 int  int
 xvasprintf(char **ret, const char *fmt, va_list ap)  xvasprintf(char **ret, const char *fmt, va_list ap)
 {  {
         int     i;          int i;
   
         i = vasprintf(ret, fmt, ap);          i = vasprintf(ret, fmt, ap);
   
         if (i < 0 || *ret == NULL)          if (i < 0 || *ret == NULL)
                 fatal("xvasprintf");                  fatal("xasprintf: %s", strerror(errno));
   
         return (i);          return i;
 }  }
   
 int  int
 xsnprintf(char *buf, size_t len, const char *fmt, ...)  xsnprintf(char *str, size_t len, const char *fmt, ...)
 {  {
         va_list ap;          va_list ap;
         int     i;          int i;
   
         va_start(ap, fmt);          va_start(ap, fmt);
         i = xvsnprintf(buf, len, fmt, ap);          i = xvsnprintf(str, len, fmt, ap);
         va_end(ap);          va_end(ap);
   
         return (i);          return i;
 }  }
   
 int  int
 xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap)  xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
 {  {
         int     i;          int i;
   
         if (len > INT_MAX)          if (len > INT_MAX)
                 fatalx("xvsnprintf: len > INT_MAX");                  fatal("xsnprintf: len > INT_MAX");
   
         i = vsnprintf(buf, len, fmt, ap);          i = vsnprintf(str, len, fmt, ap);
   
         if (i < 0 || i >= (int)len)          if (i < 0 || i >= (int)len)
                 fatalx("xvsnprintf: overflow");                  fatal("xsnprintf: overflow");
   
         return (i);          return i;
 }  }

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9