xmalloc.c
changeset 0 1d0ce1ebbc72
equal deleted inserted replaced
-1:000000000000 0:1d0ce1ebbc72
       
     1 /* $OpenBSD: xmalloc.c,v 1.11 2016/11/17 10:06:08 nicm Exp $ */
       
     2 
       
     3 /*
       
     4  * Author: Tatu Ylonen <ylo@cs.hut.fi>
       
     5  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
       
     6  *                    All rights reserved
       
     7  * Versions of malloc and friends that check their results, and never return
       
     8  * failure (they call fatalx if they encounter an error).
       
     9  *
       
    10  * As far as I am concerned, the code I have written for this software
       
    11  * can be used freely for any purpose.  Any derived versions of this
       
    12  * software must be clearly marked as such, and if the derived work is
       
    13  * incompatible with the protocol description in the RFC file, it must be
       
    14  * called by a name other than "ssh" or "Secure Shell".
       
    15  */
       
    16 
       
    17 #include <err.h>
       
    18 #include <errno.h>
       
    19 #include <limits.h>
       
    20 #include <stdarg.h>
       
    21 #include <stdint.h>
       
    22 #include <stdio.h>
       
    23 #include <stdlib.h>
       
    24 #include <string.h>
       
    25 
       
    26 #include "xmalloc.h"
       
    27 
       
    28 void *
       
    29 xmalloc(size_t size)
       
    30 {
       
    31 	void *ptr;
       
    32 
       
    33 	if (size == 0)
       
    34 		errx(1, "xmalloc: zero size");
       
    35 	ptr = malloc(size);
       
    36 	if (ptr == NULL)
       
    37 		err(1, "xmalloc: allocating %zu bytes", size);
       
    38 	return ptr;
       
    39 }
       
    40 
       
    41 void *
       
    42 xcalloc(size_t nmemb, size_t size)
       
    43 {
       
    44 	void *ptr;
       
    45 
       
    46 	if (size == 0 || nmemb == 0)
       
    47 		errx(1, "xcalloc: zero size");
       
    48 	ptr = calloc(nmemb, size);
       
    49 	if (ptr == NULL)
       
    50 		err(1, "xcalloc: allocating %zu * %zu bytes", nmemb, size);
       
    51 	return ptr;
       
    52 }
       
    53 
       
    54 void *
       
    55 xrealloc(void *ptr, size_t size)
       
    56 {
       
    57 	return xreallocarray(ptr, 1, size);
       
    58 }
       
    59 
       
    60 void *
       
    61 xreallocarray(void *ptr, size_t nmemb, size_t size)
       
    62 {
       
    63 	void *new_ptr;
       
    64 
       
    65 	if (nmemb == 0 || size == 0)
       
    66 		errx(1, "xreallocarray: zero size");
       
    67 	new_ptr = reallocarray(ptr, nmemb, size);
       
    68 	if (new_ptr == NULL)
       
    69 		err(1, "xreallocarray: allocating %zu * %zu bytes",
       
    70 		    nmemb, size);
       
    71 	return new_ptr;
       
    72 }
       
    73 
       
    74 char *
       
    75 xstrdup(const char *str)
       
    76 {
       
    77 	char *cp;
       
    78 
       
    79 	if ((cp = strdup(str)) == NULL)
       
    80 		err(1, "xstrdup");
       
    81 	return cp;
       
    82 }
       
    83 
       
    84 char *
       
    85 xstrndup(const char *str, size_t maxlen)
       
    86 {
       
    87 	char *cp;
       
    88 
       
    89 	if ((cp = strndup(str, maxlen)) == NULL)
       
    90 		err(1, "xstrndup");
       
    91 	return cp;
       
    92 }
       
    93 
       
    94 int
       
    95 xasprintf(char **ret, const char *fmt, ...)
       
    96 {
       
    97 	va_list ap;
       
    98 	int i;
       
    99 
       
   100 	va_start(ap, fmt);
       
   101 	i = xvasprintf(ret, fmt, ap);
       
   102 	va_end(ap);
       
   103 
       
   104 	return i;
       
   105 }
       
   106 
       
   107 int
       
   108 xvasprintf(char **ret, const char *fmt, va_list ap)
       
   109 {
       
   110 	int i;
       
   111 
       
   112 	i = vasprintf(ret, fmt, ap);
       
   113 
       
   114 	if (i < 0 || *ret == NULL)
       
   115 		err(1, "xasprintf");
       
   116 
       
   117 	return i;
       
   118 }
       
   119 
       
   120 int
       
   121 xsnprintf(char *str, size_t len, const char *fmt, ...)
       
   122 {
       
   123 	va_list ap;
       
   124 	int i;
       
   125 
       
   126 	va_start(ap, fmt);
       
   127 	i = xvsnprintf(str, len, fmt, ap);
       
   128 	va_end(ap);
       
   129 
       
   130 	return i;
       
   131 }
       
   132 
       
   133 int
       
   134 xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
       
   135 {
       
   136 	int i;
       
   137 
       
   138 	if (len > INT_MAX)
       
   139 		errx(1, "xsnprintf: len > INT_MAX");
       
   140 
       
   141 	i = vsnprintf(str, len, fmt, ap);
       
   142 
       
   143 	if (i < 0 || i >= (int)len)
       
   144 		errx(1, "xsnprintf: overflow");
       
   145 
       
   146 	return i;
       
   147 }