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

Annotation of src/usr.bin/rcs/xmalloc.c, Revision 1.11

1.11    ! tobias      1: /* $OpenBSD: xmalloc.c,v 1.10 2015/06/17 20:50:10 nicm Exp $ */
1.1       joris       2: /*
                      3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Versions of malloc and friends that check their results, and never return
                      7:  * failure (they call fatal if they encounter an error).
                      8:  *
                      9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
                     14:  */
                     15:
1.3       xsa        16: #include <err.h>
                     17: #include <stdarg.h>
1.7       millert    18: #include <stdint.h>
1.3       xsa        19: #include <stdio.h>
                     20: #include <stdlib.h>
                     21: #include <string.h>
1.1       joris      22:
                     23: #include "xmalloc.h"
                     24:
                     25: void *
                     26: xmalloc(size_t size)
                     27: {
                     28:        void *ptr;
                     29:
                     30:        if (size == 0)
                     31:                errx(1, "xmalloc: zero size");
                     32:        ptr = malloc(size);
                     33:        if (ptr == NULL)
1.11    ! tobias     34:                err(1, "xmalloc: allocating %zu bytes", size);
1.1       joris      35:        return ptr;
                     36: }
                     37:
                     38: void *
                     39: xcalloc(size_t nmemb, size_t size)
                     40: {
                     41:        void *ptr;
                     42:
                     43:        if (size == 0 || nmemb == 0)
                     44:                errx(1, "xcalloc: zero size");
                     45:        ptr = calloc(nmemb, size);
                     46:        if (ptr == NULL)
1.11    ! tobias     47:                err(1, "xcalloc: allocating %zu * %zu bytes", nmemb, size);
1.1       joris      48:        return ptr;
                     49: }
                     50:
                     51: void *
1.6       deraadt    52: xreallocarray(void *ptr, size_t nmemb, size_t size)
1.1       joris      53: {
                     54:        void *new_ptr;
                     55:
1.6       deraadt    56:        new_ptr = reallocarray(ptr, nmemb, size);
1.1       joris      57:        if (new_ptr == NULL)
1.11    ! tobias     58:                err(1, "xreallocarray: allocating %zu * %zu bytes",
        !            59:                    nmemb, size);
1.1       joris      60:        return new_ptr;
                     61: }
                     62:
                     63: char *
                     64: xstrdup(const char *str)
                     65: {
                     66:        char *cp;
                     67:
1.10      nicm       68:        if ((cp = strdup(str)) == NULL)
                     69:                err(1, "xstrdup");
1.1       joris      70:        return cp;
                     71: }
                     72:
                     73: int
                     74: xasprintf(char **ret, const char *fmt, ...)
                     75: {
                     76:        va_list ap;
                     77:        int i;
                     78:
                     79:        va_start(ap, fmt);
                     80:        i = vasprintf(ret, fmt, ap);
                     81:        va_end(ap);
                     82:
                     83:        if (i < 0 || *ret == NULL)
1.11    ! tobias     84:                err(1, "xasprintf");
1.1       joris      85:
1.11    ! tobias     86:        return i;
1.1       joris      87: }